<?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: how to count entry, exit and unchanged by month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-entry-exit-and-unchanged-by-month/m-p/337253#M76572</link>
    <description>&lt;P&gt;Sort by Id and date.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use BY groups to identify the first and last records and anything else as an unchanged.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For records where it's a single record, &amp;nbsp;(first.id = last.id) then I would count it as both entry and exit.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc sort data =have; by Id year month;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have; by Id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Entry=0; Exit=0; unchanged =0;&lt;/P&gt;
&lt;P&gt;if first.id then entry=1;&lt;/P&gt;
&lt;P&gt;if last.id then exit+1;&lt;/P&gt;
&lt;P&gt;if first.id =0 and last.id=0 then unchanged+1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Thu, 02 Mar 2017 07:36:38 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-03-02T07:36:38Z</dc:date>
    <item>
      <title>how to count entry, exit and unchanged by month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-entry-exit-and-unchanged-by-month/m-p/337245#M76568</link>
      <description>&lt;P&gt;I tried to create three variables: exit_count, entry_count, and unchanged_count for each month based on id.&lt;/P&gt;&lt;P&gt;1) year1993 month9 is the starting month, so&amp;nbsp;&lt;SPAN&gt;exit_count, entry_count, and unchanged_count are all set to 0.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2) next, compare year1993 month10 with month9, we find that aa, bb, cc are "unchanged", while dd and ee drop out, so they are "exit", and ff does not show up in prior month, so it is "entry".&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3) move to year1993 month11, compared with year1993 month10, only bb, cc are "unchanged", and aa, ff drop out, while dd, ee,gg, and ll are new entry.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;so, I &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;compare month t with month t-1&lt;/STRONG&gt;&lt;/FONT&gt;, to count how many "old" id drop out&amp;nbsp;or remain unchanged, and how many new id show up.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Originally,&amp;nbsp;I was thinking to use the rolling 2-month window (e.g., put 1993.9 and 1993.10 together), then count the id, if appear twice put into unchanged group; but if appear only once, it is difficult to tell whether it should be in exit or entry group.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2017 16:59:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-entry-exit-and-unchanged-by-month/m-p/337245#M76568</guid>
      <dc:creator>Jonate_H</dc:creator>
      <dc:date>2017-03-03T16:59:46Z</dc:date>
    </item>
    <item>
      <title>Re: how to count entry, exit and unchanged by month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-entry-exit-and-unchanged-by-month/m-p/337253#M76572</link>
      <description>&lt;P&gt;Sort by Id and date.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use BY groups to identify the first and last records and anything else as an unchanged.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For records where it's a single record, &amp;nbsp;(first.id = last.id) then I would count it as both entry and exit.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc sort data =have; by Id year month;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have; by Id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Entry=0; Exit=0; unchanged =0;&lt;/P&gt;
&lt;P&gt;if first.id then entry=1;&lt;/P&gt;
&lt;P&gt;if last.id then exit+1;&lt;/P&gt;
&lt;P&gt;if first.id =0 and last.id=0 then unchanged+1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2017 07:36:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-entry-exit-and-unchanged-by-month/m-p/337253#M76572</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-02T07:36:38Z</dc:date>
    </item>
    <item>
      <title>Re: how to count entry, exit and unchanged by month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-entry-exit-and-unchanged-by-month/m-p/337254#M76573</link>
      <description>&lt;P&gt;Obviously untested since I'm not typing out your data.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2017 07:37:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-entry-exit-and-unchanged-by-month/m-p/337254#M76573</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-02T07:37:21Z</dc:date>
    </item>
    <item>
      <title>Re: how to count entry, exit and unchanged by month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-entry-exit-and-unchanged-by-month/m-p/337394#M76608</link>
      <description>&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Building on Reeza understanding of the problem.&lt;BR /&gt;&lt;BR /&gt;I added unchanged counts as a post script?

SOAPBOX ON
No need for proc import.
Wish SAS would deprecate 'proc import' and build more
libname engines and not do the opposite.

I do not mind downloading xls files. I have
very strong security.

Benefit outweighs the downside.
SOAPBOX OFF


HAVE The excel sheet below
==========================

Up to 40 obs from d:/xls/exit_entry_unchange_count.xlsx  total obs=35

        A         B                 C        D         E           F

Up to 40 obs from xel.'sheet1$'n total obs=35

                                         TOTAL_   EXIT_   ENTRY_   UNCHANGED_
     Obs   YEAR   QUARTER   MONTH   ID    COUNT   COUNT    COUNT      COUNT

       1   1993      3         9    aa      5       0        0          0
       2   1993      3         9    bb      5       0        0          0
       3   1993      3         9    cc      5       0        0          0
       4   1993      3         9    dd      5       0        0          0
       5   1993      3         9    ee      5       0        0          0
       6   1993      4        10    aa      4       2        1          3
       7   1993      4        10    bb      4       2        1          3
       8   1993      4        10    cc      4       2        1          3
       9   1993      4        10    ff      4       2        1          3
      10   1993      4        11    bb      6       2        4          2


WANT
====
                                               TOTAL_ EXIT_ ENTRY_ UNCHANGED
Obs ID ENTRY EXIT UNCHANGED YEAR QUARTER MONTH  COUNT COUNT  COUNT    COUNT

  1 aa   1     0      0     1993    3       9     5     0      0        0
  2 aa   0     0      1     1993    4      10     4     2      1        3
  3 aa   0     0      1     1994    1       2     3     2      1        2
  4 aa   0     1      0     1994    1       3     5     0      2        3
  5 bb   1     0      0     1993    3       9     5     0      0        0
  6 bb   0     0      1     1993    4      10     4     2      1        3
  7 bb   0     0      1     1993    4      11     6     2      4        2
  8 bb   0     1      0     1993    4      12     5     1      0        5
  9 cc   1     0      0     1993    3       9     5     0      0        0
 10 cc   0     0      1     1993    4      10     4     2      1        3


DETAILS
=======

    1. Sort by ID

         Obs ID ENTRY UNCHANGED EXIT

           1 aa   1       0       0  Entry = 1
           2 aa   0       1       0  Unchanged  1
           3 aa   0       1       0  Unchanged still 1
           4 aa   0       0       1  Exit set to 1

           5 bb   1       0       0
           6 bb   0       1       0
           7 bb   0       1       0
           8 bb   0       0       1

WORKING CODE (very slight change to Reeza's assumes you do not want to count
unchanged.
============================================================================

  if first.id                  then entry    =1;
  if last.id                   then exit     =1;
  if first.id =0 and last.id=0 then unchanged=1;


FULL SOLUTION
=============

libname xel "d:/xls/exit_entry_unchange_count.xlsx";
Proc sort data =xel.'sheet1$'n out=have;
by Id year month;
run;quit;

data want;
  retain id '  ' entry unchanged exit 0;

  set have;
  by id;

  entry     =0;
  exit      =0;
  unchanged =0;

  if first.id                  then entry    =1;
  if last.id                   then exit     =1;
  if first.id =0 and last.id=0 then unchanged=1;

  output;

run;quit;

PS If you want to count the unchanged

data wantcnt(drop=unchange_cnt);

  do until (last.id);
     set want;
     by id;
     unchange_cnt= sum(unchange_cnt,unchanged);
  end;
  do until (last.id);
     set want;
     by id;
     if unchanged=1 then unchanged=unchange_cnt;
     output;
  end;

  unchange_cnt=0;

run;quit;


Up to 40 obs WORK.WANTCNT to


Obs ID ENTRY UNCHANGED EXIT

  1 aa   1       0       0
  2 aa   0       2       0
  3 aa   0       2       0
  4 aa   0       0       1
  5 bb   1       0       0
  6 bb   0       2       0
  7 bb   0       2       0
  8 bb   0       0       1

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Mar 2017 15:04:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-entry-exit-and-unchanged-by-month/m-p/337394#M76608</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-03-02T15:04:56Z</dc:date>
    </item>
    <item>
      <title>Re: how to count entry, exit and unchanged by month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-entry-exit-and-unchanged-by-month/m-p/337477#M76633</link>
      <description>Thank you all for the inputs.</description>
      <pubDate>Thu, 02 Mar 2017 18:01:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-entry-exit-and-unchanged-by-month/m-p/337477#M76633</guid>
      <dc:creator>Jonate_H</dc:creator>
      <dc:date>2017-03-02T18:01:12Z</dc:date>
    </item>
  </channel>
</rss>

