<?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: re: macro do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581841#M165415</link>
    <description>&lt;P&gt;No macros needed. This is a Cartesian join, or to phrase it another way, you want to test every possible combination of records in temp and have1, and keep the ones where datex is between startdate and enddate. This may take a very long time depending on the size of your input data set. I show how to increase the enddate by 1. I leave it up to you to modify this to take care of weekends.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data every_combination;
    set temp;
	do i=1 to n;
	    set have1 point=i nobs=n;
		if startdate&amp;lt;datex&amp;lt;enddate then do;
			enddate=enddate+1;
			output ;
		end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 16 Aug 2019 19:55:01 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-08-16T19:55:01Z</dc:date>
    <item>
      <title>re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581837#M165414</link>
      <description>&lt;P&gt;Hi...I am trying to get check each date in the temp dataset to see whether or not it falls between the StartDate and EndDate for each record in the have1 dataset. If it does, I would like to increase the EndDate by 1 and if this EndDate falls on the weekend, I would like to increase the EndDate to the following Monday. I am getting the same EndDates…..thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
input datex :yymmdd10.;
format datex yymmddd10.;
datalines;
2019-01-15 
2016-08-25 
2019-04-22 
2017-09-15 
2018-11-02
;
run;

data have1;
input ID 1. StartDate :yymmdd10. EndDate :yymmdd10.;
format ID 1. StartDate :yymmdd10. EndDate :yymmdd10.;
datalines;
1 2017-01-01 2018-12-30
2 2016-01-01 2017-12-30
3 2015-01-01 2016-12-30
4 2017-09-01 2017-12-30
5 2018-11-01 2019-12-30
;
run;

proc sql noprint;
	select count(datex) into :countList separated by ' '  	    
	from work.temp;		   
quit;
run;
%put &amp;amp;countList;

proc sql noprint;
	select datex into :dateList separated by ' '  	    
	from work.temp;		   
quit;
run;
%put &amp;amp;dateList;
%macro check;
data want;
set have1;
by ID;
%do i=1 %to i=&amp;amp;countList;
  %if StartDate &amp;lt;= &amp;amp;dateList &amp;lt;= EndDate
  %then %do;
    EndDate = EndDate + 1;
	%if weekday(EndDate) in (1,7) %then 
	EndDate = EndDate + 2;
%end;
%output;
%return;
%end;
run;
%mend check;
%check;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="166" style="width: 190px;"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD style="width: 40px;"&gt;ID&lt;/TD&gt;
&lt;TD style="width: 59.15px;"&gt;StartDate&lt;/TD&gt;
&lt;TD style="width: 54.5px;"&gt;EndDate&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="width: 40px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 59.15px;"&gt;2017-01-01&lt;/TD&gt;
&lt;TD style="width: 54.5px;"&gt;2018-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="width: 40px;"&gt;2&lt;/TD&gt;
&lt;TD style="width: 59.15px;"&gt;2016-01-01&lt;/TD&gt;
&lt;TD style="width: 54.5px;"&gt;2018-01-02&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="width: 40px;"&gt;3&lt;/TD&gt;
&lt;TD style="width: 59.15px;"&gt;2015-01-01&lt;/TD&gt;
&lt;TD style="width: 54.5px;"&gt;2017-01-02&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="width: 40px;"&gt;4&lt;/TD&gt;
&lt;TD style="width: 59.15px;"&gt;2017-09-01&lt;/TD&gt;
&lt;TD style="width: 54.5px;"&gt;2018-01-02&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="width: 40px;"&gt;5&lt;/TD&gt;
&lt;TD style="width: 59.15px;"&gt;2018-11-01&lt;/TD&gt;
&lt;TD style="width: 54.5px;"&gt;2019-12-31&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Fri, 16 Aug 2019 20:27:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581837#M165414</guid>
      <dc:creator>twildone</dc:creator>
      <dc:date>2019-08-16T20:27:18Z</dc:date>
    </item>
    <item>
      <title>Re: re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581841#M165415</link>
      <description>&lt;P&gt;No macros needed. This is a Cartesian join, or to phrase it another way, you want to test every possible combination of records in temp and have1, and keep the ones where datex is between startdate and enddate. This may take a very long time depending on the size of your input data set. I show how to increase the enddate by 1. I leave it up to you to modify this to take care of weekends.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data every_combination;
    set temp;
	do i=1 to n;
	    set have1 point=i nobs=n;
		if startdate&amp;lt;datex&amp;lt;enddate then do;
			enddate=enddate+1;
			output ;
		end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2019 19:55:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581841#M165415</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-16T19:55:01Z</dc:date>
    </item>
    <item>
      <title>Re: re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581843#M165416</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp; slick explanation on target Sir. Very neat!&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2019 19:59:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581843#M165416</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-16T19:59:38Z</dc:date>
    </item>
    <item>
      <title>Re: re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581845#M165417</link>
      <description>&lt;P&gt;Please show the results that you want for that input.&lt;/P&gt;
