<?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 Need to identify Sequence based off of binary field for hospitals in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-to-identify-Sequence-based-off-of-binary-field-for/m-p/401538#M97436</link>
    <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm struggling to create the appropriate groups for my data.&lt;/P&gt;&lt;P&gt;The question is around data submission. A hospital can submit data non-consecutively and I need a way to understand the count of consecutive months per hospital. The last data step in the code below will sum the total number of months but I need to find a way to report on each segment of months reported (e.g., 2017-01 to 2017-05 (one sequence), 2017-07 to 201708 (second sequence), etc).&amp;nbsp; Thank you!&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 flatfile2;
set flatfile;
beg_month=input(begin_date,anydtdte19.);
format beg_month yymms10.;
run;

proc sort data=flatfile2;
by hospital beg_month;
run;

data testing;
set flatfile2;
groupnum+(First.hospital or (dif(beg_month) GT 1));
run;

data flatfile3;
  set testing;
  by hospital;
  begindate=first.hospital;
  enddate=last.hospital;
  lagdate=lag(groupnum);
  if begindate=1 then do;
  lagdate=groupnum-1;
  end;
run;

/*this is the piece that is flagging the consecutive months*/
/*so, a hospital with records where diff= 1 0 0 0 0 0 (on separate rows) would be indicative of 6 months of consecutive data*/
/*I'm stuck here because I need to find a way to set this as one sequence.*/
data flatfile4 (drop=begindate enddate);
  set flatfile3;
  diff=groupnum-lagdate;
  if diff &amp;lt;=1 then diff=0;
  else if diff&amp;gt;1 then diff=1;
  if begindate=1 and diff=0 then diff=1;
run;

proc sort data=flatfile4;
by hospital diff;
run;

data flatfile5;
set flatfile4;
by hospital diff;
if first.hospital and first.diff=1 then totalmo=0;
totalmo+1;
if last.hospital then output;
run;

proc print data=flatfile5;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 05 Oct 2017 22:35:07 GMT</pubDate>
    <dc:creator>Kaley</dc:creator>
    <dc:date>2017-10-05T22:35:07Z</dc:date>
    <item>
      <title>Need to identify Sequence based off of binary field for hospitals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-identify-Sequence-based-off-of-binary-field-for/m-p/401538#M97436</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm struggling to create the appropriate groups for my data.&lt;/P&gt;&lt;P&gt;The question is around data submission. A hospital can submit data non-consecutively and I need a way to understand the count of consecutive months per hospital. The last data step in the code below will sum the total number of months but I need to find a way to report on each segment of months reported (e.g., 2017-01 to 2017-05 (one sequence), 2017-07 to 201708 (second sequence), etc).&amp;nbsp; Thank you!&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 flatfile2;
set flatfile;
beg_month=input(begin_date,anydtdte19.);
format beg_month yymms10.;
run;

proc sort data=flatfile2;
by hospital beg_month;
run;

data testing;
set flatfile2;
groupnum+(First.hospital or (dif(beg_month) GT 1));
run;

data flatfile3;
  set testing;
  by hospital;
  begindate=first.hospital;
  enddate=last.hospital;
  lagdate=lag(groupnum);
  if begindate=1 then do;
  lagdate=groupnum-1;
  end;
run;

/*this is the piece that is flagging the consecutive months*/
/*so, a hospital with records where diff= 1 0 0 0 0 0 (on separate rows) would be indicative of 6 months of consecutive data*/
/*I'm stuck here because I need to find a way to set this as one sequence.*/
data flatfile4 (drop=begindate enddate);
  set flatfile3;
  diff=groupnum-lagdate;
  if diff &amp;lt;=1 then diff=0;
  else if diff&amp;gt;1 then diff=1;
  if begindate=1 and diff=0 then diff=1;
run;

proc sort data=flatfile4;
by hospital diff;
run;

data flatfile5;
set flatfile4;
by hospital diff;
if first.hospital and first.diff=1 then totalmo=0;
totalmo+1;
if last.hospital then output;
run;

proc print data=flatfile5;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Oct 2017 22:35:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-identify-Sequence-based-off-of-binary-field-for/m-p/401538#M97436</guid>
      <dc:creator>Kaley</dc:creator>
      <dc:date>2017-10-05T22:35:07Z</dc:date>
    </item>
    <item>
      <title>Re: Need to identify Sequence based off of binary field for hospitals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-identify-Sequence-based-off-of-binary-field-for/m-p/401541#M97438</link>
      <description>&lt;P&gt;Provide a short example data set or sets&amp;nbsp;and what the result should be.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2017 22:40:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-identify-Sequence-based-off-of-binary-field-for/m-p/401541#M97438</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-10-05T22:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: Need to identify Sequence based off of binary field for hospitals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-identify-Sequence-based-off-of-binary-field-for/m-p/401718#M97502</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/169859"&gt;@Kaley&lt;/a&gt;:&lt;/P&gt;&lt;P&gt;I think you have a problem about identifying the consecutive months. The variable beg_month is a DATE value, not a MONTH, even though you assign a format to it that only displays the month, not the day. The expression "&lt;SPAN class="token procnames"&gt;dif&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;beg_month&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;GT&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;" will return 1 (true) whenever there is more than one day (not one month) between visits.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think what you want to do is something like this:&lt;/P&gt;&lt;PRE&gt;data flatfile2;
  set flatfile;
  beg_month=input(begin_date,anydtdte19.);
  format beg_month yymms10.;
run;

proc sort data=flatfile2;
  by hospital beg_month;
run;

data groups;
  set flatfile2;
  by hospital;
  prev_date=lag(beg_month);
  if first.hospital then
    group=1;
  else if intck('month',prev_date,beg_month)&amp;gt;1 then
    group+1;
run;&lt;/PRE&gt;&lt;P&gt;You then have data sorted by HOSPITAL and GROUP, where each GROUP is a series of visits in consecutive months.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 12:36:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-identify-Sequence-based-off-of-binary-field-for/m-p/401718#M97502</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2017-10-06T12:36:19Z</dc:date>
    </item>
    <item>
      <title>Re: Need to identify Sequence based off of binary field for hospitals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-identify-Sequence-based-off-of-binary-field-for/m-p/401872#M97522</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;Thanks so much! That worked to create the groups!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you know of an easy way to them sum the number of months (rows) for each of those groups?&lt;/P&gt;&lt;P&gt;This is the code that I currently have but it's only giving me the last.hospital.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=groups;
by hospital group;
run;

data flatfilefin;
set groups;
by hospital group;
if first.hospital and first.group=1 then totalmo=0;
totalmo+1;
if last.hospital then output;
run;

proc summary data=flatfilefin nway;
class Hospital Group;
var totalmo;
output out=test sum=;
run;
proc print data=test;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Oct 2017 18:30:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-identify-Sequence-based-off-of-binary-field-for/m-p/401872#M97522</guid>
      <dc:creator>Kaley</dc:creator>
      <dc:date>2017-10-06T18:30:59Z</dc:date>
    </item>
  </channel>
</rss>

