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

Hi All,

I have written the below to put all my rmse output from an earlier regression macro into macro variables.

If I run the below sql statement without inserting into macro (but using macro variables) it runs fine. If I run it with macro variable then i do get a log which says

proc sql; select _rmse_ into: rmse_5 from regout5; quit;

However when I do %put &rmse_5 it says macro variable not found.

Code I am running is:

options mprint;

%macro merge3;

%do i=1 %to 5;

proc sql; select _RMSE_ into :rmse_&i from regout&i;

%end;

quit;

%mend;

%merge3;

Question is: What is wrong with the above macro which is leading to %put &rmse_5 saying macro variable not found.

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

For test data I meant just make something up and post a dataset statement here, like:

data regout1;
  _rmse_=1; output;
run;
data regout2;
  _rmse_=4; output;
run;
%global rmse_1;
%global rmse_2
options mprint symbolgen mlogic;
%macro merge3;
  %do i=1 %to 2;
    proc sql;
      select  _RMSE_
      into    :rmse_&i   
      from    regout&i;
    quit;
  %end;
%mend;
%merge3;

%put &rmse_1.;
%put &rmse_2.;

Anyway, the reason is that the macro variables in the macro are considered to be Local to the macro.  If you put:

%global rmse_1;

%global rmse_2;... etc. before your code as I have done above it should work.

However, why do it like that?  What are you using these macro variables for, better ways exist for most scenarios.  You will run in to further issues later, i.e. how many are there, how to loop over them etc.

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, two things.  First, would need the full log, i.e. does regout1-regout5 actually exist, and is there a variable _rmse_ in each one?

Personally I finds there's rarely a need to create lots of macro variables.  Provide a some sample data, required output or process.  By group processing, arrays etc. are far quicker and more effective than creating lists of arrays to loop over.

Jane7476
Calcite | Level 5

Hi,

There is a data set regout1-5 and _rmse_ also exists.

Unfortuantely don't have access to upload data from this machine.

Thanks

RW9
Diamond | Level 26 RW9
Diamond | Level 26

For test data I meant just make something up and post a dataset statement here, like:

data regout1;
  _rmse_=1; output;
run;
data regout2;
  _rmse_=4; output;
run;
%global rmse_1;
%global rmse_2
options mprint symbolgen mlogic;
%macro merge3;
  %do i=1 %to 2;
    proc sql;
      select  _RMSE_
      into    :rmse_&i   
      from    regout&i;
    quit;
  %end;
%mend;
%merge3;

%put &rmse_1.;
%put &rmse_2.;

Anyway, the reason is that the macro variables in the macro are considered to be Local to the macro.  If you put:

%global rmse_1;

%global rmse_2;... etc. before your code as I have done above it should work.

However, why do it like that?  What are you using these macro variables for, better ways exist for most scenarios.  You will run in to further issues later, i.e. how many are there, how to loop over them etc.

Jane7476
Calcite | Level 5

Thanks a ton.

I should have realised earlier that wasn't declaring it as a global variable.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1524 views
  • 0 likes
  • 2 in conversation