Hi all,
I'm sure this is a relatively easy question (or there is an alternative way of doing it that's less confusing) but I'm working on automating a number of reports I need to run every month. For the first report, all I need to do is export the data from a SQL Server view to a tab delimited file. All that is easy; what I'm stuck on is naming of the file. I'm using SAS 9.4 locally installed on a Windows 7 machine.
Right now, I have
%let mnth = 5;
%let yr = 2016;
proc sql;
create table work.test as
/* a bunch of code */
quit;
proc export data = work.test
outfile = "C:\Report__&yr&mnth..txt"
DBMS = tab replace;
putnames = no;
run;
This all works perfectly, but what I'm trying to do set it up so the &mnth and &yr variables are always the previous month (and year) so I don't have to change them every month. I've tried &month = intnx('month',have.date,-1) but then my file name has the intnx function in the file name, which doesn't help 🙂
I've spent some time on Lexjansen.com but can't seem to find anything; maybe I'm looking under the wrong keywords (dynamic, fiscal year, export etc.).
Any suggestions?
Thanks!
Chris
%let year_month = %sysfunc(intnx(month, %sysfunc(today()), -1, b), yymmn6.);
Edit: Original answer had two typo's (syfunc vs sysfunc and the b does not need quotation marks).
%let year_month = %sysfunc(intnx(month, %sysfunc(today()), -1, b), yymmn6.);
Edit: Original answer had two typo's (syfunc vs sysfunc and the b does not need quotation marks).
It's the right idea, but I would suggest you consider:
%let year_month=;
%if %length(&year_month)=0 %then %let year_month= /* same thing */;
That way, if you ever want to backdate the report, you only need to change the first %LET statement, not the second. But under normal circumstances, just let it run.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.