<?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: Using first. to add  a count unique by two variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-first-to-add-a-count-unique-by-two-variables/m-p/701583#M214852</link>
    <description>&lt;P&gt;Presuming the data is grouped into contiguous blocks the syntax &lt;CODE&gt;BY var1 var2 NOTSORTED;&lt;/CODE&gt; can be used.&amp;nbsp; This assumption is made because group 5 is not sorted in alphabetical order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;data have; input
ID  CATEGORY $  wanted; datalines;
001  PIG        1
001  PIG        1
003  SHEEP      1
003  PIG        2
003  PIG        2
004  SHEEP      1
004  SHEEP      1
005  PIG        1
005  PIG        1
005  COW        2
005  SHEEP      3
;

data want;
  set have;
  by id category notsorted;
  if first.id then seq=0;
  if first.category then seq+1;
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Output&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="RichardADeVenezia_0-1606322652894.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51996iD20BE225CFC1A794/image-size/medium?v=v2&amp;amp;px=400" role="button" title="RichardADeVenezia_0-1606322652894.png" alt="RichardADeVenezia_0-1606322652894.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 25 Nov 2020 16:44:48 GMT</pubDate>
    <dc:creator>RichardDeVen</dc:creator>
    <dc:date>2020-11-25T16:44:48Z</dc:date>
    <item>
      <title>Using first. to add  a count unique by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-first-to-add-a-count-unique-by-two-variables/m-p/701572#M214845</link>
      <description>&lt;P&gt;How can I use a data step to create a count unique by ID and category. I want the same count to remain for non unique values. Please see example attached and the wanted counts. Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 16:28:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-first-to-add-a-count-unique-by-two-variables/m-p/701572#M214845</guid>
      <dc:creator>angel2</dc:creator>
      <dc:date>2020-11-25T16:28:34Z</dc:date>
    </item>
    <item>
      <title>Re: Using first. to add  a count unique by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-first-to-add-a-count-unique-by-two-variables/m-p/701575#M214847</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input (ID	CATEGORY) ($);*		WANTED COUNT;
cards;
001	PIG		1
001	PIG		1
003	SHEEP		1
003	PIG		2
003	PIG		2
004	SHEEP		1
004	SHEEP		1
005	PIG		1
005	PIG		1
005	COW		2
005	SHEEP		3
;

data want;
 set have;
 by id category notsorted;
 if first.id then count=1;
 else if first.category then count+1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Nov 2020 16:39:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-first-to-add-a-count-unique-by-two-variables/m-p/701575#M214847</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-11-25T16:39:35Z</dc:date>
    </item>
    <item>
      <title>Re: Using first. to add  a count unique by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-first-to-add-a-count-unique-by-two-variables/m-p/701583#M214852</link>
      <description>&lt;P&gt;Presuming the data is grouped into contiguous blocks the syntax &lt;CODE&gt;BY var1 var2 NOTSORTED;&lt;/CODE&gt; can be used.&amp;nbsp; This assumption is made because group 5 is not sorted in alphabetical order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;data have; input
ID  CATEGORY $  wanted; datalines;
001  PIG        1
001  PIG        1
003  SHEEP      1
003  PIG        2
003  PIG        2
004  SHEEP      1
004  SHEEP      1
005  PIG        1
005  PIG        1
005  COW        2
005  SHEEP      3
;

data want;
  set have;
  by id category notsorted;
  if first.id then seq=0;
  if first.category then seq+1;
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Output&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="RichardADeVenezia_0-1606322652894.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51996iD20BE225CFC1A794/image-size/medium?v=v2&amp;amp;px=400" role="button" title="RichardADeVenezia_0-1606322652894.png" alt="RichardADeVenezia_0-1606322652894.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 16:44:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-first-to-add-a-count-unique-by-two-variables/m-p/701583#M214852</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-11-25T16:44:48Z</dc:date>
    </item>
    <item>
      <title>Re: Using first. to add  a count unique by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-first-to-add-a-count-unique-by-two-variables/m-p/701592#M214857</link>
      <description>&lt;P&gt;Thank you so much - that's perfect!&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 17:02:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-first-to-add-a-count-unique-by-two-variables/m-p/701592#M214857</guid>
      <dc:creator>angel2</dc:creator>
      <dc:date>2020-11-25T17:02:19Z</dc:date>
    </item>
  </channel>
</rss>

