BookmarkSubscribeRSS Feed
opuyfb91
Fluorite | Level 6

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.

4 REPLIES 4
opuyfb91
Fluorite | Level 6

call symput('n', dim(var)) => call symput('n', dim(array name))

 

Sorry.

Kurt_Bremser
Super User

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?

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
ballardw
Super User

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: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1231 views
  • 0 likes
  • 4 in conversation