<?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: Cumulative counts of numbers in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-counts-of-numbers/m-p/880745#M347998</link>
    <description>&lt;P&gt;The more complicated way is to use a HASH object to store the counter for each coupon.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _n_=1 then do;
     declare hash h();
     h.definekey('coupon');
     h.definedata('count');
     h.definedone();
  end;
  set have;
  if h.find() then count=1;
  else count+1;
  h.replace();
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;                   customer_              coupon_
Obs    customer        id       coupon     value     count

  1      carl          1         4000        9         1
  2      carl          1         4001        7         1
  3      carl          1         4002        4         1
  4      carl          1         4003        3         1
  5      jess          2         4000        9         2
  6      jess          2         4004        7         1
  7      jess          2         4002        5         2
  8      jess          2         4001        3         2
  9      lucy          3         4004        8         2
 10      lucy          3         4000        6         3
 11      lucy          3         4002        5         3
 12      lucy          3         4008        2         1
 13      amii          4         4004        8         3
 14      amii          4         4000        7         4
 15      amii          4         4001        4         3
 16      amii          4         4002        3         4
&lt;/PRE&gt;</description>
    <pubDate>Wed, 14 Jun 2023 16:51:09 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-06-14T16:51:09Z</dc:date>
    <item>
      <title>Cumulative counts of numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-counts-of-numbers/m-p/880739#M347993</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking for a way to cumulatively count variable instances.&amp;nbsp; In the below dataset i want to count the first instance of each coupon 1&lt;/P&gt;&lt;P&gt;then the next time we see the same coupon then 2 and so forth.&amp;nbsp; For example: Jess 4000 should be 2, since Carl 4000&amp;nbsp; should be 1, and Amii 4000 4.&amp;nbsp; THANKS&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input customer $ customer_id coupon coupon_value;&lt;BR /&gt;datalines;&lt;BR /&gt;carl 1 4000 9&lt;BR /&gt;carl 1 4001 7&lt;BR /&gt;carl 1 4002 4&lt;BR /&gt;carl 1 4003 3&lt;BR /&gt;jess 2 4000 9&lt;BR /&gt;jess 2 4004 7&lt;BR /&gt;jess 2 4002 5&lt;BR /&gt;jess 2 4001 3&lt;BR /&gt;lucy 3 4004 8&lt;BR /&gt;lucy 3 4000 6&lt;BR /&gt;lucy 3 4002 5&lt;BR /&gt;lucy 3 4008 2&lt;BR /&gt;amii 4 4004 8&lt;BR /&gt;amii 4 4000 7&lt;BR /&gt;amii 4 4001 4&lt;BR /&gt;amii 4 4002 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 16:33:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-counts-of-numbers/m-p/880739#M347993</guid>
      <dc:creator>rkepple</dc:creator>
      <dc:date>2023-06-14T16:33:37Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative counts of numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-counts-of-numbers/m-p/880741#M347995</link>
      <description>&lt;P&gt;AND I need to retain the order need it sorted / counted by customer ID (1 --&amp;gt; n)&amp;nbsp; THANKS&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 16:39:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-counts-of-numbers/m-p/880741#M347995</guid>
      <dc:creator>rkepple</dc:creator>
      <dc:date>2023-06-14T16:39:04Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative counts of numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-counts-of-numbers/m-p/880742#M347996</link>
      <description>&lt;P&gt;The simple way to count like that it to order the data by the grouping variable and then just count them.&lt;/P&gt;
&lt;P&gt;But you don't seem to have a variable to indicate the original order.&lt;/P&gt;
&lt;P&gt;Perhaps you can just add one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data for_analysis;
  set have;
  row+1;
run;
proc sort data=for_analysis;
  by coupon row;
run;
data want;
  set for_analysis;
  by coupon;
  count+1;
  if first.coupon then count=1;
run;
proc sort data=want;
  by row;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Jun 2023 16:41:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-counts-of-numbers/m-p/880742#M347996</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-14T16:41:56Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative counts of numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-counts-of-numbers/m-p/880745#M347998</link>
      <description>&lt;P&gt;The more complicated way is to use a HASH object to store the counter for each coupon.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _n_=1 then do;
     declare hash h();
     h.definekey('coupon');
     h.definedata('count');
     h.definedone();
  end;
  set have;
  if h.find() then count=1;
  else count+1;
  h.replace();
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;                   customer_              coupon_
Obs    customer        id       coupon     value     count

  1      carl          1         4000        9         1
  2      carl          1         4001        7         1
  3      carl          1         4002        4         1
  4      carl          1         4003        3         1
  5      jess          2         4000        9         2
  6      jess          2         4004        7         1
  7      jess          2         4002        5         2
  8      jess          2         4001        3         2
  9      lucy          3         4004        8         2
 10      lucy          3         4000        6         3
 11      lucy          3         4002        5         3
 12      lucy          3         4008        2         1
 13      amii          4         4004        8         3
 14      amii          4         4000        7         4
 15      amii          4         4001        4         3
 16      amii          4         4002        3         4
&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Jun 2023 16:51:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-counts-of-numbers/m-p/880745#M347998</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-14T16:51:09Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative counts of numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-counts-of-numbers/m-p/880754#M348001</link>
      <description>&lt;P&gt;How many total Coupon identification numbers are there?&lt;/P&gt;
&lt;P&gt;Retain a bunch of elements in an array to hold counts.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   array c (4000:4008) _temporary_;
   c[coupon]+1;
   count=c[Coupon];
run;&lt;/PRE&gt;
&lt;P&gt;This does require knowing the number of coupons, that the coupon variable is indeed numeric.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 20:29:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-counts-of-numbers/m-p/880754#M348001</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-06-14T20:29:48Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative counts of numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-counts-of-numbers/m-p/880806#M348020</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/443080"&gt;@rkepple&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you like hash object solutions? Here's another one, just for fun:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
if _n_=1 then do;
  dcl hash h(suminc:'i');
  h.definekey('coupon');
  h.definedone();
end;
set have(in=i);
h.ref();
h.sum(sum:count);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Jun 2023 20:22:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-counts-of-numbers/m-p/880806#M348020</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-06-14T20:22:34Z</dc:date>
    </item>
  </channel>
</rss>

