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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.