Hi,
I am trying to understand NADIR concept. I think NADIR is lowest overserved value up to a given time point and we need to use retain instead of min function to ensure time dependent accuracy and update progressively, preserving the visit sequence. But In below dataset nadir should be 40 at Week_1 for subject 101 but I got 50 by using below provided code. Is it 40 or 50 at Week_1 and if it is 50 why? I have tried 2 different codes and I am talking about want1 program. Let me know if there are any mistake in both programs.
DATA have;
INPUT usubjid $1-3 avisit $5-13 adt $14-23 trtsdt $24-33 aval postbfl avisitn base;
DATALINES;
101 Baseline 01JAN2024 05JAN2024 50 0 0 50
101 Week_1 08JAN2024 05JAN2024 40 1 1 50
101 Week_2 15JAN2024 05JAN2024 60 1 2 50
101 Week_3 22JAN2024 05JAN2024 30 1 3 50
101 Week_4 29JAN2024 05JAN2024 35 1 4 50
102 Baseline 02FEB2024 07FEB2024 70 0 0 70
102 Week_1 09FEB2024 07FEB2024 65 1 1 70
102 Week_2 16FEB2024 07FEB2024 80 1 2 70
102 Week_3 23FEB2024 07FEB2024 60 1 3 70
102 Week_4 01MAR2024 07FEB2024 75 1 4 70
;
RUN;
proc sort; by usubjid postbfl avisitn; run;
data want1;
retain lag_aval nadir;
set have;
by usubjid postbfl;
if first.usubjid then nadir=.;
lag_aval=lag(aval);
if postbfl then do;
if first.postbfl then nadir=base;
else nadir=min(nadir,lag_aval);
end;
chg=aval-nadir;
pchgndr=chg/nadir*100;
run;
Data want2;
Retain nadir prevval;
Set have;
By usubjid avisitn aval;
If first.usubjid then do;
Prevval = aval;
Nadir = aval;
End;
Else do;
If prevval ne . then nadir = min(prevval,nadir);
Else if prevval = . then nadir =aval;
Prevval = aval;
End;
chg=aval-nadir;
If nmiss(aval, nadir) = 0 and nadir > 0 then pchgndr = round((aval-nadir)/nadir * 100, 0.1);
Run;
Thanks,
Chinna
I presume that, for each week, you want nadir to report the lowest weekly value of AVAL up through the week in hand. If so, then you can use the "retain nadir" statement:
data want;
set have;
by usubjid;
retain nadir;
if first.usubjid then nadir=aval;
else nadir=min(aval,nadir);
run;
As each new observation is read, nadir has the lowest aval through all preceding weeks, which then is compared to the current aval. Of course, for the beginning of each usubjid, nadir is set to aval.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.