|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|
|Content-Type: ||text/plain; charset=ISO-8859-1|
On Thu, Sep 18, 2008 at 12:19 PM, Jennifer Rose <email@example.com>wrote:
> 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
> 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
> me on this.
> Jen Rose
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.
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.
input id drg1_day01 drg2_day01 drg3_day01 drg4_day01 drg1_day02 drg2_day02
101 1 2 3 4 88 99 77 22
201 33 22 11 44 9 8 7 6
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]);
keep id sum:;
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