<?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: Macro variables within loop in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166980#M43249</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;could it is possible to run the loop for 2013 upto 12 months and then subset the data where date is less than month 3 and year 2013.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jag&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 19 Aug 2014 10:29:29 GMT</pubDate>
    <dc:creator>Jagadishkatam</dc:creator>
    <dc:date>2014-08-19T10:29:29Z</dc:date>
    <item>
      <title>Macro variables within loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166979#M43248</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I define a macro variable at the top of my sas program as %let enddate = 20130328; and I want to reference it in a loop later on in the program within a datastep.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a loop that runs from year 2008 to 2013 and months 1 to 12 but i want the loop to cutoff at month 3 for year 2013; I was going allong this lines:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%Do y = 2008 %to 2013;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If &amp;amp;Y = 2013 then X = month(&amp;amp;enddate) ;&lt;/P&gt;&lt;P&gt;else X = 12;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%do M = 1 %to X;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The program works perfectly when I hawve %do M = 1 %to 12;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 10:24:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166979#M43248</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2014-08-19T10:24:23Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables within loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166980#M43249</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;could it is possible to run the loop for 2013 upto 12 months and then subset the data where date is less than month 3 and year 2013.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jag&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 10:29:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166980#M43249</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2014-08-19T10:29:29Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables within loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166981#M43250</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The problems come from SAS not knowing 20130328 is a date.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;With pure macro code you could do this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro thing;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %let enddate="28MAR2013"d;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; %do y= 2008 %to 2013;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if &amp;amp;Y = 2013 %then&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let X = %sysfunc(month(&amp;amp;enddate));&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %else&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let X = 12;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do M = 1 %to &amp;amp;X;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %put y=&amp;amp;y m=&amp;amp;m;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;%thing;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 10:33:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166981#M43250</guid>
      <dc:creator>MichaelPearce</dc:creator>
      <dc:date>2014-08-19T10:33:49Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables within loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166982#M43251</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Something like the below, just a matter of setting your loops up and having a conditional within, you could also do this with a do until, do while, or even if your really enjoying it, a do loop with months as your step value.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let date=20130328;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do I=2008 to input(substr(&amp;amp;date.,1,4),best.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do I=to 12;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if input(substr(&amp;amp;date.,1,4),best.) &amp;lt; I or (input(substr(&amp;amp;date.,1,4),best.)=I and input(substr(&amp;amp;date.,5,2),best.) &amp;lt;= I then&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; YM=put(I,4.)||"-"||put(d,z2.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 10:39:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166982#M43251</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2014-08-19T10:39:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables within loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166983#M43252</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Michael, this is perfect but I need someway of converting 20130328 to a sas date. The program defines the date as an 8 digit number at the start (and unfortuantely I can't change this) but is there a way to change this in your code above, like %let date = input(substr(&amp;amp;enddate,1,6), best.);&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 10:54:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166983#M43252</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2014-08-19T10:54:24Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables within loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166984#M43253</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ah that is easy, while SAS will not assume 20130328 is a date, it can convert it from anything to anything.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro thing;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %let enddate=20130328;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %let endDt=%sysfunc(inputn(&amp;amp;enddate,yymmdd8.));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; %do y= 2008 %to 2013;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if &amp;amp;Y = 2013 %then&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let X = %sysfunc(month(&amp;amp;enddt));&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %else&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let X = 12;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do M = 1 %to &amp;amp;X;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %put y=&amp;amp;y m=&amp;amp;m;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;%thing;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 11:41:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166984#M43253</guid>
      <dc:creator>MichaelPearce</dc:creator>
      <dc:date>2014-08-19T11:41:38Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables within loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166985#M43254</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;BR /&gt;Thanks Michael, when i reference enddt outside a data step i get an error saying "argument 1 to function year referenced by the sysfunc macro function is not a number"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Do you have to do somthing different when it's outside the data step?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 12:03:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166985#M43254</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2014-08-19T12:03:15Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables within loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166986#M43255</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Well none of that is in a data step, it's all macro, if you want it all to run in a data step, it gets easier to read ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let enddate=20130328;&lt;/P&gt;&lt;P&gt;data _null_;&lt;BR /&gt;&amp;nbsp; endDt = input("&amp;amp;enddate",yymmdd8.);&lt;/P&gt;&lt;P&gt;&amp;nbsp; do y= 2008 to 2013;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if Y = 2013 then&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; X = month(enddt);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; X = 12;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; do M = 1 to X;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; put y= m=;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;BR /&gt;&amp;nbsp; end;&lt;BR /&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 12:32:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166986#M43255</guid>
      <dc:creator>MichaelPearce</dc:creator>
      <dc:date>2014-08-19T12:32:33Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables within loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166987#M43256</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The enddate variable is defined outside the macro at the start of the whole program - you're code above works perfectly when i use it within a large datastep - i think i need to use your code since the macro variable &amp;amp;enddate was defined outside the datastep.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm still not sure why I'm getting the error message above?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 12:41:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166987#M43256</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2014-08-19T12:41:05Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables within loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166988#M43257</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It's probably the quotes, in a data step, they are needed, in macro code they are not.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 12:49:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166988#M43257</guid>
      <dc:creator>MichaelPearce</dc:creator>
      <dc:date>2014-08-19T12:49:10Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables within loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166989#M43258</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;To put it a different way, suppose I want to create a macro variable (without any other code involved) called year 1. The code %let year1 = %sysfunc(year("13JUL2013"d)); gives me the same error, that is:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;"Argument 1 to function YEAR referenced by ..."&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 13:17:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166989#M43258</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2014-08-19T13:17:19Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables within loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166990#M43259</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Are you mixing macro code and data step variables?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Because when i run:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let year1 = %sysfunc(year("13JUL2013"d));&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It works .. Log:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;424&amp;nbsp; %let year1 = %sysfunc(year("13JUL2013"d));&lt;/P&gt;&lt;P&gt;425&amp;nbsp; %put Year One=&amp;amp;year1;&lt;/P&gt;&lt;P&gt;Year One=2013&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to create a macro variable from a field in your data step, you need to use call symput(), not %let.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;data _null_;&lt;BR /&gt;&amp;nbsp; dt = date();&lt;BR /&gt;&amp;nbsp; put dt=date9.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; call symput('YearOne',year(dt));&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%put Year One=&amp;amp;yearOne;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Aug 2014 13:53:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Macro-variables-within-loop/m-p/166990#M43259</guid>
      <dc:creator>MichaelPearce</dc:creator>
      <dc:date>2014-08-19T13:53:01Z</dc:date>
    </item>
  </channel>
</rss>

