DATA Step, Macro, Functions and more

Macro variable, Datalines and a Loop

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Macro variable, Datalines and a Loop

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


Accepted Solutions
Solution
‎11-27-2014 05:44 AM
Super Contributor
Posts: 305

Re: Macro variable, Datalines and a Loop

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


All Replies
Super User
Super User
Posts: 7,413

Re: Macro variable, Datalines and a Loop

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.

Contributor
Posts: 22

Re: Macro variable, Datalines and a Loop

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

Super User
Super User
Posts: 7,413

Re: Macro variable, Datalines and a Loop

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.

Super User
Posts: 6,962

Re: Macro variable, Datalines and a Loop

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;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎11-27-2014 05:44 AM
Super Contributor
Posts: 305

Re: Macro variable, Datalines and a Loop

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;

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 1407 views
  • 7 likes
  • 4 in conversation