BookmarkSubscribeRSS Feed
littlestone
Fluorite | Level 6
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;
Cards;
1 2 3
;
Run;

%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*/
%end;

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

%mend;

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?
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
littlestone
Fluorite | Level 6
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*/
%end;

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

%mend;


However, the code does not work. Could you please recommend a method to make the code work? Thanks.
Cynthia_sas
SAS Super FREQ
Hi:
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:
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001071915.htm
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001072448.htm

cynthia
[pre]
SAS Log for program and output:
412 ** make some numbered macro variables;
413 %let muppet1 = Kermit;
414 %let show1 = Sesame Street;
415
416 %let muppet2 = Wembley;
417 %let show2 = Fraggle Rock;
418
419 %let muppet3 = Fozzie;
420 %let show3 = Muppet Show;
421
422 %let numcnt = 3;
423
424 %macro showmupp;
425
426
427 %do i = 1 %to &numcnt;
428
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;
434
435 %mend showmupp;
436
437 %showmupp;
i is: 1 ~~ Muppet character Kermit was on Sesame Street
Macro variable newvar1 value is: M:Kermit and S:Sesame 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
~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~

[/pre]
littlestone
Fluorite | Level 6
thank you very much for clarification.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1532 views
  • 0 likes
  • 4 in conversation