<?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: Do loop to get cases by month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664761#M198653</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223296"&gt;@mklangley&lt;/a&gt;&amp;nbsp;I believe&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19901"&gt;@Solph&lt;/a&gt;&amp;nbsp;asked for a row per year. Below amendments to your code should return such data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=fy id);
  set have;
  _stop = intck("year", entry_date, discharge_date);
  format fy year4.;

  do i = 0 to _stop;
    fy = intnx('year', entry_date, i, 'b');
    output;
  end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1593027622072.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46593i5CFB3373268452D2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1593027622072.png" alt="Patrick_0-1593027622072.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 24 Jun 2020 19:40:30 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2020-06-24T19:40:30Z</dc:date>
    <item>
      <title>Do loop to get cases by month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664698#M198620</link>
      <description>&lt;P&gt;Hi I tried to identify IDs who are active in a period. The example below is active by fiscal year from April 1 of a yea and March 31 of the next year. I'm wondering if I want to do it by month, how do I turn the code?&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 have; format entry_date discharge_date yymmdd10.; 
input id 1 @3 entry_date yymmdd10. @15 discharge_date yymmdd10. note $ 26-42;
datalines;
1 2017-05-11  2018-08-25 Active 2017,2018
2 2017-04-01  2017-08-02 Active 2017
3 2018-05-11  2018-07-20 Active 2018
4 2019-01-05  2019-04-29 Active 2018, 2019
5 2020-05-11  2020-05-20 Active 2020
6 2020-04-05  2020-06-02 Active 2020
;
quit;
proc print; run;
%macro active;
%do fy=2017 %to 2020;
proc sql;
create table active&amp;amp;fy as
	select distinct &amp;amp;fy as fy, id
	from have
	where entry_date&amp;lt;="31mar%eval(&amp;amp;fy+1)"d and (discharge_date&amp;gt;="01apr&amp;amp;fy"d or discharge_date=.);
%end;
%mend;
%active;
data active; set active2017-active2020; run; 
proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is the results for a list of ID present or active by fiscal year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;
&lt;TABLE style="border-collapse: collapse; width: 144pt;" border="0" width="192" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD width="64" height="21" class="xl69" style="height: 15.75pt; width: 48pt;"&gt;Obs&lt;/TD&gt;
&lt;TD width="64" class="xl70" style="width: 48pt;"&gt;fy&lt;/TD&gt;
&lt;TD width="64" class="xl71" style="width: 48pt;"&gt;id&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl66" style="height: 15.75pt;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65"&gt;2017&lt;/TD&gt;
&lt;TD class="xl72"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl66" style="height: 15.75pt;"&gt;2&lt;/TD&gt;
&lt;TD class="xl65"&gt;2017&lt;/TD&gt;
&lt;TD class="xl72"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl66" style="height: 15.75pt;"&gt;3&lt;/TD&gt;
&lt;TD class="xl65"&gt;2018&lt;/TD&gt;
&lt;TD class="xl72"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl66" style="height: 15.75pt;"&gt;4&lt;/TD&gt;
&lt;TD class="xl65"&gt;2018&lt;/TD&gt;
&lt;TD class="xl72"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl66" style="height: 15.75pt;"&gt;5&lt;/TD&gt;
&lt;TD class="xl65"&gt;2018&lt;/TD&gt;
&lt;TD class="xl72"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl66" style="height: 15.75pt;"&gt;6&lt;/TD&gt;
&lt;TD class="xl65"&gt;2019&lt;/TD&gt;
&lt;TD class="xl72"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl66" style="height: 15.75pt;"&gt;7&lt;/TD&gt;
&lt;TD class="xl65"&gt;2020&lt;/TD&gt;
&lt;TD class="xl72"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl67" style="height: 15.75pt;"&gt;8&lt;/TD&gt;
&lt;TD class="xl68"&gt;2020&lt;/TD&gt;
&lt;TD class="xl73"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;P&gt;How do I turn the code to be by month (or fiscal month)?&lt;/P&gt;
&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jun 2020 16:27:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664698#M198620</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2020-06-24T16:27:39Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop to get cases by month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664707#M198625</link>
      <description>&lt;P&gt;First, I don't understand why you are splitting HAVE into years, and then recombining the results. You can do this without a macro and without a loop to get years, and months would work similarly. (Although I'm not really sure what the output would be if you did it for months, what is a "fiscal month"?)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    create table active as
	select distinct id, year(intnx('month',max(entry_date,discharge_date),-3)) as fy
	from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jun 2020 16:57:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664707#M198625</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-06-24T16:57:46Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop to get cases by month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664713#M198628</link>
      <description>&lt;P&gt;Thanks, not quite.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it's to flag active cases by year, some (case #1,#4) cut across two years. Your code would only flag f or one year.&lt;/P&gt;
