<?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: Count distinct values in one column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262865#M51411</link>
    <description>&lt;P&gt;s/order by/group by/&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 11 Apr 2016 10:49:13 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2016-04-11T10:49:13Z</dc:date>
    <item>
      <title>Count distinct values in one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262852#M51403</link>
      <description>&lt;P&gt;I have a table in SAS which contains 3 variables:&lt;/P&gt;&lt;P&gt;- dossier_handler (last name first name)&lt;/P&gt;&lt;P&gt;- r_creation_date (ddmmmyyyy)&lt;/P&gt;&lt;P&gt;- distributionoption (7 possible options containing spaces and words)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to count how many of each distributionoption I can find in the table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is how the table could look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;dossier_manager&lt;/TD&gt;&lt;TD&gt;r_creation_date&lt;/TD&gt;&lt;TD&gt;distributionoption&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NameA&lt;/TD&gt;&lt;TD&gt;Date1&lt;/TD&gt;&lt;TD&gt;Option A&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NameA&lt;/TD&gt;&lt;TD&gt;Date2&lt;/TD&gt;&lt;TD&gt;Option B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NameB&lt;/TD&gt;&lt;TD&gt;Date3&lt;/TD&gt;&lt;TD&gt;Option A&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NameC&lt;/TD&gt;&lt;TD&gt;Date4&lt;/TD&gt;&lt;TD&gt;Option C&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NameC&lt;/TD&gt;&lt;TD&gt;Date5&lt;/TD&gt;&lt;TD&gt;Option F&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NameC&lt;/TD&gt;&lt;TD&gt;Date6&lt;/TD&gt;&lt;TD&gt;Option D&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NameD&lt;/TD&gt;&lt;TD&gt;Date7&lt;/TD&gt;&lt;TD&gt;Option A&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NameD&lt;/TD&gt;&lt;TD&gt;Date8&lt;/TD&gt;&lt;TD&gt;Option E&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NameE&lt;/TD&gt;&lt;TD&gt;Date9&lt;/TD&gt;&lt;TD&gt;Option G&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NameF&lt;/TD&gt;&lt;TD&gt;Date10&lt;/TD&gt;&lt;TD&gt;Option G&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is how my final result should look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;distributionoption&lt;/TD&gt;&lt;TD&gt;count&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Option A&lt;/TD&gt;&lt;TD&gt;755&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Option B&lt;/TD&gt;&lt;TD&gt;58&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Option C&lt;/TD&gt;&lt;TD&gt;142&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Option D&lt;/TD&gt;&lt;TD&gt;228&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Option E&lt;/TD&gt;&lt;TD&gt;71&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Option F&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Option G&lt;/TD&gt;&lt;TD&gt;565&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried count and distinct. I have tried creating a variable for each option and then using a where ("this option") this option +1. Neither of these options appear to be working.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Count and distinct code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=tables.focus;
by verdelingsoptie;
run;

proc sql;
create table tables.focus_ as
select count(distinct verdelingsoptie)
from tables.focus
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where (X=?) Y+1 code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tables.focus;
set tables.focus_;
by verdelingsoptie;

retain
  Fast_Track_1_erfgenaam
  Fast_Track_Leefgeld
  Fast_Track_Saldo
  Fast_Track_Sterkmaking
  Manuele_verdeelopdracht_kantoor
  Verdeling_notaris
  Verdeling_standaard
;

if first.verdelingsoptie
then do;
  Fast_Track_1_erfgenaam=0;
  Fast_Track_Leefgeld=0;
  Fast_Track_Saldo=0;
  Fast_Track_Sterkmaking=0;
  Manuele_verdeelopdracht_kantoor=0;
  Verdeling_notaris=0;
  Verdeling_standaard=0;
end;

select (verdelignsoptie);

when ('Fast Track 1 erfgenaam') Fast_Track_1_erfgenaam +1;
when ('Fast Track Leefgeld') Fast_Track_Leefgeld +1;
when ('Fast Track Saldo') Fast_Track_Saldo +1;
when ('Fast Track Sterkmaking') Fast_Track_Sterkmaking +1;
when ('Manuele verdeelopdracht kantoor') Manuele_verdeelopdracht_kantoor +1;
when ('Verdeling notaris') Verdeling_notaris +1;
when ('Verdeling standaard') Verdeling_standaard +1;

end;

