Date: Wed, 31 Aug 2011 14:42:17 -0400
Reply-To: Tom Abernathy <tom.abernathy@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Tom Abernathy <tom.abernathy@GMAIL.COM>
Subject: Re: How to compute difference between observations in
longitudinal data format with marco
The data step solution is to use arrays and FIRST./LAST. processing.
data want;
array orig var1-var30;
array base (30);
array final (30);
array diff (30);
do until (last.id);
set have;
by id visit;
if first.visit then do i=1 to 30;
base(i)=orig(i);
end;
end;
if not first.visit then do i=1 to 30;
final(i) = orig(i);
end;
do i=1 to 30;
diff(i) = final(i) - orig(i);
end;
run;
If you just want the differences then you can get them using PROC COMPARE
also. Here is an example constructed from the SASHELP.CLASS dataset with a
little random noise added to the WEIGHT variable. Requires that you can
identify the last visit for each subject.
data sample;
do visit=1 to 3 ;
do p=1 to nobs;
set sashelp.class point=p nobs=nobs;
weight = weight + 10*ranuni(0);
output;
end;
end;
stop;
run;
proc compare
data=sample(where=(visit=1))
compare=sample(where=(visit=3))
outdiff out=diff ;
id name ;
var weight height ;
run;
proc print data=diff; run;
On Wed, 31 Aug 2011 09:55:29 -0700, jn mao <jn_mao@YAHOO.COM> wrote:
>Hi SAS-Ls,
>
>I have over 30 variables with 5 follow-up data. I need to compute the
difference between the 1st visit and the last visit. Can someone help me it
in SAS with macro?
>Here are my code, but seems I can't use if and then in macro.
>%macro prepo;
>%do i=1 %to 40;
>IF FIRST.visit THEN DO;
>fac&i=base&i;
>END;
>IF LAST.visit THEN DO;
>DIFF_fac&i = fac&i - base&i;
>OUTPUT;
>END;
>proc univariate ;
>class group;
>var diff_fac&i;
>%end;
>%mend;
>
>Can someone help me out? Thanks much!
>
>Jane