<?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: Pulling Data for Any Three Consecutive Months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Data-for-Any-Three-Consecutive-Months/m-p/354268#M82852</link>
    <description>&lt;P&gt;It would help if you posted sample have and want datasteps (preferably in the form of data steps)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Apr 2017 19:58:41 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-04-27T19:58:41Z</dc:date>
    <item>
      <title>Pulling Data for Any Three Consecutive Months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Data-for-Any-Three-Consecutive-Months/m-p/354257#M82847</link>
      <description>&lt;P&gt;Can anyone help with the following - I am trying to pull data for any consecutive three months for a performance period between October through Feb. Currently, the data pulls for Oct/Nov/Dec OR Nov/Dec/Jan, but not sure how to write the code to pull for Oct/Nov/Dec or Nov/Dec/Jan or Dec/Jan/Feb. Thank you for your help in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;*** Create test data ***;
data Test1;
   *** HCO with all four months ***;
   HCO = 1; Year = 2016; Month = 10; output;
   HCO = 1; Year = 2016; Month = 11; output;
   HCO = 1; Year = 2016; Month = 12; output;
   HCO = 1; Year = 2017; Month = 1; output;

   *** HCO with three consecutive months ***;
   HCO = 2; Year = 2016; Month = 11; output;
   HCO = 2; Year = 2016; Month = 12; output;
   HCO = 2; Year = 2017; Month = 1; output;

   *** HCO with three consecutive months ***;
   HCO = 3; Year = 2016; Month = 10; output;
   HCO = 3; Year = 2016; Month = 11; output;
   HCO = 3; Year = 2016; Month = 12; output;

   *** HCO with gap ***;
   HCO = 4; Year = 2016; Month = 10; output;
   HCO = 4; Year = 2016; Month = 12; output;
   HCO = 4; Year = 2017; Month = 1; output;
run;

proc sort data=Test1;
   by HCO Year Month;
run;

*** Create a list of all months ***;
proc sql;
   create table Months1 as
   select distinct Year, Month
   from Test1
   order by Year, Month;
quit;

*** Create a list of HCOs ***;
proc sql;
   create table HCO1 as
   select distinct HCO
   from Test1
   order by HCO;
quit;

*** Create combinations of all possible HCOs/Years/Months ***;
proc sql;
   create table Master1 as
   select a.*, b.*
   from HCO1 a,
        Months1 b
   order by HCO, Year, Month;
quit;

*** Flag months with data ***;
data Check1;
   merge Master1 (in=ina)
         Test1 (in=inb);
   by HCO Year Month;

   if inb then
      Flag = 1;   *** We have data ***;
   else
      Flag = 0;   *** We do not have data ***;
run;

*** Check for three consecutive months ***;
data Check2 (keep=HCO);
   set Check1;
   by HCO Year Month;

   retain Consecutive;

   if first.HCO then
      Consecutive = 0;

   if Flag = 1 then
      Consecutive = Consecutive + 1;
   else
      Consecutive = 0;

   if Consecutive = 3 then
      output;
run;

data Test2;
   merge Test1 (in=ina)
         Check2 (in=inb);
   by HCO;

   if ina and inb;
run;




data Months2;
   Year = 2016; Month = 11; output;
   Year = 2016; Month = 12; output;
   Year = 2017; Month = 1; output;
run;

proc sort data=Test2;
   by Year Month HCO;
run;

data Test3;
   merge Test2 (in=ina)
         Months2 (in=inb);
   by Year Month;

   if inb;
run;

proc sort data=Test3;
   by HCO Year Month;
run;

proc sql;
   create table Three1 as
   select HCO,
          count(*) as Months
   from Test3
   group by HCO
   having count(*) = 3
   order by HCO;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 21:04:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-Data-for-Any-Three-Consecutive-Months/m-p/354257#M82847</guid>
      <dc:creator>thb</dc:creator>
      <dc:date>2017-04-27T21:04:59Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling Data for Any Three Consecutive Months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Data-for-Any-Three-Consecutive-Months/m-p/354268#M82852</link>
      <description>&lt;P&gt;It would help if you posted sample have and want datasteps (preferably in the form of data steps)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 19:58:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-Data-for-Any-Three-Consecutive-Months/m-p/354268#M82852</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-04-27T19:58:41Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling Data for Any Three Consecutive Months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Data-for-Any-Three-Consecutive-Months/m-p/354274#M82853</link>
      <description>&lt;P&gt;You are making life hard for yourself by storing year and month in separate variables. Your task would be a lot easier if you created a SAS date for your data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date = mdy(month, 1, year);
format date monyy7.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Apr 2017 20:29:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-Data-for-Any-Three-Consecutive-Months/m-p/354274#M82853</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2017-04-27T20:29:03Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling Data for Any Three Consecutive Months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Data-for-Any-Three-Consecutive-Months/m-p/354278#M82856</link>
      <description>&lt;P&gt;To elaborate what &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;&amp;nbsp;just said, if 'test1' is your incoming data, please post your desired outcome.&amp;nbsp;Your last table&amp;nbsp;'three1' probably is not what you desired outcome, otherwise, you&amp;nbsp;could just live with what you&amp;nbsp;currently have. Your description doesn't help much, as far as I can read, the following statements is very confusing to me:&lt;/P&gt;
&lt;P&gt;"Currently, the data pulls for Oct/Nov/Dec OR Nov/Dec/Jan, but not sure how to write the code to pull for &lt;BR /&gt;Oct/Nov/Dec or Nov/Dec/Jan or Dec/Jan/Feb."&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 20:43:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-Data-for-Any-Three-Consecutive-Months/m-p/354278#M82856</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2017-04-27T20:43:54Z</dc:date>
    </item>
    <item>
      <title>Re: Pulling Data for Any Three Consecutive Months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pulling-Data-for-Any-Three-Consecutive-Months/m-p/354295#M82861</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data test2;
set test1;
monthDate = mdy(month,1,year);
drop month year;
format monthDate yymm.;
run;

proc sql;
create table test3 as
select
    t1.hco,
    t1.monthDate,
    t2.monthDate as monthDate2,
    t3.monthDate as monthDate3
from 
    test2 as t1 inner join
    test2 as t2 on t1.hco=t2.hco and intnx("month", t1.monthDate, 1) = t2.monthDate inner join
    test2 as t3 on t1.hco=t3.hco and intnx("month", t1.monthDate, 2) = t3.monthDate;
select * from test3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Apr 2017 22:12:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pulling-Data-for-Any-Three-Consecutive-Months/m-p/354295#M82861</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-04-27T22:12:49Z</dc:date>
    </item>
  </channel>
</rss>

