<?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 to perform calculation between using observations that fall between two dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-to-perform-calculation-between-using-observations-that/m-p/407599#M99343</link>
    <description>&lt;P&gt;Thanks RW9 for the reply.&lt;/P&gt;
&lt;P&gt;However, i have many dates and by variables so a macro is the only way to go. the code i use below is just a small sample, my data rurns from 1960 to 2017, and i need rolling regressions going back N months for each by variable (maybe around 10K of them) in each month.&lt;/P&gt;
&lt;P&gt;I have tried your suggestion below, but i still get errors messages..&lt;/P&gt;
&lt;P&gt;Many thanks again&lt;/P&gt;</description>
    <pubDate>Thu, 26 Oct 2017 10:51:04 GMT</pubDate>
    <dc:creator>costasRO</dc:creator>
    <dc:date>2017-10-26T10:51:04Z</dc:date>
    <item>
      <title>macro to perform calculation between using observations that fall between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-to-perform-calculation-between-using-observations-that/m-p/406545#M98998</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to write a macro that performs a calculation using observations from a master dataset that fall between two dates.&lt;/P&gt;
&lt;P&gt;I have an example below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%macro loop;&lt;/P&gt;
&lt;P&gt;/*These variables will be used to create the dates that i want my macro to loop over*/&lt;BR /&gt;%do yy = 1986 %to 1987;&amp;nbsp;&lt;BR /&gt; %do mm = 2 %to 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/*here i create the variables date1 and date2, which specify the length of the window that i want the calculation to occur*/&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;date2=intnx('month',mdy(&amp;amp;mm,1,&amp;amp;yy),0,'end');&lt;BR /&gt;date1= intnx('month', date2, -2,'end');&lt;BR /&gt;call symput('date1',date1);&lt;BR /&gt;call symput('date2',date2);&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/*test is my&amp;nbsp;master data set, in the example below i want to run a regression and keep some output using observations that fall between date1 and date2;&lt;/P&gt;
&lt;DIV class="line number27 index26 alt2"&gt;&lt;CODE class="sas functions"&gt;proc&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="sas plain"&gt;reg noprint&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;CODE class="sas functions"&gt;data&lt;/CODE&gt;&lt;CODE class="sas plain"&gt;=&lt;/CODE&gt;&lt;CODE class="sas variable"&gt;test&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="sas plain"&gt;outest=oreg_ds&amp;amp;yy&amp;amp;mm edf;&lt;/CODE&gt;&lt;/DIV&gt;
&lt;DIV class="line number28 index27 alt1"&gt;&lt;CODE class="sas spaces"&gt;&amp;nbsp;&amp;nbsp;&lt;/CODE&gt;&lt;CODE class="sas plain"&gt;where&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;CODE class="sas functions"&gt;date&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="sas plain"&gt;between&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;CODE class="sas variable"&gt;&amp;amp;date1&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="sas plain"&gt;and&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;CODE class="sas variable"&gt;&amp;amp;date2&lt;/CODE&gt;&lt;CODE class="sas plain"&gt;;&amp;nbsp; *Restricted&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;CODE class="sas functions"&gt;to&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="sas plain"&gt;DATE1- DATE2&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;CODE class="sas functions"&gt;data&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="sas plain"&gt;range&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;CODE class="sas functions"&gt;in&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="sas plain"&gt;the loop;&lt;/CODE&gt;&lt;/DIV&gt;
&lt;DIV class="line number29 index28 alt2"&gt;&lt;CODE class="sas spaces"&gt;&amp;nbsp;&amp;nbsp;&lt;/CODE&gt;&lt;CODE class="sas plain"&gt;model&amp;nbsp;y = x;&lt;/CODE&gt;&lt;/DIV&gt;
&lt;DIV class="line number30 index29 alt1"&gt;&lt;CODE class="sas spaces"&gt;&lt;/CODE&gt;&lt;CODE class="sas functions"&gt;run&lt;/CODE&gt;&lt;CODE class="sas plain"&gt;;&lt;/CODE&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%end;&lt;BR /&gt;%end;&lt;BR /&gt;%mend loop;&lt;BR /&gt;%loop;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; However, i keep getting errors of the type below, and i am not sure what is going on.&lt;/P&gt;
&lt;P&gt;Any help would be much appreciated!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NOTE: Line generated by the macro variable "DATE1".&lt;BR /&gt;1 9527&lt;BR /&gt; ----&lt;BR /&gt; 22&lt;BR /&gt; 200&lt;/P&gt;
&lt;P&gt;ERROR 22-322: Syntax error, expecting one of the following: a name, -, :, ;, _ALL_, _CHARACTER_,&lt;BR /&gt; _CHAR_, _NUMERIC_.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2017 14:28:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-to-perform-calculation-between-using-observations-that/m-p/406545#M98998</guid>
      <dc:creator>costasRO</dc:creator>
      <dc:date>2017-10-23T14:28:54Z</dc:date>
    </item>
    <item>
      <title>Re: macro to perform calculation between using observations that fall between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-to-perform-calculation-between-using-observations-that/m-p/406549#M98999</link>
      <description>Use options symbolgen mprint; to monitor macro behaviour.&lt;BR /&gt;Also, lift out the date logic outside and test it with hard coded macro variable values.</description>
      <pubDate>Mon, 23 Oct 2017 14:36:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-to-perform-calculation-between-using-observations-that/m-p/406549#M98999</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2017-10-23T14:36:13Z</dc:date>
    </item>
    <item>
      <title>Re: macro to perform calculation between using observations that fall between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-to-perform-calculation-between-using-observations-that/m-p/406557#M99002</link>
      <description>&lt;P&gt;Its because here:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;call symput('date1',date1);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You are putting the unformatted numeric (which is behind all dates) into the macro variable, use:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;call symput('date1',put(date1,date9.));&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Although I would really not recommend any of that approach at all.&amp;nbsp; There really are far better and simpler methodologies to attain what you want.&amp;nbsp; For instance consider adding a categorical variable to your dataset:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;data have;
  set master.have;
  if 1986 &amp;lt;= year(date) &amp;lt;=1987 and
    month(date) in (2,3) then cat="Year1";
  if ...;
run;

proc reg data=have...
  by cat;
  model...;
run;
 &lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;I am leaving now, so this was done quickly, but the premise is to manipulate your data to have the category, and then do a by group on the category varaible.&amp;nbsp; This is both simple Base SAS coding (easy to read, and maintain) and uses by grou processing in the reg procedure which is again simpler and easier to use, and will run much quicker.&amp;nbsp; You don't have to use if statements, you could put the do loop in the datastep and categorise the data any way you like, then by group it.&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2017 14:40:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-to-perform-calculation-between-using-observations-that/m-p/406557#M99002</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-10-23T14:40:51Z</dc:date>
    </item>
    <item>
      <title>Re: macro to perform calculation between using observations that fall between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-to-perform-calculation-between-using-observations-that/m-p/406635#M99032</link>
      <description>&lt;P&gt;Try sorting your data by the date variable and then use this:&lt;/P&gt;
&lt;PRE&gt;proc reg noprint data=test outest=oreg_dsedf;.
   by date;
   format date yyqd.;
   model y = x;.
run;.&lt;/PRE&gt;
&lt;P&gt;Which will create one set of output for each calendar quarter. Your Outest data set will also have one record fore each quarter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At least I think that's what you are attempting to do.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2017 16:58:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-to-perform-calculation-between-using-observations-that/m-p/406635#M99032</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-10-23T16:58:23Z</dc:date>
    </item>
    <item>
      <title>Re: macro to perform calculation between using observations that fall between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-to-perform-calculation-between-using-observations-that/m-p/407599#M99343</link>
      <description>&lt;P&gt;Thanks RW9 for the reply.&lt;/P&gt;
&lt;P&gt;However, i have many dates and by variables so a macro is the only way to go. the code i use below is just a small sample, my data rurns from 1960 to 2017, and i need rolling regressions going back N months for each by variable (maybe around 10K of them) in each month.&lt;/P&gt;
&lt;P&gt;I have tried your suggestion below, but i still get errors messages..&lt;/P&gt;
&lt;P&gt;Many thanks again&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2017 10:51:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-to-perform-calculation-between-using-observations-that/m-p/407599#M99343</guid>
      <dc:creator>costasRO</dc:creator>
      <dc:date>2017-10-26T10:51:04Z</dc:date>
    </item>
  </channel>
</rss>

