Hello,
I have a large dataset, from which I want to extract a subset based on a datetime variable.
So far, I've managed to obtain the SAS datetime that I want as a macro variable. For instance, I want the SAS datetime for 2019-04-01, 00:00:00:000, and I've tried the following to save the datetime as a macro variable called "dtime":
data save;
a='01APR2019, 00:00:00.000'dt;
run;
data _null_;
set save;
call symput("dtime", a);
run;
Question: Because I will have to repeat the codes above many times to subset based on multiple different dates, I am wondering if I can create a macro for the date part of the datetime. I tried the following to add a macro variable called "date" that has the date of interest:
%let date=01APR2019;
data save;
a='&date, 00:00:00.000'dt;
run;
data _null_;
set save;
call symput("dtime", a);
run;
Nonetheless, the &date part is not evaluated in the data step. So I wonder if there's way to let a = '01APR2019, 00:00:00.000'dt in the data step with the help of this macro variable "date".
First time posting, so I apologize if my question was not clearly stated.
You need to enclose macro variables in double-quotes if you are going to use a macro variable.
%let date=01APR2019;
data _null_;
a="&date 00:00:00.000"dt;
call symput('dtime',a);
run;
Please note proper terminology for clarity: this is not a macro as your question implies, this is a macro variable, which is different.
Even easier:
%let dtime=%sysfunc(dhms("&date"d,0,0,0));
You need to enclose macro variables in double-quotes if you are going to use a macro variable.
%let date=01APR2019;
data _null_;
a="&date 00:00:00.000"dt;
call symput('dtime',a);
run;
Please note proper terminology for clarity: this is not a macro as your question implies, this is a macro variable, which is different.
Even easier:
%let dtime=%sysfunc(dhms("&date"d,0,0,0));
Thank you so much! I didn't realize there is a short-cut (albeit a somewhat more complex one) that gets me the same result. Thanks for helping me out!
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.