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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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