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
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;
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.
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
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.
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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.