Hello - I have created a simple clinical dataset in long form with subjects, day of measurement, and measurement values. I am trying to create a variable indicating percent change from the baseline (first) visit for each subsequent visit. Based on trolling the boards, I came up with the following, but this only gives me the change between time intervals and when I try to adapt it to measure each study measurement compared to baseline, I can't seem to avoid divide by zero errors since the baseline (first observation) will of course not have a change.
I have attached the data I have and the data I want. 🙂 In the end, I only need one row per USUBJID, not the broad form that the code below gives me.
Any recommendations are very much appreciated. Thank you!
Anissa
data walk;
set msoac.t25changeshort;
by usubjid notsorted;
prevwalk=lag(walktime);
if not first.usubjid then percentchange=(walktime-prevwalk)/prevwalk;
if percentchange>.2 then changed=1;
else changed=0;
run;
I hope you are trawling these pages rather than trolling.
You can avoid a division by zero by using the divide function.
Something like this?
data WALK;
retain BASELINE;
set MSOAC.T25CHANGESHORT;
by USUBJID notsorted;
if first.USUBJID then BASELINE=WALKTIME;
else PERCENTCHANGE=(WALKTIME-BASELINE)/BASELINE;
CHANGED = ( PERCENTCHANGE > .2 );
run;
Thank you @ChrisNZ and @Kurt_Bremser ! My code is clunky, but built off of yours and seems to work, and I think I am set for the moment!! Much appreciation.
Best wishes.
Anissa
data want;
set msoac.clean25ave;
retain BASELINE;
set MSOAC.clean25ave;
by USUBJID notsorted;
if first.USUBJID then BASELINE=WALKTIME;
else PERCENTCHANGE=(WALKTIME-BASELINE)/BASELINE;
CHANGED = ( PERCENTCHANGE > .2 );
run;
data want2;
set want;
if changed=1;
run;
proc sort data=want2;
by usubjid studyday;
run;
data want3;
set want2;
by usubjid;
changeday=studyday;
drop studyday walktime changed;
if first.usubjid then output want3;
run;
I had to look up definitions - and think that I was both trolling and trawling...
Desperate times, desperate measures. 🙂
Since you want to compare with the first visit of a subject, and not with the previous, you need to retain a variable:
data want;
set myfold.t25changeshort;
by usubjid;
retain baseline;
if first.usubjid
then do;
baseline = walktime;
percentchange = 0;
changed = 0;
end;
else do;
percentchange = (walktime - baseline) / baseline;
changed = (abs(percentchange) > .02);
end;
drop baseline;
run;
If you only want to show increases as changed, remove the abs() function.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.