<?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: How to count distinct observation? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/736895#M229672</link>
    <description>&lt;P&gt;Thank you very much&amp;nbsp;&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I created a new variable because I do not know we can count directly from a subsetting as your approach by using SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your approach solved my problem perfectly. I just want to confirm one last thing from my understanding&lt;/P&gt;
&lt;P&gt;For your set of code below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   select geogn, count(*) as unique_types
  from (select distinct geogn, type from merge)
  group by geogn&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So, SAS will group the data by &lt;STRONG&gt;geogn&lt;/STRONG&gt;, then SAS will select the column &lt;STRONG&gt;geogn&lt;/STRONG&gt; and unique value of column &lt;STRONG&gt;type&lt;/STRONG&gt; from dataset &lt;STRONG&gt;merge&lt;/STRONG&gt;. Then, lastly SAS will perform the code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select geogn, count(*) as unique_types&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;, am I correct?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you and warm regards.&lt;/P&gt;</description>
    <pubDate>Mon, 26 Apr 2021 01:36:34 GMT</pubDate>
    <dc:creator>Phil_NZ</dc:creator>
    <dc:date>2021-04-26T01:36:34Z</dc:date>
    <item>
      <title>How to count distinct observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/736890#M229669</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Today I try to count the distinct observation of variable "Type" in my dataset. A part of my sample of the dataset&amp;nbsp;&lt;STRONG&gt;&lt;CODE class=" language-sas"&gt;merge_treat_con&lt;/CODE&gt;&lt;/STRONG&gt; is as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;TYPE	GEOGN	    yr
13016K	AUSTRALIA	2002
13016K	AUSTRALIA	2004
13016K	AUSTRALIA	2005
13016K	AUSTRALIA	2007
13016K	AUSTRALIA	2008
130347	AUSTRALIA	2001
130495	AUSTRALIA	2002
130495	AUSTRALIA	2004
130495	AUSTRALIA	2005
130496	AUSTRALIA	2004
130496	AUSTRALIA	2006
130496	AUSTRALIA	2007
130496	AUSTRALIA	2008
130560	AUSTRALIA	2002
130560	AUSTRALIA	2004
130560	AUSTRALIA	2005
130560	AUSTRALIA	2008
50234Q	FRANCE	    2004
50234Q	FRANCE  	2005
50234Q	FRANCE	    2006
50290W	FRANCE	    2004
50290W	FRANCE      2005
50290W	FRANCE	    2006
50343P	FRANCE	    2005
50343P	FRANCE	    2006
50367K	FRANCE	    1999&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What I want is the output&amp;nbsp;&lt;STRONG&gt;&lt;CODE class=" language-sas"&gt;unique_firms&lt;/CODE&gt; &lt;/STRONG&gt;is as below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;GEOGN        Unique_Type   
AUSTRALIA    5
FRANCE&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first procedure popping up in my head is PROC SQL, my code is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; proc sql;
 create table unique_firms as
  select distinct Type as uniquetype, count(uniquetype) as  Unique_Type, geogn
   from merge_treat_con
  group by geogn;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But the result is not the desired result (this is the result for my whole sample)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;uniquetype	Unique_Type	GEOGN
130042	    50089	UNITEDS
130045	    50089	UNITEDS
130056	    50089	UNITEDS
130062      50089	UNITEDS
130079  	50089	UNITEDS
130088	    50089	UNITEDS
130092	    1507	MEXICO
130104	    2380	ISRAEL
130113	    50089	UNITEDS
130117	    1361	BRAZIL
13012F	    939	    PERU
130165	    1361	BRAZIL
13016K  	7255	AUSTRALIA
13016L	    1615	FINLAND
130171	    1361	BRAZIL
130189	    1361	BRAZIL
130194	    1361	BRAZIL
13021K	    50089	UNITEDS&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Can you please hint me to sort it out?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you and warm regards.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Apr 2021 23:23:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/736890#M229669</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-04-25T23:23:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to count distinct observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/736893#M229670</link>
      <description>&lt;P&gt;Supplying results from full data is not helpful. From the subset you show as an example to start with is the way to show results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First thing is that your problem description is incomplete. You say: "Today I try to count the distinct observation of variable "Type" in my dataset". but then very clearly show that you want the distinct observation of type &lt;STRONG&gt;within the values of country.&lt;/STRONG&gt; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And sometimes it just plain easier to count after subsetting&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Proc sql;
   create table want as
   select geogn, count(*) as unique_types
  from (select distinct geogn, type from merge)&lt;BR /&gt;  group by geogn
  ;
run;&lt;/PRE&gt;
&lt;P&gt;generates something closer to what you show for want.&lt;/P&gt;
&lt;P&gt;I don't know why you are creating a new type variable when you say you only want a count&lt;/P&gt;</description>
      <pubDate>Mon, 26 Apr 2021 00:17:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/736893#M229670</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-26T00:17:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to count distinct observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/736895#M229672</link>
      <description>&lt;P&gt;Thank you very much&amp;nbsp;&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I created a new variable because I do not know we can count directly from a subsetting as your approach by using SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your approach solved my problem perfectly. I just want to confirm one last thing from my understanding&lt;/P&gt;
&lt;P&gt;For your set of code below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   select geogn, count(*) as unique_types
  from (select distinct geogn, type from merge)
  group by geogn&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So, SAS will group the data by &lt;STRONG&gt;geogn&lt;/STRONG&gt;, then SAS will select the column &lt;STRONG&gt;geogn&lt;/STRONG&gt; and unique value of column &lt;STRONG&gt;type&lt;/STRONG&gt; from dataset &lt;STRONG&gt;merge&lt;/STRONG&gt;. Then, lastly SAS will perform the code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select geogn, count(*) as unique_types&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;, am I correct?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you and warm regards.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Apr 2021 01:36:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/736895#M229672</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-04-26T01:36:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to count distinct observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/736896#M229673</link>
      <description>&lt;P&gt;Distinct used in the SQL and it's common to include your grouping variables at the start of your query for clarity.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; proc sql;
 create table unique_firms as
  select geogn, count(distinct Type) as  Unique_Type
   from merge_treat_con
  group by geogn;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Apr 2021 01:53:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/736896#M229673</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-26T01:53:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to count distinct observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/736901#M229676</link>
      <description>&lt;P&gt;You understand correctly.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Apr 2021 03:01:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/736901#M229676</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-26T03:01:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to count distinct observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/736904#M229679</link>
      <description>&lt;P&gt;Thank you so much,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code is even shorter and direct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks and warm regards.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Apr 2021 03:58:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/736904#M229679</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-04-26T03:58:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to count distinct observation?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/737017#M229720</link>
      <description>&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/count_distinct_by_group.sas" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/count_distinct_by_group.sas&lt;/A&gt;</description>
      <pubDate>Mon, 26 Apr 2021 15:22:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-distinct-observation/m-p/737017#M229720</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-26T15:22:22Z</dc:date>
    </item>
  </channel>
</rss>

