need macro to repeat data step

Reply
Occasional Contributor
Posts: 5

need macro to repeat data step

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

would be:

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:

Data new_xy;

     set old_xy;

run;

My data step code includes many more statements, but I see no benefit in including them, because I already understand that part!!

Super User
Posts: 5,500

Re: need macro to repeat data step

jgscott3,

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=);

   data new_&datasetnum;

      set old_&datasetnum;

   run;

%mend onetime;

%onetime (datasetnum=10)

%onetime (datasetnum=20)

%onetime (datasetnum=30)

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:

%macro onetime;

   %local i;

   %do i=10 to 30 %by 10;

      data new_&i;

         set old_&i;

      run;

   end;

%mend onetime;

%onetime

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;

%mend manytimes;

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.

Good luck.

Occasional Contributor
Posts: 5

Re: need macro to repeat data step

Posted in reply to Astounding

Thank you for your help.

PROC Star
Posts: 7,468

Re: need macro to repeat data step

My suggestion is what Astounding had drafted as his suggested third choice, namely:

%macro makenew(inlist);

  %let i=1;

 

  %do %while (%scan(&inlist.,&i.) ne);

    data new_%scan(&inlist.,&i.);

      set old_%scan(&inlist.,&i.);

    run;

    %let i=%eval(&i.+1);

  %end;

%mend makenew;

%makenew(20 30)

Occasional Contributor
Posts: 5

Re: need macro to repeat data step

Thank you very much.

Respected Advisor
Posts: 4,173

Re: need macro to repeat data step

Not really sure what you need but hoping below code gives you the idea:

%macro sample;

  %let xy=10;

  %do %until(&xy>100);

    Data new_&xy;

         set sashelp.class;

    run;

    %let xy=%eval(&xy+10);

  %end;

%mend;

%sample;

Occasional Contributor
Posts: 5

Re: need macro to repeat data step

Thank you Patrick.

SAS Super FREQ
Posts: 8,864

Re: need macro to repeat data step

Hi,

  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:

http://www2.sas.com/proceedings/sugi28/056-28.pdf

cynthia

Occasional Contributor
Posts: 5

Re: need macro to repeat data step

Posted in reply to Cynthia_sas

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

Ask a Question
Discussion stats
  • 8 replies
  • 1349 views
  • 0 likes
  • 5 in conversation