DATA Step, Macro, Functions and more

%Let macro variable be max date in dataset

Accepted Solution Solved
Reply
Contributor
Posts: 67
Accepted Solution

%Let macro variable be max date in dataset

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


Accepted Solutions
Solution
‎06-28-2017 01:28 PM
Super User
Super User
Posts: 6,495

Re: %Let macro variable be max date in dataset

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


All Replies
Solution
‎06-28-2017 01:28 PM
Super User
Super User
Posts: 6,495

Re: %Let macro variable be max date in dataset

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
;

 

Contributor
Posts: 67

Re: %Let macro variable be max date in dataset

Worked like a charm! Have never used colons before.

 

Thank you!

☑ This topic is SOLVED.

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

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