I have placed the names of all of the variables in a dataset into macro variables &&name&i. through &&name&n.
I have no trouble whatsoever with SAS resolving the macro reference in the code.
What is giving me a problem is the statemts in my data step which use the resolved macor variable.
For example:
1.
%do i=1 %to &n. %then %do;
if &&name&i. not in (,.' ') then
hold_&&name&i.=&&name&i.;
%end;
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
2.
%do i=1 %to &n. %then %do;
%if &&name&i. not in(.," ")%then
hold_&&name&i.=&&name&i
%end;
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 1
SYMBOLGEN: Macro variable NAME1 resolves to APPL_NBR
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &&name&i. not in(.," ")
ERROR: The macro TESTIT will stop executing.
The problem ONLY lends itself to condition where if statments are employed.
The following works well and does generate any errors at all:
% do i=1 %to &n.;
hold_&&name&=&&name&i.;
you do loop syntax is not correct and your use of the in condition is incorrect.
%macro holdname(n);
%do i=1 %to &n;
if &&name&i not in ('.',',',' ') then hold_&&name&i=&&name&i;
%end;
%mend;
%if does not have in condition available to it. (unless it was added in 9.3)
%macro holdname(n);
%do i=1 %to &n;
%if %quote(&&name&i) ne "," and %quote(&&name&i) ne "." and %quote(&&name&i) ne " " %then %let hold_&&name&i=&&name&i;
%end;
%mend;
You'll have to show more of your code. Is this part of a datastep? If not, you are trying to create a variable outside of a data step and that will definitely give you problems.
In your first example, it simply isn't (I don't think) valid code:
%do i=1 %to &n. %then %do;
you do loop syntax is not correct and your use of the in condition is incorrect.
%macro holdname(n);
%do i=1 %to &n;
if &&name&i not in ('.',',',' ') then hold_&&name&i=&&name&i;
%end;
%mend;
%if does not have in condition available to it. (unless it was added in 9.3)
%macro holdname(n);
%do i=1 %to &n;
%if %quote(&&name&i) ne "," and %quote(&&name&i) ne "." and %quote(&&name&i) ne " " %then %let hold_&&name&i=&&name&i;
%end;
%mend;
I'm still not sure what you are trying to do. Does the following approximate it?
%let name1=foo;
%let name2=bar;
%let n=2;
data try;
call missing(foo1);
foo2=5;
foo=3;
bar1=1;
call missing(bar2);
bar=3;
run;
%macro testthis;
data test;
set try;
%do i=1 %to &n.;
if not missing(&&name&i.) then
hold_&&name&i.=&&name&i.;
%end;
run;
%mend;
%testthis
You are both basically correct.
I want to loop around from i=1 to &n
and test each variable &&name1 through &&name&n to see if it is missing or blank.
If it is not, place it in hold_&&name&i.
Now, I have modified the code to read:
%do i=1 to &n.;
if &&name&i. not in ('.' , '') then
hold_&&name&i.=&&name&i.
%end
AND IT WORKS!! it was simply an error in the "in" syntax.
Thanks everyone!
One little side note, a macro variable name can only contain letters and numbers so any other character will automatically call the end of the name to resolve so the periods you are using are not necessary, so a macro variable name followed by a space, =, or ), etc does not need the . qualifier. Maybe it is just a pet peeve of mine...
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.