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

I am extremely new to SAS, and know nothing of coding!! I am attempting to add a date, in MMMYYYY format, to my exported filename based on the previous month.  For example, If I am reporiting data as of the end of February, and it is currently March. What would I need to write in my %let statement?

 

I have tried

%let reptgmo = %sysfunc(today()-31,AFRDFMYw.);

 

It says I am missing a parenthesis after macro function, but as far as I can see, I have matching parens.  Is there a better way to do this for set it and forget it reporting.

 

Thanks in advance!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @EIrvin,

 

Here's a corrected version of your %LET statement:

 

%let reptgmo=%sysfunc(intnx(month,%sysfunc(today()),-1),AFRDFMY7.);
  • %SYSFUNC must be applied to each data step function used in the %LET statement. So, the missing parenthesis in your case was the one right after "today()". This in turn, however, would have required another function, e.g., the PUTN function (and hence another %SYSFUNC call), to apply the format to the calculated date value.
  • Instead of subtracting 31 days from today's date it's safer to use the INTNX function to correctly go back to the previous month. Please note that otherwise, if the code was executed on 1st March, you would jump into January!
  • The w. in your format specification has to be replaced by the width of the formatted value, i.e. 7 for MMMYYYY.

 

View solution in original post

3 REPLIES 3
LinusH
Tourmaline | Level 20

Macro (%let in your case) is a text focused language. You can force calculation by using %eval(). But it quickly becomes quite unreadable. So sometimes it's easier to do you calculation in a data _null_; step, and then do a call symput() to generate your macro variable. 

 

But if you are new to SA coding, perhaps you should try to use a Prompt instead, which in turn will generate the macro variable for you.

Data never sleeps
FreelanceReinh
Jade | Level 19

Hello @EIrvin,

 

Here's a corrected version of your %LET statement:

 

%let reptgmo=%sysfunc(intnx(month,%sysfunc(today()),-1),AFRDFMY7.);
  • %SYSFUNC must be applied to each data step function used in the %LET statement. So, the missing parenthesis in your case was the one right after "today()". This in turn, however, would have required another function, e.g., the PUTN function (and hence another %SYSFUNC call), to apply the format to the calculated date value.
  • Instead of subtracting 31 days from today's date it's safer to use the INTNX function to correctly go back to the previous month. Please note that otherwise, if the code was executed on 1st March, you would jump into January!
  • The w. in your format specification has to be replaced by the width of the formatted value, i.e. 7 for MMMYYYY.

 

EIrvin
Fluorite | Level 6

Freelance,

 

Thank you so much, that explained a lot!

 

E

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1842 views
  • 1 like
  • 3 in conversation