Hello
What is the reason that way3 is not working and I get data set with null values?
What is the way to repair way3 in order to get desired results?
/***Way1****/
/***Way1****/
/***Way1****/
data want1;
b1=1;
b2=11;
b3=12;
b4=13;
Run;
/****Way2******/
/****Way2******/
/****Way2******/
%let b1=1;
%let b2=11;
%let b3=12;
%let b4=13;
data want2;
b1=&b1.;
b2=&b2.;
b3=&b3.;
b4=&b4.;
Run;
/****Way3******/
/****Way3******/
/****Way3******/
%let b1=1;
%let b2=11;
%let b3=12;
%let b4=13;
%macro RRR;
%do i=1 %to &n.;
b&i.=b&i.;
%end;
%mend RRR;
data want3;
%RRR;
run;
It seems to me the log is quite specific about what is wrong with WAY3. Did you look at the log? It appears that you did not.
WARNING: Apparent symbolic reference N not resolved. ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &n. ERROR: The %TO value of the %DO I loop is invalid. ERROR: The macro RRR will stop executing.
Once you fix that, it always helps when running macros to precede the call to the macro with this command which turns on debugging options.
options mprint;
So now with N assigned a value and the debugging option turned on, you will see this in the log
8866 data want3; 8867 %RRR; MPRINT(RRR): b1=b1; MPRINT(RRR): b2=b2; MPRINT(RRR): b3=b3; MPRINT(RRR): b4=b4; 8868 run;
This is not the same code as in WAY1. Can you see the difference?
You do not have any specification of what &n is in your code.
Hi @Ronein
Besides the &n problem mentioned by @PeterClemmensen , your code vil not work, because you don't refer to a macro variable in the version 3 data step.
Compare version 2 to version 3:
b4=&b4.; vs. b&i.=b&i.;
This vill transfer the macro variables to data step variables:
%let b1=1;
%let b2=11;
%let b3=12;
%let b4=13;
%macro RRR;
%do i=1 %to 4;
b&i.=&&b&i.;
%end;
%mend RRR;
data want3;
%RRR;
run;
It seems to me the log is quite specific about what is wrong with WAY3. Did you look at the log? It appears that you did not.
WARNING: Apparent symbolic reference N not resolved. ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &n. ERROR: The %TO value of the %DO I loop is invalid. ERROR: The macro RRR will stop executing.
Once you fix that, it always helps when running macros to precede the call to the macro with this command which turns on debugging options.
options mprint;
So now with N assigned a value and the debugging option turned on, you will see this in the log
8866 data want3; 8867 %RRR; MPRINT(RRR): b1=b1; MPRINT(RRR): b2=b2; MPRINT(RRR): b3=b3; MPRINT(RRR): b4=b4; 8868 run;
This is not the same code as in WAY1. Can you see the difference?
sorry n=4,
still data set is with null values
/****Way3******/
/****Way3******/
/****Way3******/
%let b1=1;
%let b2=11;
%let b3=12;
%let b4=13;
%macro RRR;
%do i=1 %to 4;
b&i.=b&i.;
%end;
%mend RRR;
data want3;
%RRR;
run;
I found the solution
/****Way3******/
/****Way3******/
/****Way3******/
%let b1=1;
%let b2=11;
%let b3=12;
%let b4=13;
options mprint;
%macro RRR;
%do i=1 %to 4;
b&i.=&&b&i..;
%end;
%mend RRR;
data want3;
%RRR;
run;
You have to actually reference the macro variables you have defined. In WAY3 you do not reference any of B1 to B4.
/****Way3******/
%macro RRR;
%do i=1 %to &n.;
b&i.=&&b&i.;
%end;
%mend RRR;
data want3;
%RRR;
run;
You can skip a lot of the macro code and just use data step functions instead.
/****Way4******/
data want;
array b [4];
do i=1 to dim(b);
b[i]=symgetn(cats('b',i));
end;
drop i;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.