DATA Step, Macro, Functions and more

Create a date variable from macro

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 76
Accepted Solution

Create a date variable from macro

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?


Accepted Solutions
Solution
‎08-01-2017 03:07 PM
Super User
Posts: 11,343

Re: Create a date variable from macro

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


All Replies
Solution
‎08-01-2017 03:07 PM
Super User
Posts: 11,343

Re: Create a date variable from macro

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.

 

PROC Star
Posts: 7,471

Re: Create a date variable from macro

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

 

Frequent Contributor
Posts: 76

Re: Create a date variable from macro

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

Super User
Posts: 19,789

Re: Create a date variable from macro

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;
PROC Star
Posts: 283

Re: Create a date variable from macro

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;

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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