The SAS Output Delivery System and reporting techniques

dynamic ods body filename - monthly - unable to assign variable

Posts: 0

dynamic ods body filename - monthly - unable to assign variable

What Im trying to do is fairly simple, running a monthly
batch sas program to save html output into files named dynamically the
name of the previous month/year

so here is the log of the code I write to set a variable with
the previous month ( wanted to achieve something like 012007 for
january 2007 for example ) :

14 data _null_;
15 month=month( today() - day(today()) );
16 year=year( today() - day(today()) );
17 %let monthYearChar=month || year;
18 call symput('monthYear',&monthYearChar);
19 %put ( "_&monthYear._");
( "_ 11 2007_")
20 run;

see that : ( "_ 11 2007_")
its like it is inserting leading spaces to month and year when I concatenate them

anyone as a better idea on how to do this betteR ?

Thanks in advance
Posts: 0

Re: dynamic ods body filename - monthly - unable to assign variable

Your %Let and %Put statements should not be inside the data step.

%Let is a global statement and functions outside the data step environment. Your %Put is also global, but the value you set in the macro symbol table will not be available until after the data step has executed, so the value at line 19 is not from that data step, but from a previous iteration.

Subtracting days from month and year values is prone to problems, especially around this time when a January run may not correctly specify the December month and year. Instead, use a time interval function as the code I provide below does and you will then get the right values.

14 Data _NULL_;
15 LASTMONTH = IntNx( 'Month', Today(), -1, "e");
16 HTMLNAME = "_ " || Put( LASTMONTH, Month2.) || " " || Put( LASTMONTH, Year4.) || " _";
17 Call Symput( 'MonthYear', HTMLNAME);
18 Run;

NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

20 %Put &MonthYear.;
_ 11 2007 _

Kind regards

David P.S. Including spaces in path and file names is asking for trouble in Windows. I would remove the spacing from the name.

Message was edited by: dkvj
Posts: 0

Re: dynamic ods body filename - monthly - unable to assign variable

David and Cynthia have explained some risks of the original approach and a better way of dealing with the information through the data step.

Having some experience of implementing batch processes with similar need of last month's date, I can offer a reduced solution that appears almost intuitive.

If you seek a simple statement that delivers "last month" in some format, consider these alternatives all of which use
the "interval next" function, INTNX() applied to
the automatic macro variable &sysDate (which provides the SAS session start date - a date you can usually rely on in batch processing).

What makes the following alternatives different, is just the format of the date info:

e.g. NOV2007
%let LastMon = %sysfunc( intnx( month "&sysdate"d, -1 ), monyy7 );

e.g. NOV07
%let LastMon = %sysfunc( intnx( month "&sysdate"d, -1 ), monyy5 );

e.g. 11-2007
%let LastMon = %sysfunc( intnx( month "&sysdate"d, -1 ), mmyyD7 );

e.g. 2007.11
%let LastMon = %sysfunc( intnx( month "&sysdate"d, -1 ), yymmP6 );

e.g. 0711
%let LastMon = %sysfunc( intnx( month "&sysdate"d, -1 ), yymmD4 );

e.g. 30NOV2007
%let LastMon = %sysfunc( intnx( month "&sysdate"d, -1, e ), date9 );

e.g. 20071130
%let LastMon = %sysfunc( intnx( month "&sysdate"d, -1, e ), yymmddN8 );

There are many more date formats: look for Date and Time in Formats by Category in SAS Online-Doc at

&SYSDATE is described in the Macro Language Dictionary at

For description of function INTNX(), see

For invoking base SAS functions %SYSFUNC() is described at

The most complete list of formats can be found in the internal dictionary table of formats which can be used as SAS System data set SASHELP.VFORMATS.

Seasons Greetings

Posts: 0

Re: dynamic ods body filename - monthly - unable to assign variable

Thank you, it as been very helpfull
do you recommend books or training to learn about these basic sas techniques ?

I wish there will be usefull presentations on basic sas stuff at next SUGI in texas
where I may attend this spring

Posts: 0

Re: dynamic ods body filename - monthly - unable to assign variable

hire a good consultant ;-)

and do come to San Antonio for SAS Global Forum (March 16-19, 2008) where the author is presenting in the section "Foundations and Fundamentals", a tutorial titled Add SAS(r) Macros to Your Programming Skills: Achieve More, Write Less Code

PeterC Message was edited by: Peter_c
Posts: 8,743

Re: dynamic ods body filename - monthly - unable to assign variable

The SAS Institute Macro classes are excellent. Many of these techniques for DATA step programming are basic programming techniques that are taught in the Programming I and Programming II classes (such as learning about functions, learning to read files, learning about conditional logic).

There are generally several presentations at SGF that cover Macro processing from beginner level to advanced level. The SAS Macro facility is excellent. The SAS Publishing Books by Michelle Burlew and Art Carpenter on the SAS Macro facility are excellent. There is a wealth of information freely available on SAS Macro processing, if you do a Google search on the words:
SAS Macro beginner
...the first 2 hits are papers on SAS Macro facility for beginners.

Posts: 0

Re: dynamic ods body filename - monthly - unable to assign variable

Papers are selected for SAS Global Forum to cover the range from beginner to advanced. Often, the beginner presentations are among the best attended at the conference, and planning, and arriving in good time are good strategies for ensuring you get into the room. (The fire marshalls strictly enforce room occupancies, so tucking yourself into a corner or on the floor in the aisle will not be permitted.) The Hands On Workshops are especially likely to turn away attendees. (Of course everything is bigger in Texas, so perhaps that won't be a problem in 2008 )

Each conference usually includes three days of SAS public training before commencement and often now includes a number of days afterwards. All levels of knowledge are catered for, although the products presented are necessarily restricted.

I have had the pleasure of being in Cynthia's classes a number of times; indeed she ignited the fire for ODS in me some years ago. At the very least, you should enquire about and try to attend the one-day Geekfest if it is held. The insights into what is under development for ODS, doing the impossible with ODS and why ODS isn't quite doing what you wanted it to... yet: are well worth arriving a day or two early. Eric's enthusiasm about templates is infectious.

As Cynthia said, Macros are always well covered, if you look up the paper list for SGF07 you'll see a number of hits. Just remember Zender Rule 1: always start with working and tested SAS code.

Kind regards

Posts: 8,743

Re: dynamic ods body filename - monthly - unable to assign variable

There is a rule in Macro processing that if you create macro variables inside a data step program, you cannot reference those macro variables (and get a correct result) until after a step boundary for the data step program. (As David has already explained...)

Ask a Question
Discussion stats
  • 7 replies
  • 2 in conversation