SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
A_M_
Fluorite | Level 6

Hi!

I am trying to build an array, where I define its elements through macrovariables.

It runs ok when I use references to macrovariables through 1 ampersand. However, it does not work when I try to use double ampersand macro variables to define the array elements (I am trying to go through a precreated  vertical macro variable list). The section of the code that doesn’t work and puzzles me is the following:

array y_vars_&a. (*) &&start_&a. -- &&end_&a.;

Does anyone know if there is something going on with && in arrays!? Or do you think it should actually work?

Thanks a lot for the help!

 

1 ACCEPTED SOLUTION

Accepted Solutions
A_M_
Fluorite | Level 6
Hey all,
Thanks a lot for the inputs!
I fixed it. The problem was elsewhere in the code..
Thanks again!

View solution in original post

5 REPLIES 5
Reeza
Super User

What does 'doesn't work' mean?

 

That code would be valid if it resolves to a valid SAS variable so what does it resolve to in the end?

 

Can you show the log with the MPRINT and SYMBOLGEN options turned on and include any error messages as well?

 


@A_M_ wrote:

Hi!

I am trying to build an array, where I define its elements through macrovariables.

It runs ok when I use references to macrovariables through 1 ampersand. However, it does not work when I try to use double ampersand macro variables to define the array elements (I am trying to go through a precreated  vertical macro variable list). The section of the code that doesn’t work and puzzles me is the following:

array y_vars_&a. (*) &&start_&a. -- &&end_&a.;

Does anyone know if there is something going on with && in arrays!? Or do you think it should actually work?

Thanks a lot for the help!

 


 

ballardw
Super User

Nothing to do with arrays. Macro variables only create text. How you attempt to use that created text is the issue.

For instance, when you use the double dash list builder as your array statement does then &&start_&a should resolve to either single variable or a list of multiple variables of the same type with the last one already existing in the data set followed by the resolution of &&end_&a. as a single variable that exists in the data set or starts a list of variable names where the first one is in the data set and every variable between the start and end must be of the same type, all numeric or all character. So you macro variables are extremely dependent on the order of variables in your data set.

 

So without knowing 1) the values of all the macro variables shown and 2) the contents of the data set this line of code is used with whether it may work or not cannot be determined.

 

Note that use of indirect referenced macro variables (i.e. the && or &&& or &&&& or more) can be extremely time consuming to debug and often means a different approach maybe more robust and understandable. Except for a demonstration in a small SAS training session in my organization and answering questions on this forum I don't think I've used an indirect reference more than twice in past 15 years.

 

And what prey tell is a "vertical macro variable list"?? One suspects an attempt to use macro variables in a complex way that is not supported by arrays.

Tom
Super User Tom
Super User

What is the SAS ARRAY statement you want to generate?

Is it something like:

array y_vars_1 XXX -- YYY ;

If so then the macro variables should have values like:

%let a=1;
%let start_1=XXX;
%let end_1=YYY;

For this code to generate the required ARRAY statement.

array y_vars_&a. &&start_&a. -- &&end_&a.;
A_M_
Fluorite | Level 6
Hey all,
Thanks a lot for the inputs!
I fixed it. The problem was elsewhere in the code..
Thanks again!
SASKiwi
PROC Star

@A_M_ - Please update your post as answered in that case.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1165 views
  • 3 likes
  • 5 in conversation