<?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 Counting with Condition #3 in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Counting-with-Condition-3/m-p/570431#M11944</link>
    <description>&lt;P&gt;Dear Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am stuck. Please help!&lt;/P&gt;&lt;P&gt;I am trying to label (count) without changing the sorting - by Condition.&lt;/P&gt;&lt;P&gt;I need the year of appearance as the first, and label new condition differently.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp;Condition Year&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; ZZ&amp;nbsp; 2005&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; XX&amp;nbsp; 2006&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; XX&amp;nbsp; 2007&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; ZZ&amp;nbsp; 2008&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; ZZ&amp;nbsp; 2009&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; YY&amp;nbsp; 2010&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; BB&amp;nbsp; 2006&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; BB&amp;nbsp; 2007&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; AA&amp;nbsp; 2008&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; AA&amp;nbsp; 2009&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; CC 2010&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp;Condition Year&amp;nbsp; Want&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; ZZ&amp;nbsp; 2005&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; XX&amp;nbsp; 2006&amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; XX&amp;nbsp; 2007&amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; ZZ&amp;nbsp; 2008&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; ZZ&amp;nbsp; 2009&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; YY&amp;nbsp; 2010&amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; BB&amp;nbsp; 2006&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; BB&amp;nbsp; 2007&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; AA&amp;nbsp; 2008&amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; AA&amp;nbsp; 2009&amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; CC 2010&amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;T&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 02 Jul 2019 05:28:11 GMT</pubDate>
    <dc:creator>tonoplast</dc:creator>
    <dc:date>2019-07-02T05:28:11Z</dc:date>
    <item>
      <title>Counting with Condition #3</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Counting-with-Condition-3/m-p/570431#M11944</link>
      <description>&lt;P&gt;Dear Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am stuck. Please help!&lt;/P&gt;&lt;P&gt;I am trying to label (count) without changing the sorting - by Condition.&lt;/P&gt;&lt;P&gt;I need the year of appearance as the first, and label new condition differently.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp;Condition Year&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; ZZ&amp;nbsp; 2005&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; XX&amp;nbsp; 2006&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; XX&amp;nbsp; 2007&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; ZZ&amp;nbsp; 2008&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; ZZ&amp;nbsp; 2009&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; YY&amp;nbsp; 2010&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; BB&amp;nbsp; 2006&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; BB&amp;nbsp; 2007&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; AA&amp;nbsp; 2008&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; AA&amp;nbsp; 2009&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; CC 2010&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp;Condition Year&amp;nbsp; Want&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; ZZ&amp;nbsp; 2005&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; XX&amp;nbsp; 2006&amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; XX&amp;nbsp; 2007&amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; ZZ&amp;nbsp; 2008&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; ZZ&amp;nbsp; 2009&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;ID1&amp;nbsp; YY&amp;nbsp; 2010&amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; BB&amp;nbsp; 2006&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; BB&amp;nbsp; 2007&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; AA&amp;nbsp; 2008&amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; AA&amp;nbsp; 2009&amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;ID2&amp;nbsp; CC 2010&amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;T&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2019 05:28:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Counting-with-Condition-3/m-p/570431#M11944</guid>
      <dc:creator>tonoplast</dc:creator>
      <dc:date>2019-07-02T05:28:11Z</dc:date>
    </item>
    <item>
      <title>Re: Counting with Condition #3</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Counting-with-Condition-3/m-p/570438#M11948</link>
      <description>&lt;P&gt;ID in the data set must be in the sorted order.&lt;/P&gt;
&lt;P&gt;Collect the distinct Condition into a String sufficiently spaced (STR here) in one DoW loop.&lt;/P&gt;
&lt;P&gt;Match the Condition in STR to determine its Order in it and write out the observation in the second DoW loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data Have;
input ID :$3. Condition :$2. Year;
datalines;
ID1  ZZ  2005
ID1  XX  2006
ID1  XX  2007
ID1  ZZ  2008
ID1  ZZ  2009
ID1  YY  2010
ID2  BB  2006
ID2  BB  2007 
ID2  AA  2008
ID2  AA  2009
ID2  CC 2010
;
run;

proc sort data = have;
by id;
run;

data want;
   length str $32767;
   do until(last.id);
      set have;
      by id;
      if find(str,Condition) = 0 then str = catx(' ', str, Condition);
   end;

   do until(last.id);
      set have;
      by id;
      want = findw(str, Condition, ' ', 'E');
      output;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2019 06:06:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Counting-with-Condition-3/m-p/570438#M11948</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-02T06:06:16Z</dc:date>
    </item>
    <item>
      <title>Re: Counting with Condition #3</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Counting-with-Condition-3/m-p/570439#M11949</link>
      <description>&lt;P&gt;Classic use of the hash object &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $ Condition $ Year;
datalines;
ID1  ZZ  2005
ID1  XX  2006
ID1  XX  2007
ID1  ZZ  2008
ID1  ZZ  2009
ID1  YY  2010
ID2  BB  2006
ID2  BB  2007 
ID2  AA  2008
ID2  AA  2009
ID2  CC 2010
;

data want(drop=rc _:);
   declare hash h();
   h.defineKey("ID", "Condition");
   h.defineData("Want");
   h.defineDone();
   _Want=0;

   do until (last.ID);
      set have;
      by ID;
      if h.find() ne 0 then do;
         _Want+1;
         Want=_Want;
         rc=h.add();
      end;
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Jul 2019 06:06:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Counting-with-Condition-3/m-p/570439#M11949</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-07-02T06:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: Counting with Condition #3</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Counting-with-Condition-3/m-p/570443#M11953</link>
      <description>&lt;P&gt;Thank you! This works great and you posted first, so I'll accept this as the asnwer.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2019 06:36:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Counting-with-Condition-3/m-p/570443#M11953</guid>
      <dc:creator>tonoplast</dc:creator>
      <dc:date>2019-07-02T06:36:37Z</dc:date>
    </item>
    <item>
      <title>Re: Counting with Condition #3</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Counting-with-Condition-3/m-p/570444#M11954</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works very well! I would have accepted this as the answer, but because of the order of the post, I'll have to accept the other one.&lt;/P&gt;&lt;P&gt;Thank you for introducing me to hash object. I'll have to look into it!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;T&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2019 06:38:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Counting-with-Condition-3/m-p/570444#M11954</guid>
      <dc:creator>tonoplast</dc:creator>
      <dc:date>2019-07-02T06:38:06Z</dc:date>
    </item>
  </channel>
</rss>

