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

Hello, 

 

I need to create a variable (withdrawal_date) based off of the maximum date value for each participant id. I put in some fake data into this dataset to test some SAS codes to find a way to give me the maximum value across multiple variables, and then I need to make that value be the value of the created withdrawal_date variable. I found a macro to output the maximum variable, but it just gives me the variable name that has the max value. I am trying to figure out a way to code it so that the maximum value (not variable name) is output as the new variable. 

 

Raw "fake" data:

id session1 session2 session3 session4
101/23/201902/01/201904/05/201906/01/2019
205/25/201907/02/2019..
304/07/201906/07/201909/09/2019.
410/02/201910/28/201911/05/2019.
503/04/2019...
606/07/201907/04/201908/17/201910/01/2019
703/06/201903/27/2019..
 
 
Macro: 

%macro test(dsn,vars,func);
data new;
set &dsn;
array list(*) &vars;
&func = vname(list[whichn(&func(of list[*]), of list[*])]);
run;
%mend test;

/** retrieve maximum value from a b and c **/
%test(fake,session1 session2 session3 session4 session5,max)

proc print;
run;

/** retrieve maximum value from all numeric variables **/
%test(fake,_numeric_,max)

proc print;
run;

 

Output from macro:

id  session1 session2 session3 session4 max
101/23/201902/01/201904/05/201906/01/2019session4
205/25/201907/02/2019..session2
304/07/201906/07/201909/09/2019.session3
410/02/201910/28/201911/05/2019.session3
503/04/2019...session1
606/07/201907/04/201908/17/201910/01/2019session4
703/06/201903/27/2019..session2

 

Desired output:

id session1 session2 session3 session4 withdrawal_date
101/23/201902/01/201904/05/201906/01/201906/01/2019
205/25/201907/02/2019..07/02/2019
304/07/201906/07/201909/09/2019.09/09/2019
410/02/201910/28/201911/05/2019.11/05/2019
503/04/2019...03/04/2019 
606/07/201907/04/201908/17/201910/01/201910/01/2019
703/06/201903/27/2019..03/27/2019

 

Thanks in advance!

 
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
withdrawal_date = max(of session1-session4);

That's all you should need. No macro needed anywhere.

View solution in original post

2 REPLIES 2
Reeza
Super User
withdrawal_date = max(of session1-session4);

That's all you should need. No macro needed anywhere.
fordcr2
Obsidian | Level 7

Well that was easy Smiley Very Happy Thank you!! 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 473 views
  • 0 likes
  • 2 in conversation