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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 9834 views
  • 6 likes
  • 5 in conversation