<?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: Running Count by Multiple Variables Where One Variable is NotSorted in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582029#M165486</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp; Thank you Sir for the note. I'm afraid if we do not include the data portion, the hash object compile/execution time operaton anyway does an automatic inclusion of keys as data too consequently doubling the hash entry length and memory footprint;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example; To test what's in hash table , I added the output method to&amp;nbsp; your code to view the contents&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have end=z;
  by id ;
  if _n_=1 then do;
    dcl hash h() ;
    h.definekey('id','ch') ;
    h.definedone() ;
  end;
  if first.id then count=0;
  count+(0=h.add());
if z then h.output(dataset:'whats_in_hash_table');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You would notice Id and ch combo are populated as data values in hash too confirming my initial point.-&lt;EM&gt;whats_in_hash_table&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Secondly, your boolean approach to increment count is great had ch been sorted i'd think, but i'm afraid we would need the count previously enumerated parked somewhere to retrieve and replace be it in a hash table or an array. Methinks this where OP prolly was perhaps stuck.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thirdly, Clear() helps in freeing up memory after processing each by group. A way to manage memory. The combo idea of ID and CH as opposed to just CH helps testing and debugging effectively since visualizing the contents of hash tables makes it comfy to know what's going on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS&amp;nbsp; The best fun tricky part of hashes that cause confusion is, when the keys and data portion are the same , the data portion alone can be modified. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 18 Aug 2019 22:35:11 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-08-18T22:35:11Z</dc:date>
    <item>
      <title>Running Count by Multiple Variables Where One Variable is NotSorted</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582011#M165475</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to create a running count by multiple variables where one ID variable is sorted, but the second variable is notsorted (the data set must remain sorted by a timestamp, which was not included in this sample).&amp;nbsp; Notice that for id = 1, a simple first. count variable would work, but for ID 2, it fails.&amp;nbsp; The count variable is the one I would like to create.&amp;nbsp; Any assistance in creating the count variable to match the sample output below is much appreciated.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data rank;
	set sess;
	by id notsorted ch notsorted;

	if first.id then Count = 0;
		Count + 1;
	if first.ch then Count = 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;id	ch	Count
1	P	1
1	P	2
1	P	3
1	O	1
2	W	1
2	I	1
2	W	2
2	I	2
2	P	1
2	P	2
2	O	1
3	O	1
3	P	1
3	P	2
4	W	1
4	I	1
4	W	2
4	I	2
4	W	3
4	I	3
4	P	1
4	P	2
4	O	1&lt;/PRE&gt;</description>
      <pubDate>Sun, 18 Aug 2019 15:22:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582011#M165475</guid>
      <dc:creator>P5C768</dc:creator>
      <dc:date>2019-08-18T15:22:23Z</dc:date>
    </item>
    <item>
      <title>Re: Running Count by Multiple Variables Where One Variable is NotSorted</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582013#M165476</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/22545"&gt;@P5C768&lt;/a&gt;&amp;nbsp; Pretty frequently asked question I think&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs;
input id $	ch $;*	Count ;
cards;
1	P	1
1	P	2
1	P	3
1	O	1
2	W	1
2	I	1
2	W	2
2	I	2
2	P	1
2	P	2
2	O	1
3	O	1
3	P	1
3	P	2
4	W	1
4	I	1
4	W	2
4	I	2
4	W	3
4	I	3
4	P	1
4	P	2
4	O	1
;

data want ;
if _n_=1 then do;
   dcl hash H () ;
   h.definekey  ("id","ch") ;
   h.definedata ("count") ;
   h.definedone () ;
end;
do until(last.id);
 set have;
 by id ;
 count=ifn( h.find()=0 , sum(count,1),1);
 h.replace();
 output;
end;
h.clear();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;/*Or*/&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs;
input id $	ch $;*	Count ;
cards;
1	P	1
1	P	2
1	P	3
1	O	1
2	W	1
2	I	1
2	W	2
2	I	2
2	P	1
2	P	2
2	O	1
3	O	1
3	P	1
3	P	2
4	W	1
4	I	1
4	W	2
4	I	2
4	W	3
4	I	3
4	P	1
4	P	2
4	O	1
;


data want;
 do until(last.id);
  set have;
  by id ;
  array t(99999) _temporary_ ;
  array j(99999)$ _temporary_;
  if ch in j then do;
  _k=whichc(ch,of j(*));
  count=sum(t(_k),1);
  t(_k)=count;
  end;
  else do;
  _n+1;
  j(_n)=ch;
  count=1;
  t(_n)=count;
  end;
  output;
 end;
