DATA Step, Macro, Functions and more

Creating and using Macro variables in a loop

Accepted Solution Solved
Reply
Contributor
Posts: 66
Accepted Solution

Creating and using Macro variables in a loop

Program to created two macro variables mm and nn.

/*****************Top***********/

Data aa;

……….

Run;

Data bb;

Set aa;

……

Run;

Proc reg data =bb;

…….

Run;

Data _null_;

……….

Call symputx(‘mm’, ...);

Run;

Data _null_;

.......

Call symputx(‘nn’,...);

Run;

/********************Bottom*****************/

Each time above program(Top to bottom) is run, we get a value  for  each mm and nn. For different runs these values will be different. I need to run the above program(Top-bottom) 100 times to extract 100 different values for  mm and nn. I tried something like this

%Macro Test;

% global mm1 nn1;  /*I am trying make these variables global to use in the next program.*/

….above program from top to bottom

%let mm1= &mm; 

%let nn1= &nn;

%Mend Test;    .................................. This still does not work!

Finally,  would like to extract 100 values of these variables the following way

Data final;

Do i=1 to 100;

%Test 

Mm2 =&mm1;

Nn2=&nn1;

Output;

drop i;

End;

Run;

This was what I tried.....need some help .....

Thanks again!

Raghu


Accepted Solutions
Solution
‎11-29-2012 09:35 PM
Super User
Super User
Posts: 7,076

Re: Creating and using Macro variables in a loop

Here is coding pattern that might work for you.  You will not need to change you existing program as it can continue to generate MM and NN macro variables.

%macro loop ;

proc delete data=final; run;

%do i=1 to 100 ;

...

your existing program

...


* Generate single obs dataset ;

data this_case ;

  mm=&mm; nn=&nn ;

run;

* Generate aggregate dataset ;

proc append base=final data=this_case;

run;


%end;

%mend loop;

View solution in original post


All Replies
Super User
Posts: 19,860

Re: Creating and using Macro variables in a loop

What are you trying to do? There easier ways to capture the the parameter estimates from multiple regression models in a single file.

See the last example here...

Statistical Computing Seminar: Introduction to SAS Macro Language

Contributor
Posts: 66

Re: Creating and using Macro variables in a loop

I understand what you mean to say. But in this case those mm and nn variables/parameter estimates are from two different regression procedures. mm  is the parameter estimate(b1) from simple linear regression and nn from weighted regression. There is a random number option in one of the programs, so each time we run above program

Top-bottom we get one mm from one model and one nn from another. I need to run this many times ....actually 1000 times and keep extracting these estimates. It looks like bootstrapping but it is not. I did something like this in R but this time I need to use SAS. Thanks for your response.

Super User
Posts: 19,860

Re: Creating and using Macro variables in a loop

Since you're aiming to get them all in the same dataset at the end using that data final loop at the end a proc append seems better to me within rather than storing 1000's of macro variables..

My opinion though.

%macro (param1, param2);

Data aa;

……….

Run;

Data bb;

Set aa;

……

Run;

Proc reg data =bb;

…….

output out parameterestimates=reg1;

Run;


proc reg data=bb;

...

output out parameterestimates=reg2;

run;


data reg1;

set reg1;

model=some_model_id;

run;


data reg2;

set reg2;

model=some_model_id;

run;


proc append data=reg1 base=linreg;

run;


proc append data=reg2; base=multreg;

run;


%mend;


data final;

merge linreg multreg;

by some_model_id;

run;

Solution
‎11-29-2012 09:35 PM
Super User
Super User
Posts: 7,076

Re: Creating and using Macro variables in a loop

Here is coding pattern that might work for you.  You will not need to change you existing program as it can continue to generate MM and NN macro variables.

%macro loop ;

proc delete data=final; run;

%do i=1 to 100 ;

...

your existing program

...


* Generate single obs dataset ;

data this_case ;

  mm=&mm; nn=&nn ;

run;

* Generate aggregate dataset ;

proc append base=final data=this_case;

run;


%end;

%mend loop;

Contributor
Posts: 66

Re: Creating and using Macro variables in a loop

Thanks Tom!

With your help I was able to complete what I aiming to do. I am still a kindergartener at Macro school.

I do not have the skills to access the data set final..created within the macro.  Instead ran the program 100 times manually and got what I  was looking for...very inefficient. Is there a way I can extract this data set 'final' after running the macro.

Sorry for my silly stuff.

Sincerely,

Raghu.

Super User
Super User
Posts: 7,076

Re: Creating and using Macro variables in a loop

It depends on how you are running SAS.  It you are running interactively then the FINAL dataset should be in the WORK library and you can print it or run statistical procs against it.

proc print data=final; run;

If you are running as a batch (or background) job then you should create a LIBNAME statement and write the data to that libname instead of the work.

LIBNAME mydata 'c:\mydate';  data mydata.final; set final; run;

If you are using EG then I am not sure, but it should have method to store or download the dataset.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 300 views
  • 4 likes
  • 3 in conversation