DATA Step, Macro, Functions and more

Reverse function of group by

Reply
Contributor
Posts: 32

Reverse function of group by

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

Super User
Posts: 10,041

Re: Reverse function of group by

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;
Contributor
Posts: 32

Re: Reverse function of group by

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.


Super User
Posts: 5,435

Re: Reverse function of group by

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

Data never sleeps
Super Contributor
Posts: 345

Re: Reverse function of group by

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;

Contributor
Posts: 32

Re: Reverse function of group by

Posted in reply to andreas_lds
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

Super User
Posts: 5,516

Re: Reverse function of group by

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.

Contributor
Posts: 45

Re: Reverse function of group by

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

Ask a Question
Discussion stats
  • 7 replies
  • 312 views
  • 0 likes
  • 6 in conversation