Help using Base SAS procedures

how to define and use arrays in macro?

Frequent Contributor
Posts: 89

how to define and use arrays in macro?

Dear All
I have searched on internet about Macro arrays. But the results are rather confusing to me. So I want to ask the question here by using an example.

Suppose I have following codes:

data Test;
input OldVar1 OldVar2 OldVar3;
1 2 3

%macro GetVarNames(dsn);

%local dsid rc;
%global Cnt ArrayX; /*See my question in following*/

%let dsid=%sysfunc(open(&dsn)); /* Open data set */
%let Cnt=%sysfunc(attrn(&dsid,nvars)); /* get the number of variables in the dataset*/

%do i = 1 %to &Cnt;
%let ArrayX=%sysfunc(varname(&dsid,&i)); /*See my question in following*/

%let rc=%sysfunc(close(&dsid)); /* Close the data set */


What I want to do is: I want to define a macro array ArrayX(Cnt) of which the dimension will be defined by Cnt so that, according to the above example, ArrayX(1) = OldVar1, ArrayX(2) = OldVar2, ArrayX(3) = OldVar3.

How should I do this?
Super Contributor
Super Contributor
Posts: 3,174

Re: how to define and use arrays in macro?

Within your macro %DO/%END loop, you are overlaying the current value of &ArrayX which can be observed by adding OPTIONS MLOGIC; to your code. So, your %LET statement needs to address this deficiency by concatenating &ArrayX ahead (or as you prefer) in the %LET statement within the loop.

Scott Barry
SBBWorks, Inc.
Frequent Contributor
Posts: 89

Re: how to define and use arrays in macro?

Thank you for replying. Actually that's the problem I am facing: I don't know how to declare macro array.

I tried to change the code to:

%macro GetVarNames(dsn);

%local dsid rc;
%global Cnt;
%Array ArrayX(&Cnt); /*the change I made*/

%let dsid=%sysfunc(open(&dsn));
%let Cnt=%sysfunc(attrn(&dsid,nvars));

%do i = 1 %to &Cnt;
%let ArrayX(&i)=%sysfunc(varname(&dsid,&i)); /*the change I made*/

%let rc=%sysfunc(close(&dsid));


However, the code does not work. Could you please recommend a method to make the code work? Thanks.
Posts: 8,743

Re: how to define and use arrays in macro?

There is really no such thing as a macro array. There are only numbered macro variables that you can loop through with a %DO statement in a macro program.

Sometimes, folks call numbered macro variable a macro array, but it really isn't. And I think a few folks have written their own %ARRAY macro programs to simulate array processing. But if you look in the macro language documentation, you will not see an %ARRAY statement in the Macro language:

SAS Log for program and output:
412 ** make some numbered macro variables;
413 %let muppet1 = Kermit;
414 %let show1 = Sesame Street;
416 %let muppet2 = Wembley;
417 %let show2 = Fraggle Rock;
419 %let muppet3 = Fozzie;
420 %let show3 = Muppet Show;
422 %let numcnt = 3;
424 %macro showmupp;
427 %do i = 1 %to &numcnt;
429 %put i is: &i ~~ Muppet character &&muppet&i was on &&show&i ;
430 %let newvar&i = M:&&muppet&i and S:&&show&i;
431 %put Macro variable newvar&i value is: &&newvar&i;
432 %put ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~;
433 %end;
435 %mend showmupp;
437 %showmupp;
i is: 1 ~~ Muppet character Kermit was on Sesame Street
Macro variable newvar1 value is: M:Kermit and SSmiley Frustratedesame Street
~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~
i is: 2 ~~ Muppet character Wembley was on Fraggle Rock
Macro variable newvar2 value is: M:Wembley and S:Fraggle Rock
~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~
i is: 3 ~~ Muppet character Fozzie was on Muppet Show
Macro variable newvar3 value is: M:Fozzie and S:Muppet Show
~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~

Frequent Contributor
Posts: 89

Re: how to define and use arrays in macro?

thank you very much for clarification.
Valued Guide
Posts: 632

Re: how to define and use arrays in macro?

Ask a Question
Discussion stats
  • 5 replies
  • 4 in conversation