Help using Base SAS procedures

last date from multiple columns

Accepted Solution Solved
Reply
Contributor
Posts: 57
Accepted Solution

last date from multiple columns

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

Accepted Solutions
Solution
‎02-23-2017 06:40 PM
Super User
Posts: 17,750

Re: last date from multiple columns

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


All Replies
Solution
‎02-23-2017 06:40 PM
Super User
Posts: 17,750

Re: last date from multiple columns

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;

Contributor
Posts: 57

Re: last date from multiple columns

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

Super User
Posts: 17,750

Re: last date from multiple columns

Ms. Grand Advisor Cat Happy

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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