%macro use;
proc contents data=check(drop=y) out=_out;run;
proc sql;
select distinct name into: list separated by ' '
from _out;
%put the value of %nrstr(&list):&list;
quit;
data naya;
set check;
array n{*} &list;
array x{*} _numeric_;
%do i= 1 %to dim(n);
n{i}=(x({i}=.);
%end;
run;
%mend use;
options mprint mlogic symbolgen;
%use
/*********************************************/
Hi All,
I'm trying to use list of variables which are created using proc sql INTO: in to array do loop SAS code, but I have encountered following ERRORS:
ERROR: Required operator not found in expression: dim(n)
ERROR: The %TO value of the %DO I loop is invalid.
Any suggestion, helps are highly appreciated.
Thanks!
If you were by some chance expecting to loop over the number elements created here:
proc sql;
select distinct name into: list separated by ' '
from _out;
%put the value of %nrstr(&list):&list;
quit;
It may help IF EACH NAME is a single word that you can get a count :
%let namecount= %sysfunc(countw,&list,' '));
And use &namecount for the loop control.
Especially if you intended
Array n &list;
to declare an array consisting of variable names.
Of course you may have an issue with numeric and character variables as you can't mix those in a single array.
But if you want to do something to all of the numeric variables:
Data naya;
set check;
array n _numeric_;
do i = 1 to dim(n);
end;
run;
Similar you could declare an additional array of _character_ and no macro is needed at all.
Hi @koomalkc,
Data step information such as dim(n) is unavailable to macro statements (here: %DO), because macro statements are resolved before the data step is even compiled. You can simply use a data step DO-loop instead of a macro %DO-loop there. Hence, there is no longer a need for wrapping your code into a macro at all, so you can delete the %MACRO, %MEND and %USE statements altogether.
On a separate note, it's a little strange to see two arrays here. For the program to work at all (assuming the current error is worked through), the data set CHECK must contain only a single character variable named "Y". Otherwise the dimensions of the two arrays will be different.
Is your intent to replace the original numeric variables with new values (1 for missing, 0 for nonmissing)? In that case you would only need one array.
Or is your intent to create a new set of variables with these 0/1 values? In that case, you need to do some more work to count and name a new set of variables.
Post some test data in the form of a datastep, and what you want the output to look like. I don't see any need for any macro code here, just do it in base SAS.
What are you trying to accomplish with this:
n{i}=(x({i}=.);
?
It is at least syntactically incorrect by missing a closing paranthesis.
If you were by some chance expecting to loop over the number elements created here:
proc sql;
select distinct name into: list separated by ' '
from _out;
%put the value of %nrstr(&list):&list;
quit;
It may help IF EACH NAME is a single word that you can get a count :
%let namecount= %sysfunc(countw,&list,' '));
And use &namecount for the loop control.
Especially if you intended
Array n &list;
to declare an array consisting of variable names.
Of course you may have an issue with numeric and character variables as you can't mix those in a single array.
But if you want to do something to all of the numeric variables:
Data naya;
set check;
array n _numeric_;
do i = 1 to dim(n);
end;
run;
Similar you could declare an additional array of _character_ and no macro is needed at all.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.