<?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 Using an array within a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-an-array-within-a-macro/m-p/130431#M26618</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm pretty new to macros in SAS and I'm stuck with a problem that seems like it should be pretty straightforward. I'm trying to read a set of excel sheets into SAS that all have a similar prefix in the filename but different suffixes, eg Temp 9-28-12, Temp 9-29-12, Temp 9-30-12. I wrote a simple macro that reads in an individual sheet:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%MACRO read_dates (date, outfile);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc import datafile="C:\Users\Ben\Documents\Time Series 2\Data\All Phoenix Temps\Phoenix Temp &amp;amp;date..csv" &lt;/P&gt;&lt;P&gt;DBMS=CSV Out=Phoenix.&amp;amp;outfile REPLACE;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%mend read_dates;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A sample call to this function would be:&lt;/P&gt;&lt;P&gt;%read_dates(9-23-13,test1);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've tested this and it works. However, I'm having difficulties when I try to read multiple sheets consecutively. I tried creating a macro with an array consisting of different dates, which I could use a do loop to iterate through. However, I kept getting errors so I created a 'setup' macro that calls the read_dates macro, which is in turn called from a data step. For now I'm leaving aside the problem of creating differently named sas datasets, that's why I'm just renaming each set 'test1'. The code follows:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro setup(dt);&lt;/P&gt;&lt;P&gt;&amp;nbsp; %read_dates(dt,test1)&lt;/P&gt;&lt;P&gt;%mend setup;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data dates1;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;array dates {17} $ d1-d17 ('9-23-13','9-24-13','9-25-13','9-26-13','9-27-13','9-28-13',&lt;/P&gt;&lt;P&gt;&amp;nbsp; '9-29-13','9-30-13','10-1-13', '10-2-13','10-3-13', '10-4-13', '10-5-13',&lt;/P&gt;&lt;P&gt;&amp;nbsp; '10-6-13', '10-7-13','10-8-13','10-9-13');&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do i=1 to 17;&lt;/P&gt;&lt;P&gt;&amp;nbsp; %setup(dates{i})&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I run this I get the error message: There was 1 Unenclosed DO Block. I'm not sure where this is coming from. I've tried making a number of modifications without any luck. Can someone point out where I am going wrong?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 31 Oct 2013 17:42:31 GMT</pubDate>
    <dc:creator>btkuhn</dc:creator>
    <dc:date>2013-10-31T17:42:31Z</dc:date>
    <item>
      <title>Using an array within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-an-array-within-a-macro/m-p/130431#M26618</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm pretty new to macros in SAS and I'm stuck with a problem that seems like it should be pretty straightforward. I'm trying to read a set of excel sheets into SAS that all have a similar prefix in the filename but different suffixes, eg Temp 9-28-12, Temp 9-29-12, Temp 9-30-12. I wrote a simple macro that reads in an individual sheet:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%MACRO read_dates (date, outfile);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc import datafile="C:\Users\Ben\Documents\Time Series 2\Data\All Phoenix Temps\Phoenix Temp &amp;amp;date..csv" &lt;/P&gt;&lt;P&gt;DBMS=CSV Out=Phoenix.&amp;amp;outfile REPLACE;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%mend read_dates;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A sample call to this function would be:&lt;/P&gt;&lt;P&gt;%read_dates(9-23-13,test1);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've tested this and it works. However, I'm having difficulties when I try to read multiple sheets consecutively. I tried creating a macro with an array consisting of different dates, which I could use a do loop to iterate through. However, I kept getting errors so I created a 'setup' macro that calls the read_dates macro, which is in turn called from a data step. For now I'm leaving aside the problem of creating differently named sas datasets, that's why I'm just renaming each set 'test1'. The code follows:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro setup(dt);&lt;/P&gt;&lt;P&gt;&amp;nbsp; %read_dates(dt,test1)&lt;/P&gt;&lt;P&gt;%mend setup;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data dates1;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;array dates {17} $ d1-d17 ('9-23-13','9-24-13','9-25-13','9-26-13','9-27-13','9-28-13',&lt;/P&gt;&lt;P&gt;&amp;nbsp; '9-29-13','9-30-13','10-1-13', '10-2-13','10-3-13', '10-4-13', '10-5-13',&lt;/P&gt;&lt;P&gt;&amp;nbsp; '10-6-13', '10-7-13','10-8-13','10-9-13');&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do i=1 to 17;&lt;/P&gt;&lt;P&gt;&amp;nbsp; %setup(dates{i})&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I run this I get the error message: There was 1 Unenclosed DO Block. I'm not sure where this is coming from. I've tried making a number of modifications without any luck. Can someone point out where I am going wrong?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Oct 2013 17:42:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-an-array-within-a-macro/m-p/130431#M26618</guid>
      <dc:creator>btkuhn</dc:creator>
      <dc:date>2013-10-31T17:42:31Z</dc:date>
    </item>
    <item>
      <title>Re: Using an array within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-an-array-within-a-macro/m-p/130432#M26619</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Generally speaking, macro code is resolved before data step code is executed, so the presence of a resolved proc import statement in the middle of the do loop in the data step will terminate the data step, giving you the error message.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Further, as the array does not exist at the time the macro is resolving you will just be passing the literal string "&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;dates{i}&lt;/SPAN&gt;" to the macro which is obviously not what you want.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;One approach you can take is to set up a macro variable holding a string of all of the dates, then in your setup macro function use a macro-do loop to loop through the dates invoking the read_dates macro function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I haven't got time to post sample code now, but it would be one way amongst many others I'm sure.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Oct 2013 18:31:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-an-array-within-a-macro/m-p/130432#M26619</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2013-10-31T18:31:59Z</dc:date>
    </item>
    <item>
      <title>Re: Using an array within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-an-array-within-a-macro/m-p/130433#M26620</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So you need some way to loop over the list of values.&lt;/P&gt;&lt;P&gt;For the method you started on to work the data step would need to generate macro calls that will execute after the data step finishes.&amp;nbsp; You can use CALL EXECUTE for that.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data dates1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;array dates (17) $10 d1-d17&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;('9-23-13','9-24-13','9-25-13','9-26-13','9-27-13','9-28-13'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;,'9-29-13','9-30-13','10-1-13','10-2-13','10-3-13','10-4-13'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;,'10-5-13','10-6-13', '10-7-13','10-8-13','10-9-13'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;do i=1 to 17;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; call execute(cats('%read_dates(',dates(i),',',cats('temp',i),')' ));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You could also do the looping over the list in macro code instead.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%macro read_date2(datelist,prefix);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%local i date ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%do i=1 %to %sysfunc(countw(&amp;amp;datelist,%str( )));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; %let date=%scan(&amp;amp;datelist,&amp;amp;i,%str( ));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; %read_dates(&amp;amp;date,&amp;amp;prefix.&amp;amp;i);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%mend read_date2 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%read_date2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;(9-23-13 9-24-13 9-25-13 9-26-13 9-27-13 9-28-13 9-29-13 9-30-13&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt; 10-1-13 10-2-13 10-3-13 10-4-13 10-5-13 10-6-13 10-7-13 10-8-13 10-9-13&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;,temp&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;);&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Oct 2013 18:52:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-an-array-within-a-macro/m-p/130433#M26620</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2013-10-31T18:52:57Z</dc:date>
    </item>
  </channel>
</rss>

