<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: how to define and use arrays in macro? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/how-to-define-and-use-arrays-in-macro/m-p/72284#M20941</link>
    <description>Hi:&lt;BR /&gt;
  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.&lt;BR /&gt;
&lt;BR /&gt;
  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:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001071915.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001071915.htm&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001072448.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001072448.htm&lt;/A&gt;&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
SAS Log for program and output:&lt;BR /&gt;
412  ** make some numbered macro variables;&lt;BR /&gt;
413  %let muppet1 = Kermit;&lt;BR /&gt;
414  %let show1 = Sesame Street;&lt;BR /&gt;
415&lt;BR /&gt;
416  %let muppet2 = Wembley;&lt;BR /&gt;
417  %let show2 = Fraggle Rock;&lt;BR /&gt;
418&lt;BR /&gt;
419  %let muppet3 = Fozzie;&lt;BR /&gt;
420  %let show3 = Muppet Show;&lt;BR /&gt;
421&lt;BR /&gt;
422  %let numcnt = 3;&lt;BR /&gt;
423&lt;BR /&gt;
424  %macro showmupp;&lt;BR /&gt;
425&lt;BR /&gt;
426&lt;BR /&gt;
427    %do i = 1 %to &amp;amp;numcnt;&lt;BR /&gt;
428&lt;BR /&gt;
429       %put i is: &amp;amp;i ~~ Muppet character &amp;amp;&amp;amp;muppet&amp;amp;i was on &amp;amp;&amp;amp;show&amp;amp;i ;&lt;BR /&gt;
430       %let newvar&amp;amp;i  = M:&amp;amp;&amp;amp;muppet&amp;amp;i and S:&amp;amp;&amp;amp;show&amp;amp;i;&lt;BR /&gt;
431       %put Macro variable newvar&amp;amp;i value is: &amp;amp;&amp;amp;newvar&amp;amp;i;&lt;BR /&gt;
432       %put ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~;&lt;BR /&gt;
433    %end;&lt;BR /&gt;
434&lt;BR /&gt;
435  %mend showmupp;&lt;BR /&gt;
436&lt;BR /&gt;
437  %showmupp;&lt;BR /&gt;
i is: 1 ~~ Muppet character Kermit was on Sesame Street&lt;BR /&gt;
Macro variable newvar1 value is: M:Kermit and S:Sesame Street&lt;BR /&gt;
~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~&lt;BR /&gt;
i is: 2 ~~ Muppet character Wembley was on Fraggle Rock&lt;BR /&gt;
Macro variable newvar2 value is: M:Wembley and S:Fraggle Rock&lt;BR /&gt;
~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~&lt;BR /&gt;
i is: 3 ~~ Muppet character Fozzie was on Muppet Show&lt;BR /&gt;
Macro variable newvar3 value is: M:Fozzie and S:Muppet Show&lt;BR /&gt;
~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~&lt;BR /&gt;
&lt;BR /&gt;
[/pre]</description>
    <pubDate>Thu, 02 Jun 2011 22:05:38 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2011-06-02T22:05:38Z</dc:date>
    <item>
      <title>how to define and use arrays in macro?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-define-and-use-arrays-in-macro/m-p/72281#M20938</link>
      <description>Dear All&lt;BR /&gt;
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.&lt;BR /&gt;
&lt;BR /&gt;
Suppose I have following codes:&lt;BR /&gt;
&lt;BR /&gt;
data	Test;&lt;BR /&gt;
input	OldVar1 OldVar2 OldVar3;&lt;BR /&gt;
Cards;&lt;BR /&gt;
1 2 3&lt;BR /&gt;
;&lt;BR /&gt;
Run;&lt;BR /&gt;
&lt;BR /&gt;
%macro	GetVarNames(dsn);&lt;BR /&gt;
&lt;BR /&gt;
%local dsid rc;&lt;BR /&gt;
%global Cnt  ArrayX; /*See my question in following*/&lt;BR /&gt;
&lt;BR /&gt;
%let dsid=%sysfunc(open(&amp;amp;dsn));	      /* Open data set */&lt;BR /&gt;
%let Cnt=%sysfunc(attrn(&amp;amp;dsid,nvars));     /* get the number of variables in the dataset*/&lt;BR /&gt;
&lt;BR /&gt;
%do	i = 1	%to	&amp;amp;Cnt;&lt;BR /&gt;
	%let ArrayX=%sysfunc(varname(&amp;amp;dsid,&amp;amp;i));  /*See my question in following*/&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
