Date: Thu, 18 Sep 2008 12:48:20 -0500
Reply-To: Joe Matise <snoopy369@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Joe Matise <snoopy369@GMAIL.COM>
Subject: Re: sum variables for each day
In-Reply-To: <200809181619.m8IAkr8J004403@malibu.cc.uga.edu>
Content-Type: text/plain; charset=ISO-8859-1
Lots of solutions already out there, here's a macro solution:
%macro sum_drugs(i);
sumdrg_day&i = sum(of drg1_day&i, drg2_day&i, drg3_day&i, drg4_day&i);
%mend;
%macro sum_alldrugs;
*I split up the one digit, two digit, and three digit ones since you seem to
want zero-led variables- if that is unneeded then do this all in a row;
%do i = 1 to 9;
sum_drugs(00&i);
%end;
%do i = 10 to 99;
sum_drugs(0&i);
%end;
%do i = 100 to 180;
sum_drugs(&i);
%end;
%mend;
data want;
set have;
%sum_alldrugs;
run;
As with the other posters, though, I would suggest that having this in a
single row is a bad, bad idea... your table structure is currently (if I
understand correctly)
TABLE:
user_1 drg1_day1 drg1_day2 drg1_day3 drg2_day1 drg2_day2 drg2_day3
user_2 drg1_day1 drg1_day2 drg1_day3 drg2_day1 drg2_day2 drg2_day3
etc.
It should be
user_1 drg1_day1 drg2_day1 drg3_day1
user_1 drg1_day2 drg2_day2 drg3_day2
user_1 drg1_day3 drg2_day3 drg3_day3
user_2 drg1_day1 drg2_day1 drg3_day1
user_2 drg1_day2 drg2_day2 drg3_day2
user_2 drg1_day3 drg2_day3 drg3_day3
etc.
Then you can use a simple proc freq (or means or tabulate or ...) to
determine what you want quickly and efficiently.
-Joe
On Thu, Sep 18, 2008 at 11:19 AM, 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
>
|