BookmarkSubscribeRSS Feed
nickspencer
Obsidian | Level 7
Hello everyone,
Is it correct to use date constant in %let statement? I will be using this value only to filter the data in a datastep later on.

%let mydate=‘01jan2021’d;

data test1;
set test ;
If dt=&mydate;
Run;

Please let me know your thoughts.

Thanks,
Nick
3 REPLIES 3
Tom
Super User Tom
Super User

That will work.

 

The question to ask is whether once the macro processor has finished converting the text do you have valid SAS syntax.  You can test it be replacing the macro variable reference with the text the macro variable contains.  (You should do this in your head as you are writing SAS code that uses macro variable references.)

ballardw
Super User

There are style choices involved.

Your code should work after you fix the curly quotes in your %let statement, with a simple example like this.

%let mydate=01jan2021d;

You must use simple ' quote marks.

 

I, again I will say as a style choice, dislike quotes in macro variables I create with %Let for dates and would use in this case:

%let mydate=01jan2021;

data test1;
   set test ;
   If dt= "&mydate."d;
Run;

Using the date literal version in the IF statement makes it clear that the intended value that MYDATE should contain is a date literal. So debugging may be easier.  Plus I could then use Mydate in something like a title statement: Title "This report was generated for &mydate.";

or labels for a variable : label thisvar = "Measured as of &mydate.";

or in a filename: ods rtf file="<path>/reportfile &mydate..rtf";

 

 

 

LeonidBatkhan
Lapis Lazuli | Level 10

Hi nickspencer,

 

You can also use the following code to generate SAS internal date value:

 

%let mydate = %sysfunc(inputn(01jan2021,date9.));

The value of &mydate will be an internal SAS date value 22281 (equal to the number of days from 01jan1960).

Then you can refer to it in your code as &mydate without quotes and letter d at the end.
Hope this helps.

 

 

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
  • 3 replies
  • 1714 views
  • 2 likes
  • 4 in conversation