Hi,
If I put all variables into array and use do loop in a macro,
How can i set up the end of the loop?
'%do i = 1 %to dim(array name);' did not work and
'call symput('n', dim(var))
%do i = 1 %to &n;' did not work too.
What should I do?
Thanks.
call symput('n', dim(var)) => call symput('n', dim(array name))
Sorry.
Macro statements and macro variable references are resolved before the data step runs, when the array does not yet exist.
To loop through an array, use the simple data step DO loop.
What do you want to achieve?
Mixing DATA step DO loops and macro variables requires very very clear understanding about when you want to use the DATA step code and where the macro language would fit in. As @Kurt_Bremser points out, you can't mix and match data step and macro language code as you are trying to do.
The usual advice, which you need to follow strictly here if you are going to make this work, is to write an example of working DATA step code without macro language for one situation where the DATA step will be used. Get that to work first. Once you have that working, adding in macro language ought to be easier. If you don't have the DATA step working, then adding macro language will never work. Trying to write this combination of macro language plus data step from scratch is simply not recommended.
So, please show us a working DATA step with no macro language for one situation. Only then can we move forward.
One serious consideration when working with SAS arrays is that all variables in a single array must be of the same type. You cannot mix numeric and character values. And depending on what you expect I would be extremely cautious about dropping all of either type into an array.
But SAS does make it easy to do such:
Data want;
set have;
array ___num _numeric_;
array ___char _character_;
<stuff with the two arrays, each separate>
do i= 1 to dim(___num);
<something that makes sense for numeric variable ___num[i]>
end;
run;
Another thing is that you have to provide an array name that does not duplicate any of the variables in the data set. Which is why the example above starts the array names with 3 underscores. I seldom have variables that start with 3 so is safe for my code but we have absolutely no idea what you have have for variable names.
Note that with this approach there is very likely no need for macro variables at all. Or define your problem more clearly.
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.