How can I effectively utilize macro variables to handle date values in SAS Studio?
I normally use the DATE9. format for date macro variables. Either plain text, or as date constant.
%let date1=31DEC2022; /* plain text */
%let date2='31DEC2022'd; /* Date constant */
The way to use these macro variables is simple:
data test;
retain date1 "&date1"d; /* convert to date constant */
date2=&date2; /* already formatted as date constant */
datetime="&date1:00:00"dt; /* a datetime, not possible with the second format */
datetime2=dhms(&date2,0,0,0); /* but you can do it like this */
/* more statements */
run;
Note the double quotes, as macro expressions within single quotes are not resolved.
They can also be created using functions with %SYSFUNC, e.g.
%let date1=%sysfunc(today(),date9.);
%let date2="%sysfunc(today(),date9.)"d;
I much prefer to have my date macro variables in a readable format, rather than integers, as they are easier to understand and read, for instance in MPRINT output.
... View more