Hi,
I have a particular scenario where I wanted to get the reverse of group by function.
Here is the sample data
Table 1
medication | ndc | rx_count | total_qty |
Lipitor | 59148000813 | 1 | 10 |
Lipitor | 59148000813 | 1 | 10 |
Lipitor | 59148000813 | 1 | 10 |
Lipitor | 59148000813 | 1 | 10 |
Lipitor | 59148000813 | 1 | 10 |
Lipitor | 59148000813 | 1 | 10 |
Lipitor | 59148000813 | 1 | 10 |
Lipitor | 59148000813 | 1 | 10 |
Lipitor | 59148000813 | 1 | 10 |
Lipitor | 59148000813 | 1 | 10 |
Lipitor | 59148000813 | -1 | -10 |
Lipitor | 59148000813 | 1 | 10 |
If I do the summary using the below code
PROC SQL; |
CREATE TABLE WORK.QUERY_FOR_TESTTEST AS |
SELECT t1.medication, |
t1.ndc, |
(SUM(t1.rx_count)) FORMAT=BEST12. AS SUM_of_rx_count, |
(SUM(t1.total_qty)) FORMAT=BEST12. AS SUM_of_total_qty |
FROM WORK.TESTTEST AS t1 |
GROUP BY t1.medication, t1.ndc; |
QUIT; |
I get the result as below.
Table 2
medication | ndc | SUM_of_rx_count | SUM_of_total_qty |
Lipitor | 59148000813 | 10 | 100 |
Is there a way that I get the Table 1 as out put from table 2. Table 2 is the aggregated data, what I want is the extended as table 1.
What would be the query I should use?
Thanks,
+Sathyan
You want add this aggregated data at the bottom of Table1 ?
PROC SQL; |
CREATE TABLE WORK.QUERY_FOR_TESTTEST AS select * from WORK.TESTTEST union all corr |
SELECT t1.medication, |
t1.ndc, |
(SUM(t1.rx_count)) FORMAT=BEST12. AS SUM_of_rx_count, |
(SUM(t1.total_qty)) FORMAT=BEST12. AS SUM_of_total_qty |
FROM WORK.TESTTEST AS t1 |
GROUP BY t1.medication, t1.ndc; |
QUIT; |
I do not want to add the result at the bottom.
What I want is, how do we derive the table 1 detailed information from table 2 summary? I am OK if we do not get the negative values.
Just join the two tables using the group by columns as join key.
skallamp wrote:
javascript:void(0);I do not want to add the result at the bottom.
What I want is, how do we derive the table 1 detailed information from table 2 summary? I am OK if we do not get the negative values.
Possible only if "total_qty" is constant for all obs in one group and no negative values are present. With a least one negative total_qty/rx_count the number of "restored" observations won't match the number of obs in the original dataset.
data work.faked_original;
set work.QUERY_FOR_TESTTEST;
length rx_count total_qty i 8;
drop sum_of_: i;
do i = 1 to sum_of_rx_count;
rx_count = 1;
total_qty = sum_of_total_qty / sum_of_rx_count;
output;
end;
run;
Medication | NDC | RX Count | Total QTY |
Abilify 10mg Tablet | 24236027902 | 1 | 30 |
Abilify 10mg Tablet | 59148000813 | 257 | 2297 |
Abilify 15mg Tablet | 59148000913 | 19 | 217.5 |
Abilify 20mg Tablet | 59148001013 | 441 | 8145.5 |
Hi Andreas,
Thanks, that was helpful.
But that code will fail when the case is as above. Rx count need not be 1 always.
Let me explain. In line 2 (10mg Tablet) i need 257 identical lines as output, however Total qty in output should be 8.93 (i.e 2297 / 257)
How do we crack this?
Regards,
+Sathyan
You will have to set up a few more rules about how to do it. Once you allow that negative numbers could have been part of the unsummarized table, there are an infinite number of solutions to the problem.
Is that really what you want?
data have;
input @1 Medication $ @21 NDC $ @33 sRX_Count @37 sTotal_QTY;
datalines;
Abilify 10mg Tablet 24236027902 1 30
Abilify 10mg Tablet 59148000813 257 2297
Abilify 15mg Tablet 59148000913 19 217.5
Abilify 20mg Tablet 59148001013 441 8145.5
;
run;
data want(keep=Medication NDC RX_Count Total_QTY) ;
set have;
itx=int(sTotal_QTY/sRX_Count);
do count=1 to itx;
Total_QTY=sTotal_QTY/sRX_Count;
RX_Count=sRX_Count/itx;
output;
end;
run
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: