0

Every Subject has a baseline. Once the difference between the value and the baseline exceeds 5, that value becomes the baseline for all future comparisons until another value exceeds this new baseline by 5.

This is what I want the output data to look like: enter image description here

This is what I'm getting enter image description here

This is my current code, which gets me as close as anything I've tried. I've tried different combinations of retain, lag(), and ifn (suggested in this post)

Data Have;
 Input Visit usubjid Baseline Value;
 datalines;
1 1 112.2 112.2
2 1 112.2 113.7
3 1 112.2 112
3 1 112.2 108
4 1 112.2 109
5 1 112.2 107
7 1 112.2 106
8 1 112.2 107
; 
run;

proc sort;by usubjid;run;

data want;
 Length chg $71;
 retain chg;
 set Have;
 length prevchg $71;
 by usubjid;
 prevchg=chg;
 if first.usubjid then do; prevchg=''; end;
 baseline=ifn(prevchg in ('Increase >= 5mm New', "Decrease >= 5mm"),lag(value),lag(baseline));

 diff = value-baseline;
 if visit > 1 then do;
  if diff > 5 then do; chg='Increase >= 5mm New'; order = 3; end;
  else if diff < -5 then do; chg = 'Decrease >= 5mm'; order = 6; end;
  else if -5 <= diff <= 5 then do; 
    if prevchg in('Increase >= 5mm New', 'Increase > 5mm Persistent') then do; chg ='Increase > 5mm Persistent'; order = 4; end;
    else do; chg = 'No Change (change >= -5 and <= 5mm)'; order = 5; end;
  end;
 end;
run;

Right now the code will correctly update the baseline to the previous value for the next visit, but then goes right back to the original baseline. I'm confident this has something to do with the way Lag() and Retain work with if/then, but I cannot figure out the solution. here is an example of the issue:

Kevin.C
  • 309
  • 1
  • 2
  • 10
  • You did not define the variable chg? Is it numeric or character? If the later how long should it be? If it is already on the input then retaining it will not help since the SET will overwrite the retained value. Is BASELINE already on the input or not? Your initial listing makes it look like it is, but then you are trying to change it. – Tom Apr 20 '22 at 02:32
  • How come the values in the photograph of the output do not match the values in the input you posted as actual text? Please pose input and outputs that agree and post both as text, preferably as data step code. Also you mention a difference greater than 5 but you seem to be using a difference of less then -5 instead. Which is it? – Tom Apr 20 '22 at 02:41
  • I've updated the post and code to be more clear. Baseline is already in the dataset, which as another user has just pointed out, could be the issue. – Kevin.C Apr 20 '22 at 03:11

1 Answers1

1

You should be able to do this easily. The BASELINE variable CANNOT be on the input if you want to RETAIN its value.

data want ;
  set have ;
  by usubjid;
  retain baseline;
  if first.usubjid then baseline=value;
  difference = baseline - value;
  output;
  if difference > 5 then baseline=value;
run;
Tom
  • 47,574
  • 2
  • 16
  • 29