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.
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:
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
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.
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.
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...)