BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
EIrvin
Fluorite | Level 6

For the life of me I cannot remember how to do this.

 

I have the following %let statement(s), which work beautifully when setting them in my where statements:

*CURRENT DATE*/

%LET CD=INTNX('DAY',TODAY(),-1);

/*CURRENT MONTH START DATE*/

%LET CM=INTNX('MONTH',INTNX('MONTH',TODAY(),1),-1);

/*CURRENT YEAR START DATE*/

%LET CY=INTNX('YEAR',INTNX('YEAR',&CD,1),-1);

 

However, I cannot remember how in the world to validate the dates (results shown in the log) with the %put statement.  Any one out there have any ideas?

 

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Erm, thats obfuscation.  We have a function called today() which everyone knows, and you are wrapping that into macro variables, why?  You can just use the function directly in code, for example this:

%LET CD=INTNX('DAY',TODAY(),-1);

data want;
  set have;
  where date=&cd.;
run;

Is the same (but harder to debug) as:

data want;
  set have;
  where date=today()-1;
run;

As for putting that information into the log, you first need to actually run the code held in the macro varaibles, as they are just text, so maybe something like:

data _null_;
  format cd cm cy date9.;
  cd=&cd.;
  cm=&cm.;
  cy=&cy.;
  put _all_;
run;

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Erm, thats obfuscation.  We have a function called today() which everyone knows, and you are wrapping that into macro variables, why?  You can just use the function directly in code, for example this:

%LET CD=INTNX('DAY',TODAY(),-1);

data want;
  set have;
  where date=&cd.;
run;

Is the same (but harder to debug) as:

data want;
  set have;
  where date=today()-1;
run;

As for putting that information into the log, you first need to actually run the code held in the macro varaibles, as they are just text, so maybe something like:

data _null_;
  format cd cm cy date9.;
  cd=&cd.;
  cm=&cm.;
  cy=&cy.;
  put _all_;
run;
Tom
Super User Tom
Super User

You are just assigning the CODE to calculate dates to the macro variables. You are not actually telling SAS to execute that code.

If you want to execute functions in macro code then you need to use the %SYSFUNC() macro function.

%LET CD=%sysfunc(INTNX(DAY,%sysfunc(TODAY()),-1));

This will set CD to the number of days since 01JAn1960.  That is fine for using in SAS code, but would be diffucult for a human to translate.  You could use the option second argument to %SYSFUNC() to have SAS format the result to look like a date.

%LET CD=%sysfunc(INTNX(DAY,%sysfunc(TODAY()),-1),date9);

But then to use it in SAS code you will need to let SAS itself know that you meant that stream of characters to represent a date.  For example by using it as a date literal.

duration = "&cd"d - start_dt ;

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