BookmarkSubscribeRSS Feed
anissak1
Obsidian | Level 7

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;

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

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;

 

 

anissak1
Obsidian | Level 7

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;

anissak1
Obsidian | Level 7

I had to look up definitions - and think that I was both trolling and trawling...
Desperate times, desperate measures. 🙂

 

Kurt_Bremser
Super User

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.

CFC_SAS_Communities_400x225.jpgCFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2879 views
  • 2 likes
  • 3 in conversation