<?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 until there is a change in a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883493#M349077</link>
    <description>hm its not working, its flagging everything for me because its sorting by ID and TYPE regardless of the TIME variable.</description>
    <pubDate>Tue, 04 Jul 2023 22:47:25 GMT</pubDate>
    <dc:creator>SAS_Muggle</dc:creator>
    <dc:date>2023-07-04T22:47:25Z</dc:date>
    <item>
      <title>How to count until there is a change in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883368#M349015</link>
      <description>&lt;P&gt;I have some data that looks like the below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; TYPE&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;A&amp;nbsp; &amp;nbsp; &amp;nbsp;*&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;A&amp;nbsp; &amp;nbsp; &amp;nbsp;*&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;A&amp;nbsp; &amp;nbsp; &amp;nbsp;*&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;C&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;C&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This data is already sorted by&amp;nbsp; a 3rd variable TIME, which is not shown here. Basically I was to flag when the count of a particular combination of ID and TYPE is greater than 2 (see starred rows). It is possible for example that 1A is followed by 1B but the count of each is less than or equal to two, I wouldnt flag those. I only want to flag it if they occur more than twice consecutively in the sequence it is already sorted by.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2023 23:58:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883368#M349015</guid>
      <dc:creator>SAS_Muggle</dc:creator>
      <dc:date>2023-07-03T23:58:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to count until there is a change in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883377#M349019</link>
      <description>&lt;P&gt;There are probably smarter ways to do this but here is one possibility:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
  input ID $ Type $;
  datalines;
1     A
1     A
1     B
1     B
1     A 
1     A 
1     A  
1     C
1     C
2     A
;
run;

data Have2;
  set Have;
  by ID;
  if first.ID then Type_Count = 1;
  retain Last_Type;
  else do;
    if Type = Last_Type then Type_Count + 1;
	else Type_Count = 1;
    if Type_Count &amp;gt;= 3 then Type_Count_Flag = 'Y';
  end;
  Last_Type = Type;
  if Type_Count = 1 then Group_ID + 1;
  Order + 1;
run;

proc sql;
  create table Want as
  select A.ID
        ,A.Type
        ,B.Type_Count_Flag
  from Have2 as A
  left join Have2 as B
  on A.Group_ID = B.Group_ID
  and B.Type_Count_Flag = 'Y'
  order by A.Order;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Jul 2023 02:42:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883377#M349019</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2023-07-04T02:42:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to count until there is a change in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883379#M349020</link>
      <description>&lt;P&gt;You can use a lookup array:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID TYPE$;
  cards;
1 A
1 A
1 B
1 B
1 A
1 A
1 A
1 C
1 C
2 A
1 C
1 C
;
run;

%let nobs=&amp;amp;sysnobs;
data want;
  set have;
  by id type notsorted;

  combine=catx('#',id,type);
  array _par_[&amp;amp;nobs.]$_temporary_;
  retain flag;

  if first.type then do;
    if combine in _par_ then flag='*';
    else do;
      _par_[_n_]=combine;
      flag='';
    end;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85539iF61C9C4526F24677/image-size/medium?v=v2&amp;amp;px=400" role="button" title="1.png" alt="1.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If ID and TYPE combination have been appeared in the array, flag it, if not, push this&amp;nbsp;combination into array. And be caution to do this at first.TYPE, or it is more difficult to consider the consecutive&amp;nbsp;ID and TYPE combinations.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jul 2023 02:48:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883379#M349020</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2023-07-04T02:48:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to count until there is a change in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883380#M349021</link>
      <description>&lt;P&gt;Shift! I got it wrong. Here is the right anwser:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until(last.type);
    set have;
    by id type notsorted;
    sum=sum(sum,1);
  end;

  do until(last.type);
    set have;
    by id type notsorted;
    if sum&amp;gt;2 then flag='*';
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Jul 2023 02:58:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883380#M349021</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2023-07-04T02:58:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to count until there is a change in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883493#M349077</link>
      <description>hm its not working, its flagging everything for me because its sorting by ID and TYPE regardless of the TIME variable.</description>
      <pubDate>Tue, 04 Jul 2023 22:47:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883493#M349077</guid>
      <dc:creator>SAS_Muggle</dc:creator>
      <dc:date>2023-07-04T22:47:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to count until there is a change in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883495#M349079</link>
      <description>Could you please post an example data with the TIME variable? And write down what is your desired output in this example, too.</description>
      <pubDate>Wed, 05 Jul 2023 01:49:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883495#M349079</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2023-07-05T01:49:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to count until there is a change in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883507#M349089</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416033"&gt;@SAS_Muggle&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;hm its not working, its flagging everything for me because its sorting by ID and TYPE regardless of the TIME variable.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why did it SORT by ID and TYPE?&lt;/P&gt;
&lt;P&gt;Did you accidentally add an unwanted PROC SORT step?&lt;BR /&gt;Is the data coming from some external database that is doing the sorting on the fly?&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2023 05:15:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883507#M349089</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-05T05:15:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to count until there is a change in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883566#M349100</link>
      <description>&lt;P&gt;I assume that your data is sorted by ID and TIME, and if you do not sort the data by ID and TYPE before the data step, the solution proposed by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270406"&gt;@whymath&lt;/a&gt;&amp;nbsp;should work (note the NOTSORTED option on the BY statement) - or this one, which makes the count in the loop header:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do _N_=1 by 1 until(last.type);
    set have;
    by id type notsorted;
    end;
  if _N_&amp;gt;2 then flag ='*';
  do until(last.type);
    set have;
    by id type notsorted;
    output;
    end;  
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Jul 2023 13:19:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883566#M349100</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-07-05T13:19:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to count until there is a change in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883577#M349102</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416033"&gt;@SAS_Muggle&lt;/a&gt;&amp;nbsp;, Assuming my understanding of your requirement is correct,&amp;nbsp; methinks your question presents a neat use case for Hash suminc method. Of course, Hash requires significant RAM, nevertheless should be quick.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data Have;
  input ID $ Type $;
  datalines;
1     A
1     A
1     B
1     B
1     A 
1     A 
1     A  
1     C
1     C
2     A
;
run;


data want ;
  retain _iorc_ 1 ;
  if _n_=1 then do;
    dcl hash h (suminc:'_iorc_') ;
    h.definekey('id','type' ) ;
    h.definedone( );
  end ;
  set have ;
  h.ref ( ) ;
  h.sum (sum : _count) ; 
  if _count &amp;gt; 2 then flag = '*' ;
  drop _: ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Jul 2023 14:01:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-until-there-is-a-change-in-a-variable/m-p/883577#M349102</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2023-07-05T14:01:36Z</dc:date>
    </item>
  </channel>
</rss>

