LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (September 2008, week 3)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Fri, 19 Sep 2008 08:54:26 -0400
Reply-To:   Muthia Kachirayan <muthia.kachirayan@GMAIL.COM>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   Muthia Kachirayan <muthia.kachirayan@GMAIL.COM>
Subject:   Re: sum variables for each day
Comments:   To: Jennifer Rose <jrose01@wesleyan.edu>
In-Reply-To:   <200809181619.m8IAkr8J004403@malibu.cc.uga.edu>
Content-Type:   text/plain; charset=ISO-8859-1

On Thu, Sep 18, 2008 at 12:19 PM, Jennifer Rose <jrose01@wesleyan.edu>wrote:

> Hi, > > I'm working with a data set that has drug use data for several drugs > reported on several days. What I need to do is to create a variable that > sums up the number of drugs used on each day. For example, for Day 1, I > need > to sum the variables drg1_day01, drg2_day01, drg3_day01, drg4_day01. > > It is simple enough to create a variable using the code: > > sumdrg_day01=sum(of drg1_day01,drg2_day01,drg3_day01,drg4_day01); > > The problem is that I need do this for 180 days, so I have to create 180 > variables. There has to be a better way than writing 180 lines of code, but > I don't know how to do it. I would really appreciate it if anyone could > help > me on this. > > Thanks! > Jen Rose >

Jen,

With the current data structure one data step is sufficient to find the sums by day. Also there is no need to write 180 lines of code(statements).

Borrowing example data of Nat, it can be seen that there are 4 drugs and 2 days leading to 8 variables for appropriate sums.

Let I run from 1, 8. If I be converted to k taking values 1 or 2 to represent the day, then sum can be put into the fitting cells.

Consider

i = 1 to 8 and

k = int( ( i - 1) / 4) + 1.

When i = 1, 2, 3, 4 then k = 1

and similarly when i = 5 to 8, k = 2.

This helps to keep the data horizontally and do summation using array.

Data Jen1; input id drg1_day01 drg2_day01 drg3_day01 drg4_day01 drg1_day02 drg2_day02 drg3_day02 drg4_day02; cards; 101 1 2 3 4 88 99 77 22 201 33 22 11 44 9 8 7 6 run;

data need; set Jen1; array sum_d sum_d1 - sum_d2; array drg[*] drg1_day01 -- drg4_day02; do i = 1 to dim(drg); k = int((i - 1)/4) + 1; sum_d[k]= sum(sum_d[k] , drg[i]); end; keep id sum:; run;

Output :

Obs id sum_d1 sum_d2 1 101 10 286 2 201 110 30

For 180 days, the array for sum has to be declared as:

array sum_d sum_d1 - sum_d180 ;

No pains to enter 180 statements.

If the number of drugs are N instead of 4 then replace 4 by N in the computation of K and the array DRG[ ] as

array drg[*] drg1_day01 - drgN-day180;

Macro variables for number of days and drugs would be handy for a general case.

Regards,

Muthia Kachirayan


Back to: Top of message | Previous page | Main SAS-L page