I have a dataset that looks like the following for multiple patients. I am trying to subtract each visit value from the baseline value of corresponding variables (which are sometimes missing).
Data Have:
Patient Variable Value Visit
A Height 100 Baseline
A Weight 50 Baseline
A HDCIRC 30 Baseline
A BMI 50 Baseline
A Height 100 a
A Weight 50 a
A HDCIRC 30 a
A BMI 50 a
A Height 100 b
A Weight 50 b
Data Want:
Patient Variable Value Visit BASELINE Change
A Height 100 Baseline 100 0
A Weight 50 Baseline 50 0
A HDCIRC 30 Baseline 30 0
A BMI 50 Baseline 50 0
A Height 120 a 100 20
A Weight 50 a 50 0
A HDCIRC 30 a 30 0
A BMI 34.7 a 50 -15.3
A Height 150 b 100 50
A Weight 51 b 50 1
My attempt would be to first create BASELINE and then calculate the change. In order to get BASELINE, I've seen some people use a lag or a dif function. How can I correctly create the BASELINE variable?
proc sort data=have;
by patient visit;
;
data want;
set have;
by patient visit;
difstamp = dif(visit);
if first.patient then do;
dif=0;
end;
else dif=difstamp;
drop difstamp;
run;
proc sort data=want;
by timestamp;
run;
As alternative you could simply merge have with itself
data have;
input Patient $ Variable $ Value Visit $;
cards;
A Height 100 Baseline
A Weight 50 Baseline
A HDCIRC 30 Baseline
A BMI 50 Baseline
A Height 120 a
A Weight 50 a
A HDCIRC 30 a
A BMI 34.7 a
A Height 150 b
A Weight 51 b
;
proc sort;
by patient variable;
run;
data want;
merge have have(where=(__visit='Baseline') keep=patient variable value visit rename=(visit=__visit value=BASELINE))
;
by patient variable;
Change=Value-BASELINE;
drop __:;
run;