if last.verdelingsoptie then output;
keep Fast_Track_1_erfgenaam Fast_Track_Leefgeld Fast_Track_Saldo Fast_Track_Sterkmaking Manuele_verdeelopdracht_kantoor Verdeling_notaris Verdeling_standaard;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The count/distinct script results in a table that says there are 7 distributionoptions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The where (X=?) Y+1 results in some errors:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      528:7   529:7   530:7   531:7   532:7   533:7   534:7
NOTE: Variable verdelignsoptie is uninitialized.
NOTE: Variable first.verdelingsoptie is uninitialized.
NOTE: Variable last.verdelingsoptie is uninitialized.
NOTE: Invalid numeric data, 'Fast Track 1 erfgenaam' , at line 528 column 7.
_TEMG002=455 Fast_Track_1_erfgenaam=1 Fast_Track_Leefgeld=. Fast_Track_Saldo=.
Fast_Track_Sterkmaking=. Manuele_verdeelopdracht_kantoor=. Verdeling_notaris=.
Verdeling_standaard=. first.verdelingsoptie=0 verdelignsoptie=. last.verdelingsoptie=0 _ERROR_=1
_N_=1
NOTE: There were 1 observations read from the data set TABLES.FOCUS_.
NOTE: The data set TABLES.FOCUS has 0 observations and 7 variables.
NOTE: DATA statement used (Total process time):
      real time           0.04 seconds
      cpu time            0.03 seconds&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2016 09:51:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262852#M51403</guid>
      <dc:creator>Yves_Boonen</dc:creator>
      <dc:date>2016-04-11T09:51:35Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct values in one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262854#M51405</link>
      <description>&lt;P&gt;In SQL I would do something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;select distributionoptions, count (*)&lt;/P&gt;&lt;P&gt;from tables.focus&lt;/P&gt;&lt;P&gt;group by distributionoptions&lt;/P&gt;&lt;P&gt;save as tables.focus_&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2016 09:54:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262854#M51405</guid>
      <dc:creator>Yves_Boonen</dc:creator>
      <dc:date>2016-04-11T09:54:48Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct values in one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262859#M51407</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your first code:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table TABLES.FOCUS_ as
  select  count(*) as COUNT
  from    TABLES.FOCUS
  group by DISTRIBUTIONOPTION;
quit;&lt;/PRE&gt;
&lt;P&gt;Should work fine. &amp;nbsp;I note the code you post, doesn't match the names given in the description, so I used the description variable names. &amp;nbsp;In your second code, you have:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; verdelingsoptie&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which is not in that dataset, which is why you have problems. &amp;nbsp;Could it be that you meant to use focus dataset rather than focus_? &amp;nbsp;Although, in the description again, that variable is not mentioned - columns are:&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;dossier_manager&lt;/TD&gt;
&lt;TD&gt;r_creation_date&lt;/TD&gt;
&lt;TD&gt;distributionoption&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Mon, 11 Apr 2016 10:28:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262859#M51407</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-04-11T10:28:03Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct values in one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262860#M51408</link>
      <description>&lt;P&gt;The table I start from is called "focus" in my "tables" library (tables.focus).&lt;/P&gt;&lt;P&gt;I want to perform the count on the variable "verdelingsoptie" (distributionoption in English).&lt;/P&gt;&lt;P&gt;After the count is complete, I want to store the new data in a table called "focus_" in my "tables" library (tables.focus_).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I use your suggestion, which I tried also, this is the result:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table tables.focus_ as
select count(*) as count
from tables.focus
order by verdelingsoptie
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Result:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;count&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;455&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;455&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;455&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;455&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;455&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;455&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;455&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;455&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;455&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;455&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And that for 455 rows, because that's how many rows of data "tables.focus" contains.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2016 10:38:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262860#M51408</guid>
      <dc:creator>Yves_Boonen</dc:creator>
      <dc:date>2016-04-11T10:38:46Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct values in one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262861#M51409</link>
      <description>&lt;P&gt;Sorry, you haven't used my code. &amp;nbsp;The key part of the SQL is the group by:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table TABLES.FOCUS_ as
  select  VERDELINGSOPTIE,        /* &amp;lt;= added, as we are grouping by */&lt;BR /&gt;          count(*) as COUNT
  from    TABLES.FOCUS
  group by VERDELINGSOPTIE;   /*  &amp;lt;--- this part here is important! */
quit;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Apr 2016 10:42:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262861#M51409</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-04-11T10:42:17Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct values in one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262863#M51410</link>
      <description>&lt;P&gt;Mondays cause me to be like "order by" = "group by". After changing that, it did work like you said. Thanks a dozen! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2016 10:46:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262863#M51410</guid>
      <dc:creator>Yves_Boonen</dc:creator>
      <dc:date>2016-04-11T10:46:09Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct values in one column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262865#M51411</link>
      <description>&lt;P&gt;s/order by/group by/&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2016 10:49:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-values-in-one-column/m-p/262865#M51411</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-04-11T10:49:13Z</dc:date>
    </item>
  </channel>
</rss>

