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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 1051 views
  • 1 like
  • 3 in conversation