%let rc=%sysfunc(close(&amp;amp;dsid));			/* Close the data set */&lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
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.&lt;BR /&gt;
&lt;BR /&gt;
How should I do this?</description>
      <pubDate>Thu, 02 Jun 2011 18:39:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-define-and-use-arrays-in-macro/m-p/72281#M20938</guid>
      <dc:creator>littlestone</dc:creator>
      <dc:date>2011-06-02T18:39:14Z</dc:date>
    </item>
    <item>
      <title>Re: how to define and use arrays in macro?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-define-and-use-arrays-in-macro/m-p/72282#M20939</link>
      <description>Within your macro %DO/%END loop, you are overlaying the current value of &amp;amp;ArrayX which can be observed by adding OPTIONS MLOGIC;   to your code.  So, your %LET statement needs to address this deficiency by concatenating &amp;amp;ArrayX ahead (or as you prefer) in the %LET statement within the loop.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 02 Jun 2011 20:25:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-define-and-use-arrays-in-macro/m-p/72282#M20939</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2011-06-02T20:25:01Z</dc:date>
    </item>
    <item>
      <title>Re: how to define and use arrays in macro?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-define-and-use-arrays-in-macro/m-p/72283#M20940</link>
      <description>Thank you for replying. Actually that's the problem I am facing: I don't know how to declare macro array.&lt;BR /&gt;
&lt;BR /&gt;
I tried to change the code to:&lt;BR /&gt;
&lt;BR /&gt;
%macro GetVarNames(dsn);&lt;BR /&gt;
&lt;BR /&gt;
%local dsid rc;&lt;BR /&gt;
%global Cnt; &lt;BR /&gt;
%Array ArrayX(&amp;amp;Cnt);             /*the change I made*/&lt;BR /&gt;
&lt;BR /&gt;
%let dsid=%sysfunc(open(&amp;amp;dsn)); &lt;BR /&gt;
%let Cnt=%sysfunc(attrn(&amp;amp;dsid,nvars)); &lt;BR /&gt;
&lt;BR /&gt;
%do i = 1 %to &amp;amp;Cnt;&lt;BR /&gt;
%let ArrayX(&amp;amp;i)=%sysfunc(varname(&amp;amp;dsid,&amp;amp;i)); /*the change I made*/&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
%let rc=%sysfunc(close(&amp;amp;dsid)); &lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
However, the code does not work. Could you please recommend a method to make the code work? Thanks.</description>
      <pubDate>Thu, 02 Jun 2011 20:37:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-define-and-use-arrays-in-macro/m-p/72283#M20940</guid>
      <dc:creator>littlestone</dc:creator>
      <dc:date>2011-06-02T20:37:19Z</dc:date>
    </item>
    <item>
      <title>Re: how to define and use arrays in macro?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-define-and-use-arrays-in-macro/m-p/72284#M20941</link>
      <description>Hi:&lt;BR /&gt;
  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.&lt;BR /&gt;
&lt;BR /&gt;
  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:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001071915.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001071915.htm&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001072448.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001072448.htm&lt;/A&gt;&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
SAS Log for program and output:&lt;BR /&gt;
412  ** make some numbered macro variables;&lt;BR /&gt;
413  %let muppet1 = Kermit;&lt;BR /&gt;
414  %let show1 = Sesame Street;&lt;BR /&gt;
415&lt;BR /&gt;
416  %let muppet2 = Wembley;&lt;BR /&gt;
417  %let show2 = Fraggle Rock;&lt;BR /&gt;
418&lt;BR /&gt;
419  %let muppet3 = Fozzie;&lt;BR /&gt;
420  %let show3 = Muppet Show;&lt;BR /&gt;
421&lt;BR /&gt;
422  %let numcnt = 3;&lt;BR /&gt;
423&lt;BR /&gt;
424  %macro showmupp;&lt;BR /&gt;
425&lt;BR /&gt;
426&lt;BR /&gt;
427    %do i = 1 %to &amp;amp;numcnt;&lt;BR /&gt;
428&lt;BR /&gt;
429       %put i is: &amp;amp;i ~~ Muppet character &amp;amp;&amp;amp;muppet&amp;amp;i was on &amp;amp;&amp;amp;show&amp;amp;i ;&lt;BR /&gt;
430       %let newvar&amp;amp;i  = M:&amp;amp;&amp;amp;muppet&amp;amp;i and S:&amp;amp;&amp;amp;show&amp;amp;i;&lt;BR /&gt;
431       %put Macro variable newvar&amp;amp;i value is: &amp;amp;&amp;amp;newvar&amp;amp;i;&lt;BR /&gt;
432       %put ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~;&lt;BR /&gt;
433    %end;&lt;BR /&gt;
434&lt;BR /&gt;
435  %mend showmupp;&lt;BR /&gt;
436&lt;BR /&gt;
437  %showmupp;&lt;BR /&gt;
i is: 1 ~~ Muppet character Kermit was on Sesame Street&lt;BR /&gt;
Macro variable newvar1 value is: M:Kermit and S:Sesame Street&lt;BR /&gt;
~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~&lt;BR /&gt;
i is: 2 ~~ Muppet character Wembley was on Fraggle Rock&lt;BR /&gt;
Macro variable newvar2 value is: M:Wembley and S:Fraggle Rock&lt;BR /&gt;
~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~&lt;BR /&gt;
i is: 3 ~~ Muppet character Fozzie was on Muppet Show&lt;BR /&gt;
Macro variable newvar3 value is: M:Fozzie and S:Muppet Show&lt;BR /&gt;
~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~&lt;BR /&gt;
&lt;BR /&gt;
[/pre]</description>
      <pubDate>Thu, 02 Jun 2011 22:05:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-define-and-use-arrays-in-macro/m-p/72284#M20941</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2011-06-02T22:05:38Z</dc:date>
    </item>
    <item>
      <title>Re: how to define and use arrays in macro?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-define-and-use-arrays-in-macro/m-p/72285#M20942</link>
      <description>thank you very much for clarification.</description>
      <pubDate>Fri, 03 Jun 2011 02:12:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-define-and-use-arrays-in-macro/m-p/72285#M20942</guid>
      <dc:creator>littlestone</dc:creator>
      <dc:date>2011-06-03T02:12:39Z</dc:date>
    </item>
    <item>
      <title>Re: how to define and use arrays in macro?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-define-and-use-arrays-in-macro/m-p/72286#M20943</link>
      <description>Ron Fehd has written several papers on list processing in the macro language.  a couple of starting points might be:&lt;BR /&gt;
&lt;BR /&gt;
&lt;A href="http://www.sascommunity.org/wiki/List_Processing_Basics_Creating_and_Using_Lists_of_Macro_Variables" target="_blank"&gt;http://www.sascommunity.org/wiki/List_Processing_Basics_Creating_and_Using_Lists_of_Macro_Variables&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
&lt;A href="http://www.lexjansen.com/pnwsug/2009/Fehd,%20Ron%20and%20Art%20Carpenter%20-%20List%20Processing%20Basics.pdf" target="_blank"&gt;http://www.lexjansen.com/pnwsug/2009/Fehd,%20Ron%20and%20Art%20Carpenter%20-%20List%20Processing%20Basics.pdf&lt;/A&gt;</description>
      <pubDate>Mon, 06 Jun 2011 05:50:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-define-and-use-arrays-in-macro/m-p/72286#M20943</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2011-06-06T05:50:51Z</dc:date>
    </item>
  </channel>
</rss>

