Hi.
Is there a data step that will help me determine the very last date for each patient below?
The table shows 4 patients each with dates under each olumn. I’m trying to write code that will look at
each of the columns (presc1-4) to determine the very last date and tell me the column header the date falls under?
example below:
PATIENT | PRESC1 | PRESC2 | PRESC3 | PRESC4 |
D51841 | 5/16/2013 | 5/10/2012 | 6/1/2013 | 1/10/2000 |
D255 | 8/1/2011 | 3/5/2010 | 8/1/2011 | 5/6/2011 |
F27898 | 9/16/2000 | 3/6/1995 | 2/20/2014 | 7/1/1995 |
Desired results:
PATIENT | date | type |
D51841 | 1/10/2000 | presc4 |
D255 | 8/1/2011 | presc1 & presc3 |
F27898 | 2/20/2014 | presc3 |
If you can have multiples it's a little more difficult but not too difficult.
Use an array with MAX(). Dates are stored as numbers, so the largest date is the largest number and the MAX function will tell you which date is the largest. VNAME() will return the name of the variable.
Untested:
data want;
set have;
length var_name $100.;
array pr(*) presc1-presc4;
max_date = max(of pr(*));
do i=1 to dim(pr);
if pr(i)=max_date then var_name = catx(" & ", var_name, vname(pr(i)));
end;
run;
If you can have multiples it's a little more difficult but not too difficult.
Use an array with MAX(). Dates are stored as numbers, so the largest date is the largest number and the MAX function will tell you which date is the largest. VNAME() will return the name of the variable.
Untested:
data want;
set have;
length var_name $100.;
array pr(*) presc1-presc4;
max_date = max(of pr(*));
do i=1 to dim(pr);
if pr(i)=max_date then var_name = catx(" & ", var_name, vname(pr(i)));
end;
run;
Ohh thank you so much Mr. Grand Advisor!!! It even produced results where dates were the same in different columns. I can flag those for further work. thanks again!! This SAS Support Cummunity bulletin is really the BEST
Ms. Grand Advisor
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.