I want to create a date variable in my program from a macro %LET statement.
%LET date9 01AUG2017;
data new;
set old;
date = &date9;
run;
I cant get this to work, I dont quite understand how the date9 is stored in SAS I think - adding a format statement also doesnt work. What am I missing here?
Try
data new; set old; date = "&date9"d; format date date9.; run;
You were attempting to use a "date literal" value but missing that in basic syntax the way to use a data literal is "01JAN2017"d; The d at the end, no space after the quote, tells SAS to use this a date. Date literal must be in the form of ddMONyy or ddMONyyyy where dd is day of month, MON is three-letter abbreviation and yy or yyyy is either a 2 or 4 digit year. I recomment using the 4-digit to remove any confusion.
Try
data new; set old; date = "&date9"d; format date date9.; run;
You were attempting to use a "date literal" value but missing that in basic syntax the way to use a data literal is "01JAN2017"d; The d at the end, no space after the quote, tells SAS to use this a date. Date literal must be in the form of ddMONyy or ddMONyyyy where dd is day of month, MON is three-letter abbreviation and yy or yyyy is either a 2 or 4 digit year. I recomment using the 4-digit to remove any confusion.
Same suggestion as @ballardw, but you'll also have to correct your let statement:
%LET date9=01AUG2017; data old; input x y; cards; 1 2 3 4 ; data new; set old; format date date9.; date = input("&date9",date9.); run;
Art, CEO, AnalystFinder.com
Thank you both! LET statement was a typo, both worked beautifully.
And if you want today, you can use DATE() or TODAY()
*Create a macro variable;
%let myDate = %sysfunc(today(), date9.);
*Display macro variable for testing;
%put &myDate;
Hi, I noticed you have missed out an = sign in a %let statement. %let is a macro assignment statement just like a variable assignment statement in a datastep.
Secondly you can copy paste @ballardw solution
Correction:
%LET date9=01AUG2017;
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 25. 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.