operations with reference cells proc sql? - sas

I have this table, call it "pre_report":
initial_balance
deposit
withdrawal
final_balance
1000
50
0
.
1000
0
25
.
1000
45
0
.
1000
30
0
.
1000
0
70
.
I want create a code in SAS that updates the "final_balance" field, the "deposit" field adds to the balance and "withdrawal" subtracts, but at the same time changes the values of the "initial_balance" field, in such a way that my desired output be this:
initial_balance
deposit
withdrawal
final_balance
1000
50
0
1050
1050
0
25
1025
1025
45
0
1070
1070
30
0
1100
1100
0
70
1030
I try this:
proc sql;
select initial_balance format=dollar32.2,
deposit format=dollar32.2,
withdrawal format=dollar32.2,
sum(initial_balance,deposit,-withdrawal) as final_balance,
calculated final_balance as initial_balance
from work.pre_report;
quit;
But it doesn't work properly. This code create two fields "final_balance" and "initial_balance" but both with the sames quantity.
code for creating "pre_report" table
data work.pre_report;
input initial_balance deposit withdrawal final_balance;;
datalines;
1000 50 0 .
1000 0 25 .
1000 45 0 .
1000 30 0 .
1000 0 70 .
run;
I would really appreciate if you help me.

Related

Power BI Report with dynamic columns according to slicer selection

I have a table with the columns as below -
There are rows showing the allocation of various people under various project. The months (columns) can extend to Dec,20 and continue on from Jan,21 in the same pattern as above.
One Staff can be tagged to any number of projects in a given month.
Now I want to prepare a Power BI report on this in the format as below -
Staff ID, Project ID and End Date are the slicers to be present.
For the End Date slicer we can select options in the format of (MMM,YY) (eg - Jan,23). On the basis of this slicer I want to show the preceding 6 months of data, as portrayed by the above sample image.
I have tried using parameters but those have to specified for each combination so not usable for this data as this is going to increase over time.
Is there any way to do this or am I missing some simple thing in particular?
Any help on this will be highly appreciated.
Adding in the sample data as below -
Staff ID
Project ID
Jan,20
Feb,20
Mar,20
Apr,20
May,20
Jun,20
Jul,20
1
20
0
0
0
100
80
10
0
1
30
0
0
0
0
20
90
100
2
20
100
100
100
0
0
0
0
3
50
80
100
0
0
0
0
0
3
60
15
0
0
0
20
0
0
3
70
5
0
100
100
80
0
0

DISCOUNT with multiple criteria in Power BI

I have two tables are Data and Report.
Data Table: In the Data table, two columns are Item, Qty, and Order. The Item columns contain as a text & number and qty and number column stored as text and number.
The item column is repeated according to the order and the same item column contains two different qty according to the order column.
Report Table:
I have a unique item column.
Data and Report file looks like.
Data
ITEM QTY ORDER
123 200 1
123 210 0
5678 220 1
5678 230 0
5555 240 1
6666 250 1
9876 260 1
2345 270 1
901 280 1
901 280 1
902 300 1
902 300 1
123456 200 1
123456 200 1
123456 210 1
123456 210 1
123456 0 1
567 200 1
567 210 1
567 210 1
567 0 1
453 5000 1
453 5000 1
453 5000 1
453 5000 1
112 5000 1
112 5000 1
112 5000 1
112 5000 1
116 5000 1
116 5001 1
116 0 1
116 0 1
116 5000 0
116 5001 0
116 0 0
116 0 0
Report
ITEM DESIRED RESULT (QTY)
123 200
5678 220
5555 240
6666 250
9876 260
2345 270
901 280
902 300
123456 MIXED
567 MIXED
4444 NA
12 NA
10 NA
453 5000
112 5000
116 MIXED
Expand snippet
Desired Result
I would like to pull the qty against the order “1” from the data table into the report table according to the item.
If the item is found in the data table then return the qty in the report table according to the item. {Please refer to the “Data” and “Report table for item 123 and 5678 etc….}
If an item is not found in the data table then return “NA” in the report table according to the item. {Please refer to the “Data” and “Report table for item 10, 12,444}
The same item contains two different qty then returns as a text “Mixed” in the report table according to the item. {Please refer to the “Data” and “Report table for item 123456,116 & 567}
Currently I am using the following calculated column CURRENT DAX FOR QTY = LOOKUPVALUE(DATA[QTY],DATA[ITEM],'DESIRED RESULT'[ITEM],DATA[ORDER],1,"NA") enter image description here
It’s almost working fine but it’s giving the wrong result “NA” were two different qty for the same item & two different order (0,1) or (1) or (o) {Please refer to the “Data” and “Report table for item 123456, 116 & 567} but the desired result is “Mixed” those three items.
Note: I convert the qty column from number to text otherwise it gives an error, is there any alternative option to achieve my result.
Herewith attached the PBI file for your reference https://www.dropbox.com/s/hf40q27pvn3ij2g/DAX-LOOKUPVALUE%20FILTER%20BY.pbix?dl=0.
If I'm understanding correctly, this can be done with the method I suggested previously with the addition of a filter for DATA[ORDER] = 1.
IF (
CALCULATE ( DISTINCTCOUNT ( DATA[QTY] ), DATA[ORDER] = 1 ) > 1,
"MIXED",
CALCULATE ( SELECTEDVALUE ( DATA[QTY], "NA" ), DATA[ORDER] = 1 )
)

A dynamic SAS program to consolidate dates of events that are nested within each other

Hello,
I want to write a dynamic program which helps me to flag the start and end dates of events that are nested within the consolidated dates that are present at the top of each Pt.ID in the attached example. I can easily do these if there is only one such consolidated period per Pt.ID. However, there could be more than one such consolidated periods per Pt. ID. (As shown for second Pt.ID, 1002). As shown in the example, the events that fall within the consolidated period/s are fagged as "Y" in the flag variable and if they don't fall within the consolidated period then they are flagged as "N" in this variable. How can I write a program that accounts for all of such consolidated periods per Pt.ID and then compare them with the dates for the rest of the events of a particular patient and flag events which fall within any of those consolidated periods?
Thank you.
So join the event records with the period records and calculate whether the event is within the period. Then you could take the MAX over all periods.
For example here is code for your sample that creates a binary 1/0 flag variable called INCLUDED.
data Sample;
infile datalines missover;
input Pt_ID Event_ID Category $ Start_Date : mmddyy10.
Start_Day End_date : mmddyy10. End_day Duration
;
format Start_date End_date mmddyy10.;
datalines;
1001 . Moderate 8/5/2016 256 9/3/2016 285 30
1001 1 Moderate 3/8/2016 106 3/16/2016 114 9
1001 2 Moderate 8/5/2016 256 8/14/2016 265 10
1001 3 Moderate 8/21/2016 272 8/24/2016 275 4
1001 4 Moderate 8/23/2016 274 9/3/2016 285 12
1002 . Severe 11/28/2016 13 12/19/2016 34 22
1002 . Severe 2/6/2017 83 2/28/2017 105 23
1002 1 Severe 11/28/2016 13 12/5/2016 20 8
1002 2 Severe 12/12/2016 27 12/19/2016 34 8
1002 3 Severe 1/9/2017 55 1/12/2017 58 4
1002 4 Severe 2/6/2017 83 2/13/2017 90 8
1002 5 Severe 2/20/2017 97 2/28/2017 105 9
1002 6 Severe 3/17/2017 122 3/24/2017 129 8
1002 7 Severe 5/4/2017 170 5/13/2017 179 10
1002 8 Severe 5/24/2017 190 5/30/2017 196 7
1002 9 Severe 6/9/2017 206 6/13/2017 210 5
;
proc sql ;
create table want as
select a.*
, max(b.start_date <= a.start_date and b.end_date >= a.end_date ) as Included
from sample a
left join sample b
on a.pt_id = b.pt_id and missing(b.event_id)
group by 1,2,3,4,5,6,7,8
order by a.pt_id, a.event_id, a.start_date , a.end_date
;
quit;

Data frames pandas python

I have a data frame that looks like this:
id age sallary
1 16 500
2 21 1000
3 25 3000
4 30 6000
5 40 25000
and a list of ids that I would like to ignore [1,3,5]
how can I get a data frame that will contain all the remaining rows: 2,4.
Big thanks for every one.
Call isin and negate the result using ~:
In [42]:
ignore_ids=[1,3,5]
df[~df.id.isin(ignore_ids)]
Out[42]:
id age sallary
1 2 21 1000
3 4 30 6000

Counting how many times a condition succeeds in SAS

I have a table A with real time values in it.
Amount Count Pct1 Pct2
300 2 0.000 100.000
1,891 2 0.001 100.000
500 2 0.000 100.000
100 2 0.000 100.000
1,350 2 0.001 100.000
2,648 2 0.001 100.000
2,255 2 0.001 100.000
500 2 0.000 100.000
200 2 0.000 30.441
10 2 0.000 100.000
1,928 2 0.001 100.000
40 2 0.000 100.000
200 2 0.000 100.000
256 2 0.000 100.000
254 2 0.000 100.000
100 2 0.001 100.000
50 1 0.000 33.333
1,512 2 0.001 100.000
I have a table B with a set of conditions. I want to generate the Condition success count in SAS. i.e. If I pass the row 1 in the below table as a condition to the table A it succeeds 2 times. I am using a join to generate a cartesin product and its not efficient. I want an efficient way to solve this problem (similar to what countifs function does in excel). Thanks a lot for your help.
Amount Count Pct1 Pct2 Condion Success Count
1,576 2 0 100 4
1,537 2 0 100 4
1,484 2 0 100 5
1,405 2 0 100 5
1,290 2 0 100 6
1,095 2 0 100 6
948 2 0 100 6
932 2 0 100 6
914 2 0 100 6
887 2 0 100 6
850 2 0 100 6
774 2 0 100 6
707 2 0 100 6
704 2 0 100 6
695 2 0 100 6
646 2 0 100 6
50 1 0 5.42 16
50 1 0 5.42 16
You said that you have tried join to make to make a cartesian product. However, since you didn't post any code I am not sure if you tried to make full product and then calculate the rows. Doing the counting in one SQL statement is much faster since actually full cartesian product is not written anywhere. Like this:
proc sql;
create table tableC as
select c.*, coalesce(s,0) as SuccessCount from TableB c
left join (
select id, count(*) as s from TableA a,TableB b
where
a.amount >= b.amount and
a.count >= b.count and
a.pct1 >= b.pct1 and
a.pct2 >= b.pct2
group by id
) as d
on c.id = d.id
;
quit;
Note that tableB needs to have some unique id column. You should always have some column to use as id but if you don't have it already simple create it like this for example:
data tableB;
set tableB;
id = _N_;
run;