How can I convert the following text date stored in a macro variable into a sas date?
%let dt=September 16, 2005;
%put &dt;
For macro variables, I suggest using text in the form of ddMMMyyyy, without any quotes.
I.e, something like
%let d=05JUL2021 ;
This tactic makes it easy to use macrovar-based dates in regular SAS code, as in:
data want;
set have;
where date >="&d"d;
run;
I.e., in the SAS code, just provide the trailing d, and the quotes - but they have to be double quotes to avoid masking the &d value.
Similarly for time, use
%let t=15:05:20.05
And for date-times
%let dt=03jul2021:12:20:30.10;
whose use in a sas program might look like:
%let d=05jul2021;
%let t=15:30:15.05;
%let dt=03jul2021:12:20:30.10;
data _null_;
d="&d"d; put d=date9.;
t="&t"t; put t=time11.2;
dt="&dt"dt; put dt=datetime30.3;
run;
How do you set the macro variable initially?
I initially read it in as a parameter with the %quote() around it. Similar to
%macro xyz(dt);
....
%mend;
%xyz(%quote(September 16, 2005))
Where do you want to use this new "date" value? As another macro variable? Variable in a data step or proc sql?
General solution without details is going to involve an Input function with a proper informat as the possibly cleanest solution. But which and where can bring up some details.
@Kurt_Bremser is hinting, I think, that when you make it may be the place to create the new value.
I intend to use it as a date filter in a data step. I know I can convert the date by hand to someone more user friendly, but was hoping SAS could do it if it doesn't require really elaborate code.
For macro variables, I suggest using text in the form of ddMMMyyyy, without any quotes.
I.e, something like
%let d=05JUL2021 ;
This tactic makes it easy to use macrovar-based dates in regular SAS code, as in:
data want;
set have;
where date >="&d"d;
run;
I.e., in the SAS code, just provide the trailing d, and the quotes - but they have to be double quotes to avoid masking the &d value.
Similarly for time, use
%let t=15:05:20.05
And for date-times
%let dt=03jul2021:12:20:30.10;
whose use in a sas program might look like:
%let d=05jul2021;
%let t=15:30:15.05;
%let dt=03jul2021:12:20:30.10;
data _null_;
d="&d"d; put d=date9.;
t="&t"t; put t=time11.2;
dt="&dt"dt; put dt=datetime30.3;
run;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: