<?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: Loop through months in macro in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528324#M32884</link>
    <description>&lt;P&gt;First of all, when using date values for anything but display in a macro variable, it is best to not have them formatted. See Maxim 28.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	start_date=intnx('month','01AUG18'd,0,'B');
	end_date=intnx('month','31AUG18'd,0,'E');
	end_date1=intnx('month','31AUG18'd,-1,'E');
	call symputx('start_date',start_date);
	call symputx('end_date',end_date);
	call symputx('end_date1',end_date1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;symputx will take care of the numeric-to-character conversion in a graceful way (as opposed to symput).&lt;/P&gt;
&lt;P&gt;With the raw values, it is quite easy to create a macro loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call symputx('start_date','01aug2018'd);
call symputx('end_date','01jan2010'd);
run;

%macro mymac;
%let curr_date=&amp;amp;start_date;
%do %until (&amp;amp;curr_date &amp;lt; &amp;amp;end_date);
  /* your code in here */
  %let curr_date = %sysfunc(intnx(month,&amp;amp;curr_date,-1,b));
%end;
%mend;

%mymac&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 18 Jan 2019 14:57:54 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-01-18T14:57:54Z</dc:date>
    <item>
      <title>Loop through months in macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528319#M32883</link>
      <description>&lt;P&gt;Folks,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've written the following piece of code;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;

	start_date=put(intnx('month','01AUG18'd,0,'B'),date9.);
	end_date=put(intnx('month','31AUG18'd,0,'E'),date9.);
	end_date1=put(intnx('month','31AUG18'd,-1,'E'),date9.);
	call symputx('start_date',start_date);
	call symputx('end_date',end_date);
	call symputx('end_date1',end_date1);
run;

%put &amp;amp;=start_date &amp;amp;=end_date &amp;amp;=end_date1;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As you can see for my analysis my start_date is the first day of August my end_date of August and my other end_date1 is the last day of July.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I need to do now is put this into a macro so I loop back through previous months to this effect.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So basically the macro runs and carries out various datasetps and outputs results for start_date=1st of August end_date=last day of August and end_date1=last day of July.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then it loops through again for when the start_date=1 of July end_date=last day of July and end_date1=last day of June.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need this process to continue until January 2010.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would anyone know how I could include something like this in a macro, please?&lt;/P&gt;</description>
      <pubDate>Fri, 18 Jan 2019 14:45:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528319#M32883</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2019-01-18T14:45:09Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through months in macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528324#M32884</link>
      <description>&lt;P&gt;First of all, when using date values for anything but display in a macro variable, it is best to not have them formatted. See Maxim 28.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	start_date=intnx('month','01AUG18'd,0,'B');
	end_date=intnx('month','31AUG18'd,0,'E');
	end_date1=intnx('month','31AUG18'd,-1,'E');
	call symputx('start_date',start_date);
	call symputx('end_date',end_date);
	call symputx('end_date1',end_date1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;symputx will take care of the numeric-to-character conversion in a graceful way (as opposed to symput).&lt;/P&gt;
&lt;P&gt;With the raw values, it is quite easy to create a macro loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call symputx('start_date','01aug2018'd);
call symputx('end_date','01jan2010'd);
run;

%macro mymac;
%let curr_date=&amp;amp;start_date;
%do %until (&amp;amp;curr_date &amp;lt; &amp;amp;end_date);
  /* your code in here */
  %let curr_date = %sysfunc(intnx(month,&amp;amp;curr_date,-1,b));
%end;
%mend;

%mymac&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Jan 2019 14:57:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528324#M32884</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-18T14:57:54Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through months in macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528325#M32885</link>
      <description>&lt;P&gt;Let's build on what you have already.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop_mon;

   %local n_mon k;
   %let n_mon = %sysfunc(intck(month, "15Jan2010"d, "15Aug2010"d);
   %do k = 0 %to n_mon;
      ** your code plus analysis code goes here;
   %end;

%mend loop_mon;
%loop_mon&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So what does it mean when I say "** your code plus analysis code goes here?&amp;nbsp; Here's what gets inserted at that point:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; _null_&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

	start_date&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;put&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;intnx&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'month'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token datetime number"&gt;'01AUG18'd&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;FONT color="#0000FF"&gt;-&amp;amp;k&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'B'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;date9&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	end_date&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;put&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;intnx&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'month'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token datetime number"&gt;'31AUG18'd&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;FONT color="#0000FF"&gt;-&amp;amp;k&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'E'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;date9&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	end_date1&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;put&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;intnx&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'month'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token datetime number"&gt;'31AUG18'd&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;-1&lt;FONT color="#0000FF"&gt;-&amp;amp;k&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'E'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;date9&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	call &lt;SPAN class="token function"&gt;symputx&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'start_date'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;start_date&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	call &lt;SPAN class="token function"&gt;symputx&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'end_date'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;end_date&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	call &lt;SPAN class="token function"&gt;symputx&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'end_date1'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;end_date1&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

&lt;SPAN class="token macrostatement"&gt;%put&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;start_date &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;end_date &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;end_date1&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;BR /&gt;*** plus the analysis code (that you haven't shown yet) for this set of dates;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Jan 2019 14:59:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528325#M32885</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-01-18T14:59:28Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through months in macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528331#M32886</link>
      <description>&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm running into some error messages here. Does it relate to the piece of code here?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: Line generated by the macro variable "START_DATE".
379         "21397
            ______
            77
ERROR: Invalid date/time/datetime constant "21397"d.
ERROR 77-185: Invalid number conversion on "21397"d.

NOTE: Line generated by the macro variable "END_DATE".
379        "18263
           ______&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Subset for your lookup month*/
data august;
set testa;
 if "&amp;amp;start_date"d &amp;lt;= dt_filed &amp;lt;="&amp;amp;end_date"d then result="match";
 if result='match' then output august;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Jan 2019 15:15:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528331#M32886</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2019-01-18T15:15:19Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through months in macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528333#M32887</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/116786"&gt;@Sean_OConnor&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Kurt,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm running into some error messages here. Does it relate to the piece of code here?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: Line generated by the macro variable "START_DATE".
379         "21397
            ______
            77
ERROR: Invalid date/time/datetime constant "21397"d.
ERROR 77-185: Invalid number conversion on "21397"d.

NOTE: Line generated by the macro variable "END_DATE".
379        "18263
           ______&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Subset for your lookup month*/
data august;
set testa;
 if "&amp;amp;start_date"d &amp;lt;= dt_filed &amp;lt;="&amp;amp;end_date"d then result="match";
 if result='match' then output august;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Since you have raw numbers in the macro variables, you can use those directly:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data august;
set testa;
 if &amp;amp;start_date &amp;lt;= dt_filed &amp;lt;= &amp;amp;end_date then result="match";
 if result='match' then output august;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Jan 2019 15:18:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528333#M32887</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-18T15:18:07Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through months in macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528353#M32889</link>
      <description>&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks for your input. Unfortunately I'm still having some issues.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Part of may data steps is that we subset the dataset based on months.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	start_date=intnx('month','01AUG18'd,0,'B');
	end_date=intnx('month','31AUG18'd,0,'E');
	end_date1=intnx('month','31AUG18'd,-1,'E');
	call symputx('start_date',start_date);
	call symputx('end_date',end_date);
	call symputx('end_date1',end_date1);
run;

%put &amp;amp;=start_date &amp;amp;=end_date &amp;amp;=end_date1;

data _null_;
call symputx('start_date','01aug2018'd);
call symputx('end_date','01jul2018'd);
run;




%macro mymac;
%let curr_date=&amp;amp;start_date;

%do %until (&amp;amp;curr_date &amp;lt; &amp;amp;end_date);
  /* your code in here */

data testa;
set a.analysis_dataset;
run;

/*Subset for your lookup month*/
data august;
set testa;
 if &amp;amp;start_date &amp;lt;= dt_filed &amp;lt;=&amp;amp;end_date then result="match";
 if result='match' then output august;
run;

  %let curr_date = %sysfunc(intnx(month,&amp;amp;curr_date,-1,b));
%end;
%mend;

%mymac&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When it gets to the step to create the august dataset we're getting an issue&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: Format $CRS_CYN was not found or could not be loaded.
NOTE: There were 529429 observations read from the data set A.ANALYSIS_DATASET.
NOTE: The data set WORK.TESTA has 529429 observations and 167 variables.
NOTE: DATA statement used (Total process time):
      real time           15.86 seconds
      cpu time            4.25 seconds
      


NOTE: Format $CRS_CYN was not found or could not be loaded.
NOTE: There were 529429 observations read from the data set WORK.TESTA.
NOTE: The data set WORK.AUGUST has 0 observations and 168 variables.
NOTE: DATA statement used (Total process time):
      real time           1.42 seconds
      cpu time            1.40 seconds&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The variable dt_filed is a date variable with the following format -&amp;nbsp;04/01/2010&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have you any advise, please?&lt;/P&gt;</description>
      <pubDate>Fri, 18 Jan 2019 16:04:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528353#M32889</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2019-01-18T16:04:38Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through months in macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528704#M32890</link>
      <description>&lt;P&gt;Remove that step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call symputx('start_date','01aug2018'd);
call symputx('end_date','01jul2018'd);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as it overwrites the values set in the previous step with hardcoded ones that won't work in&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;amp;start_date &amp;lt;= dt_filed &amp;lt;=&amp;amp;end_date then result="match";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as the hardcoded end_date is smaller than the hardcoded start_date, so the condition MUST be false.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jan 2019 09:22:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528704#M32890</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-21T09:22:44Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through months in macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528782#M32891</link>
      <description>&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help so far.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To be honest I'm still a bit lost about all of this. I've put up some of my code to give a better idea what I'm currently doing and what I'm trying to do.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Manually adding your Rules based categories*/

%let start_date='01nov2017'd; *First day of analysis month;
%let end_date='31dec2018'd; * Last day of analysis month;
%let end_date1='30nov2017'd; *Last day of lookback month month previous to above;


/*Copy of your dataset*/

data testa;
set a.analysis_dataset;
run;

/*Subset for your lookup month*/
data subset;
set testa;
 if &amp;amp;start_date &amp;lt;= dt_filed &amp;lt;=&amp;amp;end_date then result="match";
 if result='match' then output subset;
run;

data month;
set subset ;

	if substr(id,1,1) in ('X') then
		delete;

	if  dc_type_participant='Purchaser' then 
		output month;
run;

data month_t;
	length type_of_buyer classification $50.;
	set month;
	if dc_res_purch_info='First Time Buyer Owner-Occupier' then
		type_of_buyer ='First Time Buyer';
	else if dc_res_purch_info ne 'First Time Buyer Owner-Occupier' then type_of_buyer='Previous Residential Home Owner';
run;

proc sort data=month_t; by id;

*Create your previous months data which omits the latest month again only looking at
those who purchased a house previously and a res one;
data previous_t;
	set a.full_period;
where dt_filed between '01JAN2010'd and &amp;amp;end_date1;

	if substr(id,1,1) in ('X') then
		delete;

	if  dc_type_participant='Purchaser' then
		output previous_t;
run;

proc sort data=previous_t(keep=id);
	by id;

	*Compute the number of times someone has appeared previously;
proc summary data=previous_t sum;
	by id;
	output out=appearances_buyer;
run;

data appearances_buyer(drop=_type_);
set appearances_buyer;
rename _freq_ = estamp_buyer;run;

proc sort data=appearances_buyer;
	by id;
run;

data previous_estamp;
	merge month_t(in=a) appearances_buyer(in=b);
	by id;

	if a;

		array change _numeric_;

	do over change;
		if change=. then
			change=0;
	end;
run;

data sole_&amp;amp;start_date;
	set previous_estamp;

	if estamp_buyer&amp;gt;=1 then
		first_time=1;
	else first_time=0;
if first_time=1 then classification='Previous Residential Home Owner';
if first_time=0 then classification='First Time Buyer of Residential Property';
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So currently, I'm changing the months at the top of the program but as discussed I would like to write a macro which would do this automatically and create a dataset at the very end.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hopefully this makes things a little clearer to understand.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jan 2019 15:15:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528782#M32891</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2019-01-21T15:15:58Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through months in macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528790#M32892</link>
      <description>&lt;P&gt;This creates your dates from one set of year/month dynamically:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let year=2017;
%let month=11;

data _null_;
start_date = mdy(&amp;amp;month,1,&amp;amp;year);
end_date = intnx('month',start_date,13,'e');
end_date1 = intnx('month',start_date,0,'e');
call symputx('start_date',start_date,'g');
call symputx('end_date',end_date,'g');
call symputx('end_date1',end_date1,'g');
run;

/* this is just for testing/verifying */

data test;
start_date = &amp;amp;start_date;
end_date = &amp;amp;end_date;
end_date1 = &amp;amp;end_date1;
format _numeric_ yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Look at the dataset, and you'll see the same date values (although in a different format for human consumption) you created with&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let start_date='01nov2017'd; *First day of analysis month;
%let end_date='31dec2018'd; * Last day of analysis month;
%let end_date1='30nov2017'd; *Last day of lookback month month previous to above;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Jan 2019 15:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528790#M32892</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-21T15:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through months in macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528803#M32893</link>
      <description>&lt;P&gt;Please clarify your rules for the relationship between the dates?&lt;/P&gt;
&lt;P&gt;It looks like you want to treat START_DATE as the input and derive the other two from that?&lt;/P&gt;
&lt;P&gt;Then you want to loop changing START_DATE from starting at August 2018 by stepping back one month until start_date is January 2010?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So set a range of start dates. Then use an interative %DO loop to generate an offset in months over that range.&amp;nbsp; Use that offset and the range to calculate new START_DATE. Use the START_DATE to calculate the other two dates.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let range_high='01AUG2018'd;
%let range_low='01JAN2010'd ;

%do offset=0 %to %sysfunc(intck(month,&amp;amp;range_high,&amp;amp;range_low)) %by -1;
  %let start_date=%sysfunc(intnx(month,&amp;amp;range_high,&amp;amp;offset,b));
  %let end_date=%sysfunc(intnx(month,&amp;amp;start_date,0,e));
  %let end_date1=%sysfunc(intnx(month,&amp;amp;start_date,-1,e));
  
  ... your code using START_DATE END_DATE and END_DATE1 ...
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that these %LET statements will just set START_DATE etc to the actual number of days that SAS uses to store a date value.&amp;nbsp; You can use that in your calculations.&amp;nbsp; But if you want to display if for yourself in a way that is human readable then you will need to apply a date style format to it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So for example here is a trivial macro that implements this loop and then just prints the generate macro variables values so you can see.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;677   %macro test(range_high,range_low);
678   %local offset start_date end_date end_date1 ;
679   %do offset=0 %to %sysfunc(intck(month,&amp;amp;range_high,&amp;amp;range_low)) %by -1;
680     %let start_date=%sysfunc(intnx(month,&amp;amp;range_high,&amp;amp;offset,b));
681     %let end_date=%sysfunc(intnx(month,&amp;amp;start_date,0,e));
682     %let end_date1=%sysfunc(intnx(month,&amp;amp;start_date,-1,e));
683
684     %put %sysfunc(putn(&amp;amp;start_date,date9.)) &amp;amp;=offset &amp;amp;=start_date &amp;amp;=end_date &amp;amp;=end_date1 ;
685
686   %end;
687   %mend test;
688
689   %test('01AUG2018'd,'01JUN2018'd) ;
01AUG2018 OFFSET=0 START_DATE=21397 END_DATE=21427 END_DATE1=21396
01JUL2018 OFFSET=-1 START_DATE=21366 END_DATE=21396 END_DATE1=21365
01JUN2018 OFFSET=-2 START_DATE=21336 END_DATE=21365 END_DATE1=21335

&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Jan 2019 16:18:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/528803#M32893</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-01-21T16:18:30Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through months in macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/529063#M32899</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This appears to be doing the trick.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I just have one question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As this macro will be creating a dataset for each given month is it possible to include a piece of code that would create a distinct dataset rather than writing over each additional one?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i= 1......n;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data output_&amp;amp;i;&lt;/P&gt;&lt;P&gt;set month;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jan 2019 14:54:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/529063#M32899</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2019-01-22T14:54:11Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through months in macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/530289#M32917</link>
      <description>&lt;P&gt;Using&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let range_high='01AUG2018'd;
%let range_low='01JAN2010'd ;

%do offset=0 %to %sysfunc(intck(month,&amp;amp;range_high,&amp;amp;range_low)) %by -1;
  %let start_date=%sysfunc(intnx(month,&amp;amp;range_high,&amp;amp;offset,b));
  %let end_date=%sysfunc(intnx(month,&amp;amp;start_date,0,e));
  %let end_date1=%sysfunc(intnx(month,&amp;amp;start_date,-1,e));
  %let dataset_suffix = %eval(&amp;amp;offset * -1);
  
  ... your code using START_DATE END_DATE and END_DATE1 ...

  data MyTable&amp;amp;dataset_suffix;
  .....

%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 26 Jan 2019 01:01:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Loop-through-months-in-macro/m-p/530289#M32917</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-01-26T01:01:14Z</dc:date>
    </item>
  </channel>
</rss>

