Joshua Schlichting
Joshua Schlichting

Reputation: 3460

Subtract date from macro variable

I'm having trouble subtracting a date from a Macro Variable.

Currently, I create a macro variable by running:

%LET date = %SYSFUNC(TODAY(),MMDDYY10.); 

I feel like I should be able to subtract 1 day from &date by doing the following:

%LET newDate = %SYSFUNC(%INTNX('day',&date,-1),date9.);

However, this produces the error:

ERROR: Function name missing in %SYSFUNC or %QSYSFUNC macro function reference.

I need the output for &newDate to be in date9.

Any help would be appreciated, thanks!

Upvotes: 3

Views: 3781

Answers (2)

vknowles
vknowles

Reputation: 784

@RawFocus, you are correct that there is no need to format the original date (today's date), and it is easier to deal with that way.

Just for completeness, if someone wanted to apply the MMDDYY10. format, this is how it could be done:

%LET date = %SYSFUNC(TODAY(),mmddyy10.); 
%LET newDate = %SYSFUNC(INTNX(day,%SYSFUNC(INPUTN(&date,mmddyy10)),-1),date9.);
%put &=date &=newdate;

Upvotes: 0

Allan Bowe
Allan Bowe

Reputation: 12701

Quick answer:

%LET date = %SYSFUNC(TODAY()); 
%LET newDate = %SYSFUNC(INTNX(day,&date,-1),date9.);
%put &=newdate;

Explanation:

Firstly, best to remove the formatting from &date to ensure it is interpreted correctly as a date. Your original code resolved (today) inside intnx() to 12/06/2016, which then resolved to 12 divided by 6 divided by 2016 - etc.

Secondly, the inner function to %sysfunc() should be a datastep function - indeed, the whole point of %sysfunc() is to bring these functions into sas. %intnx() isn't a macro function, but if if was, then by definition you wouldn't need to wrap it in %sysfunc().

Finally, the 'day' parameter shouldn't be quoted - everything in sas macro is treated as text by default.

Upvotes: 4

Related Questions