<?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 Increment on out of order but distinct values? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Increment-on-out-of-order-but-distinct-values/m-p/749580#M235581</link>
    <description>&lt;P&gt;I have to sort by NAME and then DATE. Once this is complete I'm trying to determine the UNIQUE frequency of each ID occurring between any ID's -4.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently if ID = 1 then 2 then 1 it'll count as 3 when I'm needing that to count as 2. Below is examples.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input NAME $ Flag DATE ID RESULT;
format DATE yymmdd10.;
cards;
Joe 0 20190101 1 500
Joe 0 20190102 1 500
Joe 0 20190103 1 500
Joe 0 20190104 1  -4
Joe 1 20190121 1 500
Joe 0 20190122 1 500
Tom 0 20190104 1 500
Tom 0 20190101 1  -4
Tom 0 20190102 1 500
Tom 0 20190103 2 500
Tom 0 20190104 3 500
Tom 0 20190105 1 500
Tom 0 20190106 2 500
Tom 0 20190107 7  -4
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input NAME $ DATE ID RESULT want;
format DATE yymmdd10.;
cards;
Joe 0 20190101 1 500 1
Joe 0 20190102 1 500 1
Joe 0 20190103 1 500 1
Joe 0 20190104 1 -4 1
Joe 1 20190121 1 500 1
Joe 0 20190122 1 500 1
Tom 0 20190104 1 500 1
Tom 0 20190101 1 -4 1
Tom 0 20190102 1 500 1
Tom 0 20190103 2 500 2
Tom 0 20190104 3 500 3
Tom 0 20190105 1 500 3
Tom 0 20190106 2 500 3
Tom 0 20190107 7 -4  4
;
run;

*existing&amp;nbsp;non-working&amp;nbsp;code;
data want;
set have;
by name id notsorted;
retain have;
if first.name or lag(id) = -4 then card_Count = 1;
else if first.id then card_Count + 1; 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 22 Jun 2021 15:06:37 GMT</pubDate>
    <dc:creator>Krueger1</dc:creator>
    <dc:date>2021-06-22T15:06:37Z</dc:date>
    <item>
      <title>Increment on out of order but distinct values?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Increment-on-out-of-order-but-distinct-values/m-p/749580#M235581</link>
      <description>&lt;P&gt;I have to sort by NAME and then DATE. Once this is complete I'm trying to determine the UNIQUE frequency of each ID occurring between any ID's -4.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently if ID = 1 then 2 then 1 it'll count as 3 when I'm needing that to count as 2. Below is examples.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input NAME $ Flag DATE ID RESULT;
format DATE yymmdd10.;
cards;
Joe 0 20190101 1 500
Joe 0 20190102 1 500
Joe 0 20190103 1 500
Joe 0 20190104 1  -4
Joe 1 20190121 1 500
Joe 0 20190122 1 500
Tom 0 20190104 1 500
Tom 0 20190101 1  -4
Tom 0 20190102 1 500
Tom 0 20190103 2 500
Tom 0 20190104 3 500
Tom 0 20190105 1 500
Tom 0 20190106 2 500
Tom 0 20190107 7  -4
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input NAME $ DATE ID RESULT want;
format DATE yymmdd10.;
cards;
Joe 0 20190101 1 500 1
Joe 0 20190102 1 500 1
Joe 0 20190103 1 500 1
Joe 0 20190104 1 -4 1
Joe 1 20190121 1 500 1
Joe 0 20190122 1 500 1
Tom 0 20190104 1 500 1
Tom 0 20190101 1 -4 1
Tom 0 20190102 1 500 1
Tom 0 20190103 2 500 2
Tom 0 20190104 3 500 3
Tom 0 20190105 1 500 3
Tom 0 20190106 2 500 3
Tom 0 20190107 7 -4  4
;
run;

*existing&amp;nbsp;non-working&amp;nbsp;code;
data want;
set have;
by name id notsorted;
retain have;
if first.name or lag(id) = -4 then card_Count = 1;
else if first.id then card_Count + 1; 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Jun 2021 15:06:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Increment-on-out-of-order-but-distinct-values/m-p/749580#M235581</guid>
      <dc:creator>Krueger1</dc:creator>
      <dc:date>2021-06-22T15:06:37Z</dc:date>
    </item>
    <item>
      <title>Re: Increment on out of order but distinct values?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Increment-on-out-of-order-but-distinct-values/m-p/749624#M235608</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305357"&gt;@Krueger1&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to create some sort of lookup table to store the information about the distinct IDs you've already seen in the current group of observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would use a hash object for this purpose:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
if _n_=1 then do;
  dcl hash h();
  h.definekey('id');
  h.definedone();
end;
set have;
by name date;
if first.name or lag(result) = -4 then h.clear();
h.ref();
card_Count=h.num_items;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(This assumes that dataset HAVE has been sorted by NAME DATE, as you mentioned.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you're not familiar with hash objects, you may want to use a temporary array instead. Depending on the ID values, you can use&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;an array &lt;EM&gt;indexed&lt;/EM&gt; by ID values (assuming that ID values are integers between 1 and the array dimension, e.g., 999)&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by name date;
array c[999] _temporary_;
if first.name or lag(result) = -4 then call missing(of c[*]);
c[id]=1;
card_Count=sum(of c[*]);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;UL&gt;
&lt;LI&gt;or an array &lt;EM&gt;containing&lt;/EM&gt;&amp;nbsp;(up to, e.g., 999) distinct ID values:&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by name date;
array c[999] _temporary_;
if first.name or lag(result) = -4 then do;
  card_Count=0;
  call missing(of c[*]);
end;
if id ~in c then do;
  card_Count+1;
  c[card_Count]=id;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jun 2021 16:31:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Increment-on-out-of-order-but-distinct-values/m-p/749624#M235608</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-06-22T16:31:33Z</dc:date>
    </item>
    <item>
      <title>Re: Increment on out of order but distinct values?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Increment-on-out-of-order-but-distinct-values/m-p/749630#M235613</link>
      <description>&lt;P&gt;This is great and makes a lot of sense! My ID's in actual dataset are closer to GUID's then simple ID's but this is a great base and I'll definitely do some more research into HASH. Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jun 2021 16:43:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Increment-on-out-of-order-but-distinct-values/m-p/749630#M235613</guid>
      <dc:creator>Krueger1</dc:creator>
      <dc:date>2021-06-22T16:43:31Z</dc:date>
    </item>
    <item>
      <title>Re: Increment on out of order but distinct values?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Increment-on-out-of-order-but-distinct-values/m-p/749638#M235620</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305357"&gt;@Krueger1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;My ID's in actual dataset are closer to GUID's then simple ID's ...&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Unlike the first array solution, the second can be modified easily to work with &lt;EM&gt;character&lt;/EM&gt; IDs. Just insert a suitable length specification into the array definition, e.g.,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array c[999] $16 _temporary_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;if the IDs have a length &amp;lt;=16.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The hash object solution works with character IDs as well because it uses the type and length of the ID variable in dataset HAVE.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jun 2021 17:08:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Increment-on-out-of-order-but-distinct-values/m-p/749638#M235620</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-06-22T17:08:30Z</dc:date>
    </item>
  </channel>
</rss>