&lt;P&gt;Are you expecting 5 time 5 = 25 observations?&amp;nbsp; Or something else?&lt;/P&gt;
&lt;P&gt;What about values of ENDDATE that already fall on a week-end?&amp;nbsp; What value do you want for those when DATEX is NOT between STARTDATE and ENDDATE?&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2019 20:13:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581845#M165417</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-16T20:13:29Z</dc:date>
    </item>
    <item>
      <title>Re: re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581846#M165418</link>
      <description>&lt;P&gt;You have some nonsense code in there.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do you think the macro processor is going to do with this statement?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if weekday(EndDate) in (1,7) %then &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The letters WEE.... are never going to be equal to the digit 1 or the digit 7.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2019 20:15:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581846#M165418</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-16T20:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581848#M165420</link>
      <description>&lt;P&gt;Is this what you want?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
  date_id +1;
  input datex :yymmdd.;
  format datex yymmddd10.;
datalines;
2019-01-15 
2016-08-25 
2019-04-22 
2017-09-15 
2018-11-02
;

data have1;
  input ID StartDate :yymmdd. EndDate :yymmdd.;
  format StartDate EndDate yymmdd10.;
datalines;
1 2017-01-01 2018-12-30
2 2016-01-01 2107-12-30
3 2015-01-01 2016-12-30
4 2017-09-01 2017-12-30
5 2018-11-01 2019-12-30
;

proc sql noprint ;
  create table want as 
    select * 
         , (StartDate &amp;lt;= datex &amp;lt;= EndDate) as is_between
         , max(EndDate,intnx('weekday',EndDate,calculated is_between))
           as new_Enddate format=yymmdd10.
   from temp,have1
   ;
quit;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                                                                      new_
Obs    date_id         datex    ID     StartDate       EndDate    is_between       Enddate

  1       1       2019-01-15     1    2017-01-01    2018-12-30         0        2018-12-30
  2       1       2019-01-15     2    2016-01-01    2107-12-30         1        2108-01-02
  3       1       2019-01-15     3    2015-01-01    2016-12-30         0        2016-12-30
  4       1       2019-01-15     4    2017-09-01    2017-12-30         0        2017-12-30
  5       1       2019-01-15     5    2018-11-01    2019-12-30         1        2019-12-31
  6       2       2016-08-25     1    2017-01-01    2018-12-30         0        2018-12-30
  7       2       2016-08-25     2    2016-01-01    2107-12-30         1        2108-01-02
  8       2       2016-08-25     3    2015-01-01    2016-12-30         1        2017-01-02
  9       2       2016-08-25     4    2017-09-01    2017-12-30         0        2017-12-30
 10       2       2016-08-25     5    2018-11-01    2019-12-30         0        2019-12-30
 11       3       2019-04-22     1    2017-01-01    2018-12-30         0        2018-12-30
 12       3       2019-04-22     2    2016-01-01    2107-12-30         1        2108-01-02
 13       3       2019-04-22     3    2015-01-01    2016-12-30         0        2016-12-30
 14       3       2019-04-22     4    2017-09-01    2017-12-30         0        2017-12-30
 15       3       2019-04-22     5    2018-11-01    2019-12-30         1        2019-12-31
 16       4       2017-09-15     1    2017-01-01    2018-12-30         1        2018-12-31
 17       4       2017-09-15     2    2016-01-01    2107-12-30         1        2108-01-02
 18       4       2017-09-15     3    2015-01-01    2016-12-30         0        2016-12-30
 19       4       2017-09-15     4    2017-09-01    2017-12-30         1        2018-01-01
 20       4       2017-09-15     5    2018-11-01    2019-12-30         0        2019-12-30
 21       5       2018-11-02     1    2017-01-01    2018-12-30         1        2018-12-31
 22       5       2018-11-02     2    2016-01-01    2107-12-30         1        2108-01-02
 23       5       2018-11-02     3    2015-01-01    2016-12-30         0        2016-12-30
 24       5       2018-11-02     4    2017-09-01    2017-12-30         0        2017-12-30
 25       5       2018-11-02     5    2018-11-01    2019-12-30         1        2019-12-31
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Or do you want it collapsed somehow?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint ;
  create table want as 
    select id, startdate, enddate
         , sum(StartDate &amp;lt;= datex &amp;lt;= EndDate) as days_between
         , max(EndDate,intnx('weekday',EndDate,calculated days_between))
           as new_Enddate format=yymmdd10.
   from temp , have1
   group by id, startdate, enddate
   ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                          days_           new_
Obs    ID     StartDate       EndDate    between       Enddate

 1      1    2017-01-01    2018-12-30       2       2019-01-01
 2      2    2016-01-01    2107-12-30       5       2108-01-06
 3      3    2015-01-01    2016-12-30       1       2017-01-02
 4      4    2017-09-01    2017-12-30       1       2018-01-01
 5      5    2018-11-01    2019-12-30       3       2020-01-02
&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Aug 2019 20:21:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581848#M165420</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-16T20:21:46Z</dc:date>
    </item>
    <item>
      <title>Re: re: macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581850#M165422</link>
      <description>&lt;P&gt;Thanks Tom....It was the second approach that I really want....Awesome!!&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2019 20:32:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-macro-do-loop/m-p/581850#M165422</guid>
      <dc:creator>twildone</dc:creator>
      <dc:date>2019-08-16T20:32:06Z</dc:date>
    </item>
  </channel>
</rss>