call missing(of j(*),of t(*),_n);
drop _:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 18 Aug 2019 17:13:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582013#M165476</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-18T17:13:40Z</dc:date>
    </item>
    <item>
      <title>Re: Running Count by Multiple Variables Where One Variable is NotSorted</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582014#M165477</link>
      <description>&lt;P&gt;What is the point of the running count if the data is not sorted by that second variable?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is not at all clear what you are counting. In the first value of ID you seem to be counting the row number within the lowest level by group.&amp;nbsp; But in the second value of ID you seem to be counting the number of distinct CH values within that ID, but then it violates that after awhile.&lt;/P&gt;</description>
      <pubDate>Sun, 18 Aug 2019 16:43:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582014#M165477</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-18T16:43:00Z</dc:date>
    </item>
    <item>
      <title>Re: Running Count by Multiple Variables Where One Variable is NotSorted</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582015#M165478</link>
      <description>&lt;P&gt;Sort the data by IDs, do the counts, then "un-sort" by timestamp&lt;/P&gt;</description>
      <pubDate>Sun, 18 Aug 2019 16:48:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582015#M165478</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-18T16:48:26Z</dc:date>
    </item>
    <item>
      <title>Re: Running Count by Multiple Variables Where One Variable is NotSorted</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582022#M165483</link>
      <description>&lt;P&gt;Thank you so much&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;!&amp;nbsp; Worked like a charm!&lt;/P&gt;</description>
      <pubDate>Sun, 18 Aug 2019 19:16:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582022#M165483</guid>
      <dc:creator>P5C768</dc:creator>
      <dc:date>2019-08-18T19:16:01Z</dc:date>
    </item>
    <item>
      <title>Re: Running Count by Multiple Variables Where One Variable is NotSorted</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582026#M165484</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;No need to include any data in the hash.&amp;nbsp; Also if you going to include ID in the hash keys there is no need to clear it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;
  by id ;
  if _n_=1 then do;
    dcl hash h() ;
    h.definekey('id','ch') ;
    h.definedone() ;
  end;
  if first.id then count=0;
  count+(0=h.add());
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 18 Aug 2019 20:14:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582026#M165484</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-18T20:14:08Z</dc:date>
    </item>
    <item>
      <title>Re: Running Count by Multiple Variables Where One Variable is NotSorted</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582029#M165486</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp; Thank you Sir for the note. I'm afraid if we do not include the data portion, the hash object compile/execution time operaton anyway does an automatic inclusion of keys as data too consequently doubling the hash entry length and memory footprint;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example; To test what's in hash table , I added the output method to&amp;nbsp; your code to view the contents&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have end=z;
  by id ;
  if _n_=1 then do;
    dcl hash h() ;
    h.definekey('id','ch') ;
    h.definedone() ;
  end;
  if first.id then count=0;
  count+(0=h.add());
if z then h.output(dataset:'whats_in_hash_table');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You would notice Id and ch combo are populated as data values in hash too confirming my initial point.-&lt;EM&gt;whats_in_hash_table&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Secondly, your boolean approach to increment count is great had ch been sorted i'd think, but i'm afraid we would need the count previously enumerated parked somewhere to retrieve and replace be it in a hash table or an array. Methinks this where OP prolly was perhaps stuck.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thirdly, Clear() helps in freeing up memory after processing each by group. A way to manage memory. The combo idea of ID and CH as opposed to just CH helps testing and debugging effectively since visualizing the contents of hash tables makes it comfy to know what's going on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS&amp;nbsp; The best fun tricky part of hashes that cause confusion is, when the keys and data portion are the same , the data portion alone can be modified. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Aug 2019 22:35:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582029#M165486</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-18T22:35:11Z</dc:date>
    </item>
    <item>
      <title>Re: Running Count by Multiple Variables Where One Variable is NotSorted</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582102#M165510</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp; Thank you Sir for the note. I'm afraid if we do not include the data portion, the hash object compile/execution time operaton anyway does an automatic inclusion of keys as data too consequently doubling the hash entry length and memory footprint;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example; To test what's in hash table , I added the output method to&amp;nbsp; your code to view the contents&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have end=z;
  by id ;
  if _n_=1 then do;
    dcl hash h() ;
    h.definekey('id','ch') ;
    h.definedone() ;
  end;
  if first.id then count=0;
  count+(0=h.add());
if z then h.output(dataset:'whats_in_hash_table');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You would notice Id and ch combo are populated as data values in hash too confirming my initial point.-&lt;EM&gt;whats_in_hash_table&lt;/EM&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't really know how hash tables are stored in memory, but they obviously need to know the keys in order for them to work. So adding data variables also will by definition take more space.&amp;nbsp; Your test of what gets written to an output dataset probably has little to do with what is actually stored in memory.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Secondly, your boolean approach to increment count is great had ch been sorted i'd think, but i'm afraid we would need the count previously enumerated parked somewhere to retrieve and replace be it in a hash table or an array. Methinks this where OP prolly was perhaps stuck.&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not sure what you point is.&amp;nbsp; The normal variable COUNT has the current count.&amp;nbsp; Whether more logic is needed or not depends on what the original question actually wants.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Thirdly, Clear() helps in freeing up memory after processing each by group. A way to manage memory. The combo idea of ID and CH as opposed to just CH helps testing and debugging effectively since visualizing the contents of hash tables makes it comfy to know what's going on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS&amp;nbsp; The best fun tricky part of hashes that cause confusion is, when the keys and data portion are the same , the data portion alone can be modified. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_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;If you are going to clear the hash after each ID group then there is no need to store the ID in the hash.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Aug 2019 13:26:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-Count-by-Multiple-Variables-Where-One-Variable-is/m-p/582102#M165510</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-19T13:26:33Z</dc:date>
    </item>
  </channel>
</rss>

