BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
weg
Obsidian | Level 7 weg
Obsidian | Level 7

edit--  I edited my question as my first attempt was very unclear.  Hopefully this is better.

 

I have a variable number of  macro variables class1 ... classN that store the names of variables I am using in my macro. I am trying to store the variable names as text in the array so I can loop over them later in some do statements   (I also need to do more things on the in macro step which is why they are macro variables in the first place).

 

I want to put into an array as text strings.  I want to do something like this:

 

array classname[&nclass] $32 (
					 "&class1" "&class2" ... "&classN"
				);

say I had class1=red, class2 = green class3=blue

then I want to have

array classname[3] ("red" "green" "blue");

 

I wanted to stick something like this in but I cant get it to come together

%do j=1 %to &nclass; %str("&&class&j"); %end;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Your original code was almost there, just remove the extra semi-colon (and the unneeded macro quoting).

%let v1=A1;
%let v2=B2;
%let v3=B3;
%let n=3;
array names [&n] $32 _temporary_ (
%do i=1 %to &n;
  "&&v&i"
%end;
);

But if your original input is the list of variable names then just keep the list in one macro variable. Then they are easier to use in SAS code.  It is not hard to convert it to a quoted list without looping.

%let mylist=A1 B2 B3 ;
%let n=%sysfunc(countw(&varlist,%str( )));
data want;
  set have;
  array values &varlist;
  array names [&n] _temporary_ ("%sysfunc(tranwrd(&varlist,%str( )," "))");
  ... logic here ...
run;

And it is easier to pass into other macros:

%mymacro(varlist=&mylist);

View solution in original post

8 REPLIES 8
LeonidBatkhan
Lapis Lazuli | Level 10

Hi weg,

You don't need those quotes around variable names comprising an array, and you don't need to put the in ( ). I think what you need to define your array is this:

options symbolgen;
%macro a;
   %let class1 = ABC;
   %let class2 = BCD;
   %let class3 = CDE;
   %let nclass = 3;

   data a;
      array classname[&nclass] $32 
         %do j=1 %to &nclass; &&class&j %end;
      ;
   run;
%mend a;
%a

Hope this helps.

Patrick
Opal | Level 21

Not sure that I fully understand what you have and what you need.

Does below do what you're after? If not then what's the different to your "have and want"?

options mprint;
%let nclass=3;
%let class1=age;
%let class2=height;
%let class3=weight;

%macro doit();
  %do j=1 %to &nclass; 
    &&class&j 
  %end;
%mend;

data test;
  set sashelp.class;
  array classname {*} %doit() ;
  do i=1 to dim(classname);
    put classname[i]=;
  end;
run;
weg
Obsidian | Level 7 weg
Obsidian | Level 7

edit - I realize I was not clear at all with what I asked. I will rewrite the origional question as there are several things I did that were just plain wrong and misleading.  I was tired and fed up and it made sense in my head when I wrote it.

 

 

So I want the name of the variable in the array, not the value of the variable.  So I can iterate on array[i] to get the variable name that I need so I can perform some action on it later.  The full macro that I am trying  write is kinda complicated and part of a massive sas program.  But basically it takes this as input "arrayname(var1,var2 ...,varn)" which is an array and the variables that it will eventually be partially filled with after I do some processing.  those variable names are what the class&i variables are, and I am trying to stick the names in an array so I can use them later. I was trying to generalize this one part of it because I will need to do much the same thing later when I need to create a variable number of do loops. The basic problem that I am hitting is  I have some function and I want to generate the input strings to go into it.

func ( 
/* generate text that goes inside including quotes */
)

in this case I am filling the array with the number of variables I pulled from that input string.  so say my program gets  testarray(red,blue,green), I parse the string, figure out I have 3 variables, and make the macros class1 =red class2=blue class3=green.    I then want to move this down to the data step so I make an array macarray[3] that has "red" "blue" "green" stored. 

 

 

ballardw
Super User

It may help to show exactly how you get/build your values to begin with.

 

It also may help to show how you intend to use that array with some worked examples.

 

Tom
Super User Tom
Super User

What SAS syntax are you trying to use the macro variables to create?

The code you posted is going to initialize the values of the variables named CLASSNAME1 to the value "CLASS1"?  If that is what you want you probably also need to retain CLASSNAME1 to CLASSNAME&nclass.

array classname[3] $32 ("class1" "class2" "class3");
retain classname1 - classname3;

Or perhaps just make it a temporary array, those are automatically retained.

array classname[3] $32 _temporary_ ("class1" "class2" "class3");

If you want the array reference CLASSNAME to point to the variables named CLASS1 .... then you want to generate this array statement instead (note you don't need to tell SAS how many they are since you are listing them).

array classname $32 class1 class2 class3;

Either way the main problem with your %DO loop is you are generating semi-colons in the middle. So you are generating :

array classname[3] $32 ("class1"; "class2"; "class3";);

which is not valid syntax for an ARRAY statement.

 

Note that if you want to have a list of variable names you only need one macro variable.

%let classlist=class1 class2 class3;
array classnames &classlist;

In general making a lot of macro variables with numeric suffixes is usually just going to make the code generation problem harder.

weg
Obsidian | Level 7 weg
Obsidian | Level 7

I edited my question as I was extremely unclear  and misleading with what I was asking.  But I see my main problem is still what you pointed out, that I am generating semicolons in the middle.   

 

so my main problem is if I don't know how many macro variables are created ahead of time, how do I make a list with their values as text?  say the macro &Vi had value Ai, how do I get the list "A1" "A2" ... "An" so I can insert it in my sas program.

 

 

Tom
Super User Tom
Super User

Your original code was almost there, just remove the extra semi-colon (and the unneeded macro quoting).

%let v1=A1;
%let v2=B2;
%let v3=B3;
%let n=3;
array names [&n] $32 _temporary_ (
%do i=1 %to &n;
  "&&v&i"
%end;
);

But if your original input is the list of variable names then just keep the list in one macro variable. Then they are easier to use in SAS code.  It is not hard to convert it to a quoted list without looping.

%let mylist=A1 B2 B3 ;
%let n=%sysfunc(countw(&varlist,%str( )));
data want;
  set have;
  array values &varlist;
  array names [&n] _temporary_ ("%sysfunc(tranwrd(&varlist,%str( )," "))");
  ... logic here ...
run;

And it is easier to pass into other macros:

%mymacro(varlist=&mylist);
ballardw
Super User

@weg wrote:

I edited my question as I was extremely unclear  and misleading with what I was asking.  But I see my main problem is still what you pointed out, that I am generating semicolons in the middle.   

 

so my main problem is if I don't know how many macro variables are created ahead of time, how do I make a list with their values as text?  say the macro &Vi had value Ai, how do I get the list "A1" "A2" ... "An" so I can insert it in my sas program.

 

 


As I asked earlier: how are you creating your possibly unknown number of macro variables?

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1342 views
  • 3 likes
  • 5 in conversation