&lt;P&gt;And I was looking for solution to produce similar data but by month. So say for #1, this case is active each month from May 2017 to Aug 2018, for 16 months. Hope it is clear now.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jun 2020 17:01:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664713#M198628</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2020-06-24T17:01:42Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop to get cases by month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664735#M198634</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let period= DAY;
	data have; format entry_date discharge_date yymmdd10.; 
    informat entry_date discharge_date yymmdd10.; 
    length period $10.;
	input @5 id   @7 entry_date   @19 discharge_date   @30 note :$32  ;
	if "&amp;amp;period."="YEAR" then do;
		start = year(entry_date);
		end = year(discharge_date);
		
		do _period = start to end;
		 period=put(_period,z4.);
		 put _all_;
		 output;
		end;
	end;
	else if "&amp;amp;period."="MONTH" then do;
		start = entry_date;
		end = discharge_date;
		do _period = start to end ;
		 period=put(_period,yymm10.);
		 put _all_;
		 output;
		 _period = intnx('month',_period,1,'beg');
		end;
	end;
	else if "&amp;amp;period."="DAY" then do;
		start = entry_date;
		end = discharge_date;
		do _period = start to end ;
		 period=put(_period,Juldate7.);
		 output;
		end;
	end;
	datalines;
	1 2017-05-11  2018-08-25 Active 2017,2018
	2 2017-04-01  2017-08-02 Active 2017
	3 2018-05-11  2018-07-20 Active 2018
	4 2019-01-05  2019-04-29 Active 2018, 2019
	5 2020-05-11  2020-05-20 Active 2020
	6 2020-04-05  2020-06-02 Active 2020
	;;;
	run;

	proc means data=have nway missing noprint;
	class period;
	var ID;
	output out= want count=;
	run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Jun 2020 18:36:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664735#M198634</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-06-24T18:36:57Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop to get cases by month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664737#M198635</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19901"&gt;@Solph&lt;/a&gt;&amp;nbsp; Try this. Does this produce the results you're expecting?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=fiscal_month id);
    set have;
    x = intck("month", entry_date, discharge_date);
    format fiscal_month monyy7.;
    do i = 0 to x;
        fiscal_month = intnx('month', entry_date, i, 'b');
        output;
    end;
run;

proc sort data=want;
    by fiscal_month id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;EM&gt;Edit: corrected DO loop to start from 0&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jun 2020 18:46:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664737#M198635</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2020-06-24T18:46:33Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop to get cases by month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664761#M198653</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223296"&gt;@mklangley&lt;/a&gt;&amp;nbsp;I believe&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19901"&gt;@Solph&lt;/a&gt;&amp;nbsp;asked for a row per year. Below amendments to your code should return such data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=fy id);
  set have;
  _stop = intck("year", entry_date, discharge_date);
  format fy year4.;

  do i = 0 to _stop;
    fy = intnx('year', entry_date, i, 'b');
    output;
  end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1593027622072.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46593i5CFB3373268452D2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1593027622072.png" alt="Patrick_0-1593027622072.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jun 2020 19:40:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664761#M198653</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-06-24T19:40:30Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop to get cases by month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664768#M198657</link>
      <description>&lt;P&gt;mklangley, yours is similar to the one I googled and found on the net (see below), but it's good that you created var concatenating fiscal year and month.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	date = entry_date;
	do until(date &amp;gt; discharge_date);
	    month = month(date);
	    output;
		date = intnx("MONTH", date, 1, "BEGINNING"); *Alignment SAME is clock 30 days, BEGINNING counts if present 1st day, END then last date;
	    end;
	format date Date9.;
run;
proc print; run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jun 2020 20:13:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-to-get-cases-by-month/m-p/664768#M198657</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2020-06-24T20:13:40Z</dc:date>
    </item>
  </channel>
</rss>

