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

Hello,

 

I wanted to make a macro variable that is the maximum date of the variable (datevar) in my dataset (mydata). Something along the lines of the following:

 

%let max_dt = max(work.mydata.datevar);

 

 

Any recommendation would be much appreciated.

 

Best,

 

daszlosek

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Use PROC SQL to find the max and put it into the macro variable.

Depending on how you use it might want to format the value in different ways.

This should get a number that you can use to compare to other DATE variables.

proc sql noprint;
select max(datevar)
  into :max_dt trimmed
  from mydata
;
quit;

Or you could use something like this to get a string that you could use building a filename or title statement.

select max(datevar) format=yymmddn8.
  into :max_dt
  from mydata
;

Or you could generate the value in the form of a date literal.

select cats(quote(put(max(datevar),date9.)),'d')
  into :max_dt
  from mydata
;

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Use PROC SQL to find the max and put it into the macro variable.

Depending on how you use it might want to format the value in different ways.

This should get a number that you can use to compare to other DATE variables.

proc sql noprint;
select max(datevar)
  into :max_dt trimmed
  from mydata
;
quit;

Or you could use something like this to get a string that you could use building a filename or title statement.

select max(datevar) format=yymmddn8.
  into :max_dt
  from mydata
;

Or you could generate the value in the form of a date literal.

select cats(quote(put(max(datevar),date9.)),'d')
  into :max_dt
  from mydata
;

 

daszlosek
Quartz | Level 8

Worked like a charm! Have never used colons before.

 

Thank you!

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!

How to Concatenate Values

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.

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
  • 2 replies
  • 15584 views
  • 2 likes
  • 2 in conversation