BookmarkSubscribeRSS Feed
cypher85
Calcite | Level 5

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!

4 REPLIES 4
Linlin
Lapis Lazuli | Level 10

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

art297
Opal | Level 21

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.

Astounding
PROC Star

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.

Ksharp
Super User

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1512 views
  • 0 likes
  • 5 in conversation