08-28-2012 10:19 AM
I need a macro to repeat a data step for several datasets whose names are identical except for 2 characters. In plain english (except for the obvious SAS code) the program
Do the following data step replacing xy with 10, then do it again replacing xy with 20, then do it again replacing xy with 30:
My data step code includes many more statements, but I see no benefit in including them, because I already understand that part!!
08-28-2012 10:35 AM
There are several similar ways to approach the problem. As you start out with macros, you would define a simple macro and call it 3 times:
%macro onetime (datasetnum=);
That gives you flexibility to change the numbers each time, run as many times as you would like, etc.
If you know that you will always use 10, 20, and 30, you can set up a loop:
%do i=10 to 30 %by 10;
If the time periods will change, the programming will get more complicated (not fully demonstrated below).
%macro manytimes (numberlist=10 20 30);
%** Create a loop to pick off each number from the list individually;
%** Generate a data step for each;
Part of picking a "best" solution involves what you need to do, and part of it involves how much macro language you feel comfortable using. This should be a good starting point.
08-28-2012 10:42 AM
My suggestion is what Astounding had drafted as his suggested third choice, namely:
%do %while (%scan(&inlist.,&i.) ne);
08-28-2012 10:50 AM
Not really sure what you need but hoping below code gives you the idea:
08-28-2012 11:27 AM
My .02 on the subject is that this is a very good paper that goes over the basics of Macro processing in 9 steps. It is a good place to start really understanding SAS macro processing and some of the basic concepts:
08-28-2012 12:00 PM
Thanks Cynthia. Obviously, I am new to SAS macros; however, I have done some programming in VB so I am familiar with many of the concepts (macro variables, program control, loops, etc.)