BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Melk
Lapis Lazuli | Level 10

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?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

View solution in original post

5 REPLIES 5
ballardw
Super User

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.

 

art297
Opal | Level 21

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

 

Melk
Lapis Lazuli | Level 10

Thank you both! LET statement was a typo, both worked beautifully.

Reeza
Super User

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;
novinosrin
Tourmaline | Level 20

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;

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 10273 views
  • 6 likes
  • 5 in conversation