BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SannaSanna
Quartz | Level 8

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
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

3 REPLIES 3
Reeza
Super User

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;

SannaSanna
Quartz | Level 8

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

Reeza
Super User

Ms. Grand Advisor Cat Happy

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2955 views
  • 3 likes
  • 2 in conversation