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,

Hope this problem seems interesting.

I have:

- Macro Variables

- That I want to put in a new table (can do it easily in an existing table but i wanted to see if i can push my luck)

- Run a loop

I have:

%let rmse_1=10;

%let rmse_2=234.32;

%let rmse_3=231.24;

data stats;

     input rmse $10.;/*adding it as character as it didn't work with numeric and thought i can easily convert later on*/

          rmse=deqoute(resolve(quote(rmse)));/*to ensure that macro variable gets read*/

               do i=1 to 3;

               output;

               stop;

     datalines;

&&rmse_&i

;

run;

Problem: I get the last rmse populated thrice in the table instead of the individual rmse. Until now unable to find a resource that uses macro variable in a datelines with a loop.

Any help is much appreciated as this will go towards building a table with lots of other stats in.

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Loko
Barite | Level 11

Hello,

Another solution:

%let rmse_1=10;

%let rmse_2=234.32;

%let rmse_3=231.24;

data want;

do i=1 to 3;

rmse=symget(cat("rmse_",i));

output;

end;

run;

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Sorry, have to ask why?  You must have put the data into the macro variables somewhere and now your putting them back into a datastep, seems like going round in circles to me.  There are many ways to get macro variables: symget is one, you could also just use the SAS metadata tables:

data stats;

     set sashelp.vmacro (where=(name in ("XYZ","ERT"...) keep=value);

run;

This returns a dataset with a list of values held in the macro variables in the in list.

I return however to my original question - why?  Provide some background on the problem as 99% of the time there is no need for macro anything, just a way of thinking about the data and your process.

Jane7476
Calcite | Level 5

You are right. Maybe there is no need for a macro.

The issue is: I have ods output and other tables where stats are stored. I need the stats (some from each of the tables) in a particular pre-formatted table design. Hence I thought of putting macro variables into a new table.

Thanks

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, something I do all the time.  My approach, without using any macro code:

proc sql;

     create table OUTPUT

     (

          COL1 char(200) label "Stat",

          COL2 char(200) label "Result"

     );

quit;

proc sql;  

     insert into OUTPUT

     set COL1="n",

          COL2=(select put(n,3.) from MEANS_OUTPUT)

     set COL1="mean",

           COL2=(select put(mean,5.2) from MEANS_OUTPUT)

...

quit;

Think it seems a bit long winded, fine combine the approach with arrays or datasets of parameters, and call execute and you can generate a whole output table with a datastep of 5 or 6 rows, formatted etc.

Kurt_Bremser
Super User

As often, you confuse macro execution with data step execution; i is a data step variable, &i is a macro variable, both are completely disconnected.

If you think you need the macro, do it this way:

%macro datalines;

%do i = 1 %to 3;

&&rmse_&i

%end;

%mend;

data .....;

infile cards;

input rmse;

cards;

%datalines

;

run;

Loko
Barite | Level 11

Hello,

Another solution:

%let rmse_1=10;

%let rmse_2=234.32;

%let rmse_3=231.24;

data want;

do i=1 to 3;

rmse=symget(cat("rmse_",i));

output;

end;

run;

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
  • 5 replies
  • 4549 views
  • 7 likes
  • 4 in conversation