SAS EG 5.1 Macro Variable for date validation

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

SAS EG 5.1 Macro Variable for date validation

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!

 


Accepted Solutions
Solution
‎06-01-2017 10:08 AM
Super User
Super User
Posts: 7,671

Re: SAS EG 5.1 Macro Variable for date validation

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


All Replies
Solution
‎06-01-2017 10:08 AM
Super User
Super User
Posts: 7,671

Re: SAS EG 5.1 Macro Variable for date validation

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;
Super User
Super User
Posts: 6,842

Re: SAS EG 5.1 Macro Variable for date validation

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 ;
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 98 views
  • 2 likes
  • 3 in conversation