<?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: Count number of unique cancellations per policy in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/548180#M151992</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input policy version cancelled;
cards;
123 1 0
123 2 1
123 3 0
123 4 0
123 5 0
123 6 1
123 7 1
157 1 0
157 2 1
157 3 1
157 4 1
157 5 0
157 6 1
157 7 0
188 1 0
188 2 1
997 1 0
997 2 1
997 3 0
997 4 1
997 5 0
997 6 0
997 7 1
997 8 1
;
data want;
 set have;
 by policy cancelled notsorted;
 if first.policy then temp=0;
 if first.cancelled and cancelled=1 then do;temp+1;want=temp;end;
 drop temp;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 03 Apr 2019 12:24:06 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2019-04-03T12:24:06Z</dc:date>
    <item>
      <title>Count number of unique cancellations per policy</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/548137#M151973</link>
      <description>&lt;P&gt;I have a sorted list of policies. Each time a policy is changed, a new policy version is created.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The policy versions can indicate the policy being cancelled. But policies can be 'saved' and later cancelled again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want a new variable that counts each new cancellation. It should only count the first rows with cancelled=1 after having had cancelled=0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example with 4 policies. The 4th column is what I want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="height: 495px; width: 254px;" width="292"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 39px;"&gt;
&lt;TD style="width: 40px; height: 39px;"&gt;Policy&lt;/TD&gt;
&lt;TD style="width: 47px; height: 39px;"&gt;Policy version&lt;/TD&gt;
&lt;TD style="width: 65px; height: 39px;"&gt;Cancelled&lt;/TD&gt;
&lt;TD style="width: 42px; height: 39px;"&gt;Want&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;123&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;0&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;123&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;2&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;123&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;3&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;0&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;123&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;4&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;0&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;123&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;5&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;0&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;123&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;6&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;123&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;7&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;157&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;0&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;157&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;2&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;157&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;3&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;157&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;4&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;157&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;5&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;0&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;157&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;6&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;157&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;7&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;0&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;188&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;0&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;188&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;2&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;997&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;0&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;997&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;2&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;997&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;3&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;0&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;997&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;4&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;997&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;5&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;0&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;997&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;6&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;0&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;997&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;7&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 19px;"&gt;
&lt;TD style="width: 40px; height: 19px;"&gt;997&lt;/TD&gt;
&lt;TD style="width: 47px; height: 19px;"&gt;8&lt;/TD&gt;
&lt;TD style="width: 65px; height: 19px;"&gt;1&lt;/TD&gt;
&lt;TD style="width: 42px; height: 19px;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can someone please advice me on how to create the 4th column in a data step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2019 08:48:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/548137#M151973</guid>
      <dc:creator>EinarRoed</dc:creator>
      <dc:date>2019-04-03T08:48:41Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of unique cancellations per policy</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/548147#M151976</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/33000"&gt;@EinarRoed&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input policy version cancelled;
cards;
123 1 0
123 2 1
123 3 0
123 4 0
123 5 0
123 6 1
123 7 1
157 1 0
157 2 1
157 3 1
157 4 1
157 5 0
157 6 1
157 7 0
188 1 0
188 2 1
997 1 0
997 2 1
997 3 0
997 4 1
997 5 0
997 6 0
997 7 1
997 8 1
;

data want(drop=_:);
set have;
by policy version;
_i=(dif(cancelled)&amp;gt;0);
_s+_i;
if first.policy then _s=0;
want=ifn(first.policy | ~_i,.,_s);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2019 09:49:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/548147#M151976</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-04-03T09:49:59Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of unique cancellations per policy</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/548166#M151985</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt; : that ifn is for increasing job-security, isn't it &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2019 11:28:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/548166#M151985</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-04-03T11:28:19Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of unique cancellations per policy</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/548169#M151987</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this, want2 should be your desired output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input policy version cancelled;
cards;
123 1 0
123 2 1
123 3 0
123 4 0
123 5 0
123 6 1
123 7 1
157 1 0
157 2 1
157 3 1
157 4 1
157 5 0
157 6 1
157 7 0
188 1 0
188 2 1
997 1 0
997 2 1
997 3 0
997 4 1
997 5 0
997 6 0
997 7 1
997 8 1
;


data want;
set have;
by policy;
if first.policy then do;
count=0;
end;
if not last.policy and cancelled=1 then count+1;
if last.policy and cancelled=1 then count+1;
else count=count;
run;

data want2;
set want;
if cancelled=0 then count=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2019 11:48:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/548169#M151987</guid>
      <dc:creator>Shivam</dc:creator>
      <dc:date>2019-04-03T11:48:44Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of unique cancellations per policy</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/548171#M151989</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt; : that ifn is for increasing job-security, isn't it &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't think it's too cryptic (if that's what you mean). Variable WANT is to be set to missing if the observation is the first in the POLICY BY-group or there isn't an increase of CANCELLED. Otherwise it is assigned the value of _S, the cumulative count of increases per BY-group.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2019 11:47:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/548171#M151989</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-04-03T11:47:50Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of unique cancellations per policy</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/548180#M151992</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input policy version cancelled;
cards;
123 1 0
123 2 1
123 3 0
123 4 0
123 5 0
123 6 1
123 7 1
157 1 0
157 2 1
157 3 1
157 4 1
157 5 0
157 6 1
157 7 0
188 1 0
188 2 1
997 1 0
997 2 1
997 3 0
997 4 1
997 5 0
997 6 0
997 7 1
997 8 1
;
data want;
 set have;
 by policy cancelled notsorted;
 if first.policy then temp=0;
 if first.cancelled and cancelled=1 then do;temp+1;want=temp;end;
 drop temp;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Apr 2019 12:24:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/548180#M151992</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-04-03T12:24:06Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of unique cancellations per policy</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/550206#M152751</link>
      <description>&lt;P&gt;The first suggestion, which was tagged as the solution, worked well in all cases except for one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When the input consisted of 1 single policy row with Cancelled=1, then Want was NULL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This scenario wasn't covered by my example scenarios though, as I didn't predict it could happen.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In any case, Ksharp's suggestion covers this scenario as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for all the feedback!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Apr 2019 12:52:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-unique-cancellations-per-policy/m-p/550206#M152751</guid>
      <dc:creator>EinarRoed</dc:creator>
      <dc:date>2019-04-11T12:52:44Z</dc:date>
    </item>
  </channel>
</rss>

