Hello fellow SAS programmers,
I have this code that I cannot determine how to get beyond the error. The error is "Expecting an arithmetic operator." But, I simply want to do a length of a macro variable value
%let stat=%substr(N PCT N_ANY PCT_ANY,1);
data bob2;
set bob;
do i=1 to length(&stat);
%let st=upcase(scan("&stat",i));
if &st = N then %let _n=Y;
if &st = PCT then %let _pct=Y;
if &st = PERCENT then %let _pct=Y;
if &st = N_ANY then %let _nany=Y;
if &st = PCT_ANY then %let _pctany=Y;
if &_nany=Y or &_pctany=Y then %let _anyev=Y;
end;
run;
NOTE: Line generated by the macro variable "STAT". 1 N PCT N_ANY PCT_ANY ___ 388 76 ERROR 388-185: Expecting an arithmetic operator. ERROR 76-322: Syntax error, statement will be ignored.
If I use %length, I get notes such as the following:
NOTE: Variable N is uninitialized. NOTE: Variable PCT is uninitialized. NOTE: Variable PERCENT is uninitialized. NOTE: Variable N_ANY is uninitialized. NOTE: Variable PCT_ANY is uninitialized. NOTE: Variable Y is uninitialized.
I do not even have a variable Y.
My question is: how do I avoid the error and the notes?
If I keep it length(), I do not know how to avoid the error (expecting an arithmetic operator).
If I keep the %length, I do not know how to avoid the notes.
Thank you in advance for your support and for reading my question.
From now on, please show us the entire log for this data step, so we can see the code and all messages; please do not show us just the errors
You cannot mix and match macro variables and data step variables as you have done. &i is a macro variable, and it does not know about the data step variable called i
Why even use a macro variable here? This should work, although I cannot test it because I don't have your data.
data bob2;
set bob;
stat='N PCT N_ANY PCT_ANY';
do i=1 to countw(stat);
st=scan(stat,i);
if st = 'N' then _n=Y;
if st = 'PCT' then _pct=Y;
if st = 'PERCENT' then _pct=Y;
if st = 'N_ANY' then _nany=Y;
if st = 'PCT_ANY' then _pctany=Y;
if _nany=Y or _pctany=Y then _anyev=Y;
end;
run;
data bob2;
set bob;
stat='N PCT N_ANY PCT_ANY';
do i=1 to countw(stat);
st=scan(stat,i);
if st = "N" then _n="Y";
if st = "PCT" then _pct="Y";
if st = "PERCENT" then _pct="Y";
if st = "N_ANY" then _nany="Y";
if st = "PCT_ANY" then _pctany="Y";
if _nany="Y" or _pctany="Y" then _anyev="Y";
end;
run;
In the original code the values on the RIGHT of the = N PCT PERCENT N_ANY PCT_ANY and Y since they were not quoted were treated as variable names that had no values assigned. Hence the "uninitialized" notes.
So the error is from this function call:
length(&stat)
So based on how you defined the macro variable STAT this is the code that SAS sees after the macro processor has finished replacing the macro code with the generated text:
length(N PCT N_ANY PCT_ANY)
If you want to evaluate the length of that string you could either use the macro function %LENGTH() or give the LENGTH() function a string to evaluate.
%length(&stat)
length("&stat")
But as you note this does not fix the logic problem you have with your code. You are increasing the value if I based on the number of characters in the string. But you are actually testing based on the number of "words" in the string. To count that use the COUNTW() function, not the LENGTH() function.
How ever the whole program is extremely confused. For example this statement makes no sense at all.
if &st = N then %let _n='Y';
Once the macro processor has finished the condition will be valid (but useless) SAS code
if upcase(scan("N PCT N_ANY PCT_ANY",i)) = N then;
So the macro variables ST and STAT will resolve to a valid SAS function call on the left of the equality operator. The result of that upcase() function will be compared the variable N. Does your dataset include a variable named N? But no matter whether the equality test is TRUE or FALSE there is no SAS statement to be executed after the THEN statement.
Please explain what you are trying to do. Provide example input data and the expected resulting data.
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.