BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rnmishra
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

6 REPLIES 6
Reeza
Super User

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

rnmishra
Calcite | Level 5

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.

Reeza
Super User

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;

Tom
Super User Tom
Super User

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;

rnmishra
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 6 replies
  • 1400 views
  • 4 likes
  • 3 in conversation