BookmarkSubscribeRSS Feed
skallamp
Obsidian | Level 7

Hi,

I have a particular scenario where I wanted to get the reverse of group by function.

Here is the sample data

Table 1

medicationndcrx_counttotal_qty
Lipitor59148000813110
Lipitor59148000813110
Lipitor59148000813110
Lipitor59148000813110
Lipitor59148000813110
Lipitor59148000813110
Lipitor59148000813110
Lipitor59148000813110
Lipitor59148000813110
Lipitor59148000813110
Lipitor59148000813-1-10
Lipitor59148000813110

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

medicationndcSUM_of_rx_countSUM_of_total_qty
Lipitor5914800081310100

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

7 REPLIES 7
Ksharp
Super User

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;
skallamp
Obsidian | Level 7

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.


LinusH
Tourmaline | Level 20

Just join the two tables using the group by columns as join key.

Data never sleeps
andreas_lds
Jade | Level 19

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;

skallamp
Obsidian | Level 7
MedicationNDCRX CountTotal QTY
Abilify 10mg Tablet24236027902130
Abilify 10mg Tablet591480008132572297
Abilify 15mg Tablet5914800091319217.5
Abilify 20mg Tablet591480010134418145.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

Astounding
PROC Star

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.

joehinson
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1225 views
  • 0 likes
  • 6 in conversation