<?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 Calculating and retaining the 'count of change of a value' within the group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-and-retaining-the-count-of-change-of-a-value-within/m-p/300881#M63599</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like a variable, say group_count&amp;nbsp;to show and accumulate a count if there is a change from previous value within the group. I want the count of names(in the following data) and the number of times it changed from one fmt to other&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, I have a dataset ,&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data comm;
input var1 name$ fmt$;
datalines;
1 A other
1 B other
1 C c
1 D d
2 A other
2 B c
2 C b
2 D c
3 A a
3 B c
3 C d
3 D c
4 A c
4 B other
4 C other
4 D b
;
run;


data test_comm;
set comm;
by var1;
retain group_change;
if first.var1 then 
group_change=0;
if lag(val_fmt)=val_fmt then group_change=0;
else group_change+1;
if last.var1 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the output to be&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;var1&amp;nbsp;names&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;group_Change&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;2&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;2&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;3&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;3&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;3&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;4&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;2&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;As 1 changed from other to c and c to d, the count of change is 2&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;As 2 changed from&amp;nbsp; other to c to b to c the count of changes is 3..&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;With my code,&amp;nbsp;I get the group_Changes as 2,4,4,1 respectively .&lt;/FONT&gt; &lt;FONT face="arial,helvetica,sans-serif"&gt;Also, I cannot figure out how to maintain the count of names.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 26 Sep 2016 21:46:06 GMT</pubDate>
    <dc:creator>vsharipriya</dc:creator>
    <dc:date>2016-09-26T21:46:06Z</dc:date>
    <item>
      <title>Calculating and retaining the 'count of change of a value' within the group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-and-retaining-the-count-of-change-of-a-value-within/m-p/300881#M63599</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like a variable, say group_count&amp;nbsp;to show and accumulate a count if there is a change from previous value within the group. I want the count of names(in the following data) and the number of times it changed from one fmt to other&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, I have a dataset ,&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data comm;
input var1 name$ fmt$;
datalines;
1 A other
1 B other
1 C c
1 D d
2 A other
2 B c
2 C b
2 D c
3 A a
3 B c
3 C d
3 D c
4 A c
4 B other
4 C other
4 D b
;
run;


data test_comm;
set comm;
by var1;
retain group_change;
if first.var1 then 
group_change=0;
if lag(val_fmt)=val_fmt then group_change=0;
else group_change+1;
if last.var1 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the output to be&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;var1&amp;nbsp;names&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;group_Change&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;2&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;2&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;3&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;3&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;3&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;4&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;2&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;As 1 changed from other to c and c to d, the count of change is 2&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" face="arial,helvetica,sans-serif"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;As 2 changed from&amp;nbsp; other to c to b to c the count of changes is 3..&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;With my code,&amp;nbsp;I get the group_Changes as 2,4,4,1 respectively .&lt;/FONT&gt; &lt;FONT face="arial,helvetica,sans-serif"&gt;Also, I cannot figure out how to maintain the count of names.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2016 21:46:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-and-retaining-the-count-of-change-of-a-value-within/m-p/300881#M63599</guid>
      <dc:creator>vsharipriya</dc:creator>
      <dc:date>2016-09-26T21:46:06Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating and retaining the 'count of change of a value' within the group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-and-retaining-the-count-of-change-of-a-value-within/m-p/300884#M63600</link>
      <description>&lt;P&gt;Here's one approach.&lt;/P&gt;
&lt;P&gt;Note change in variable to FMT from val_fmt in your code.&lt;/P&gt;
&lt;P&gt;IF and LAG in the same statement seldom work as intended. Please spend some time reading the documentation about the separate streams. Then read again. Generally to mimimize debugging time just assign the LAGged variables for every observation.&lt;/P&gt;
&lt;P&gt;You also needed another variable to accumulate the name count.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data comm;
input var1 name$ fmt$;
datalines;
1 A other
1 B other
1 C c
1 D d
2 A other
2 B c
2 C b
2 D c
3 A a
3 B c
3 C d
3 D c
4 A c
4 B other
4 C other
4 D b
;
run;


data test_comm;
   set comm;
   by var1;
   retain group_change Names;
   LagFmt = lag(Fmt);

   if first.var1 then do; 
      group_change=0;
      Names=1;
   end;
   Else do;
      if LagFmt ne fmt then group_change+1;
      Names+1;
   end;
   if last.var1 then output;
   Keep var1 names group_change;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Sep 2016 21:58:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-and-retaining-the-count-of-change-of-a-value-within/m-p/300884#M63600</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-09-26T21:58:19Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating and retaining the 'count of change of a value' within the group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-and-retaining-the-count-of-change-of-a-value-within/m-p/301098#M63675</link>
      <description>Thanks! This worked.</description>
      <pubDate>Tue, 27 Sep 2016 18:33:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-and-retaining-the-count-of-change-of-a-value-within/m-p/301098#M63675</guid>
      <dc:creator>vsharipriya</dc:creator>
      <dc:date>2016-09-27T18:33:28Z</dc:date>
    </item>
  </channel>
</rss>

