Desktop productivity for business analysts and programmers

SAS EG 5.1 - Adding date to the end of an XLSX export

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

SAS EG 5.1 - Adding date to the end of an XLSX export

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!

 

 


Accepted Solutions
Solution
‎03-22-2016 12:10 PM
Trusted Advisor
Posts: 1,116

Re: SAS EG 5.1 - Adding date to the end of an XLSX export

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


All Replies
Super User
Posts: 5,387

Re: SAS EG 5.1 - Adding date to the end of an XLSX export

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
Solution
‎03-22-2016 12:10 PM
Trusted Advisor
Posts: 1,116

Re: SAS EG 5.1 - Adding date to the end of an XLSX export

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.

 

Occasional Contributor
Posts: 10

Re: SAS EG 5.1 - Adding date to the end of an XLSX export

Freelance,

 

Thank you so much, that explained a lot!

 

E

☑ This topic is solved.

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

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