BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
Yannick
7 REPLIES 7
deleted_user
Not applicable
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


19
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
deleted_user
Not applicable
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 http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a001263753.htm.

&SYSDATE is described in the Macro Language Dictionary at http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a000489463.htm.

For description of function INTNX(), see http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000212700.htm.

For invoking base SAS functions %SYSFUNC() is described at http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/z3514sysfunc.htm.


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

PeterC
deleted_user
Not applicable
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

Yannick
deleted_user
Not applicable
hire a good consultant 😉

P.S.
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
Cynthia_sas
SAS Super FREQ
Hi:
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.

cynthia
deleted_user
Not applicable
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

David
Cynthia_sas
SAS Super FREQ
Hi:
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...)

cynthia

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!

How to Concatenate Values

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.

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
  • 7 replies
  • 873 views
  • 0 likes
  • 2 in conversation