Hello,
What is the way to create a SAS date macro variable with d at the end?
The resulted value in this case should be '21feb2020'd (one day after max date in ttt data set)
Data ttt;
format date date9.;
input date :date9.;
cards;
21feb2020
31dec2020
04Mar2021
19mar2021
28Jun2021
;
Run;
PROC SQL noprint;
select intnx('day',max(date),1) into: start_date
from ttt
;
QUIT;
%put &start_date;
It is not really necessary to use INTNX() to add days since SAS stores dates as number of days. Just use normal arithmetic.
The QUOTE() function can be used to add quotes.
data ttt;
format date date9.;
input date :date9.;
cards;
21feb2020
31dec2020
04Mar2021
19mar2021
28Jun2021
;
proc sql noprint;
select max(date)+1
, cats(quote(put(max(date)+1,date9.)),'d')
into :start_date trimmed
, :start_date_human
from ttt
;
quit;
%put &=start_date &=start_date_human ;
START_DATE=22460 START_DATE_HUMAN="29JUN2021"d
Note that both forms refer to the same day and can be used it most of the same places. The only restriction is you cannot use the human readable date literal with the %EVAL() macro function. You would need to use %SYSEVALF() if you want the macro process to see that string as a date.
Your solution is already ideal: you store the raw numeric value in the macro variable, which is the most easiest to handle in the following code.
See Maxim 28.
@Ronein wrote:
Hello,
What is the way to create a SAS date macro variable with d at the end?
The resulted value in this case should be '
21feb2020'd (one day after max date in ttt data set)
Don't create macro variables like this, with quotes and a D on the end. Macro variables should not be formatted or human readable (unless they are needed for titles or labels or filenames, in which case they still don't need the quotes or a D on the end)
It is not really necessary to use INTNX() to add days since SAS stores dates as number of days. Just use normal arithmetic.
The QUOTE() function can be used to add quotes.
data ttt;
format date date9.;
input date :date9.;
cards;
21feb2020
31dec2020
04Mar2021
19mar2021
28Jun2021
;
proc sql noprint;
select max(date)+1
, cats(quote(put(max(date)+1,date9.)),'d')
into :start_date trimmed
, :start_date_human
from ttt
;
quit;
%put &=start_date &=start_date_human ;
START_DATE=22460 START_DATE_HUMAN="29JUN2021"d
Note that both forms refer to the same day and can be used it most of the same places. The only restriction is you cannot use the human readable date literal with the %EVAL() macro function. You would need to use %SYSEVALF() if you want the macro process to see that string as a date.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.