DATA Step, Macro, Functions and more

Nesting a macro within another macro

Reply
Contributor
Posts: 23

Nesting a macro within another macro

Hi all,

I have this macro

%macro var(sec);

data f;

  set f;

  if &sec not = "" then tot_&sec + 1;

  by ptstid cycle;

  if first.cycle and &sec = 1 then tot_&sec = 0;

run;

%MEND var;

options mprint;

%var(gm_hold)

%var(gm_red)

%var(cis_hold)

%var(cis_red)

%var(cet_hold)

%var(cet_red)

The macro as currently written is working on dataset "f". However, I have 4 different datasets that I need to run this same macro on (f,g,h,i). How would I write a macro that would do this. Essentially I am trying to do a macro inside of a macro. Thanks for any help!

Super Contributor
Posts: 1,636

Re: Nesting a macro within another macro

Hi,

Do you want to try if this one works?

%let dsn=f g h i;

%let var=gm_hold gm_red cis_hold cis_red cet_hold cet_red;

%macro var();

%do i=1 %to %sysfunc(countw(&dsn));

   %let ndsn=%sysfunc(scan(&dsn,&i));

      %do j=1 %to %sysfunc(countw(&var));

         %let nvar=%sysfunc(scan(&var,&j));

              data &ndsn._&nvar;

                set &ndsn;

           if &nvar ne "" then tot_&nvar+1;

           by ptstid cycle;

         if first.cycle and &nvar = 1 then tot_&nval = 0;

       run;

      %end;

  %end;

%MEND var;

%mend;

options mprint;

%var()

Linlin

Message was edited by: Linlin

PROC Star
Posts: 7,363

Re: Nesting a macro within another macro

I wouldn't recommend nesting macros within macros.  One alternative might be to call the macro, using call execute from a data step.  That way, you could either list all of your desired files within the datastep, or use a pipe to identify the files.

Super User
Posts: 5,085

Re: Nesting a macro within another macro

cypher85,

You would be better served, and learn more, if you took this in steps.  Each of these steps will force you to learn something.

First, without the use of any macro language, how could a single DATA step process all 6 variables?  Remember, each DATA step has to input and output all the observations.  If you can use one DATA step instead of six, your programs will run considerably faster.

Second, how could you do this in macro language?  Using just one data set, pass a list of variable names instead of just one.  Have the macro use that list of variable names to construct the program that you came up with in step 1.

Finally, how could you add a second parameter to the macro that contains a list of data set names.  Have the macro run through the same process for each data set in the list.  This is the least important step on the list.  Your program would be easier to write, and easier to understand, if you were to forget about this step and just run the macro separately for each of your four data sets.

Good luck.

Super User
Posts: 9,685

Re: Nesting a macro within another macro

As Astounding said ,add another parameter to identify a dataset.

%macro var(dsn,sec);

data &dsn;

  set &dsn;

  if &sec not = "" then tot_&sec + 1;

  by ptstid cycle;

  if first.cycle and &sec = 1 then tot_&sec = 0;

run;

%MEND var;

options mprint;

%var(f,gm_hold)

%var(g,gm_red)

Ksharp

Ask a Question
Discussion stats
  • 4 replies
  • 265 views
  • 0 likes
  • 5 in conversation