<?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 no. of unique values only in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45751#M9467</link>
    <description>Thank you for the reply.&lt;BR /&gt;
&lt;BR /&gt;
I had 100 character variables.&lt;BR /&gt;
&lt;BR /&gt;
Do I have to sort every one individually and then follow this procedure of nodupkey for every variable? I feel that would be time consuming. &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Is there any way where I could do it without having to sort the data?&lt;BR /&gt;
&lt;BR /&gt;
Like in a Proc print you could specify :&lt;BR /&gt;
sum var_name;&lt;BR /&gt;
Is there any method similar to this in SAS  where in I could say count(distinct (var_name)) without using proc SQL?</description>
    <pubDate>Thu, 08 Jul 2010 23:35:45 GMT</pubDate>
    <dc:creator>rockerd</dc:creator>
    <dc:date>2010-07-08T23:35:45Z</dc:date>
    <item>
      <title>How to count no. of unique values only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45747#M9463</link>
      <description>Suppose I have a dataset with 100 char and 100 numeric variables and I want to count the no. of unique observations in char variables only ...&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
I know we could use the count distinct in Proc SQL but I wanted to know if there is any method using data step approach...&lt;BR /&gt;
&lt;BR /&gt;
Thank you,&lt;BR /&gt;
&lt;BR /&gt;
Rockerd.</description>
      <pubDate>Thu, 08 Jul 2010 08:28:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45747#M9463</guid>
      <dc:creator>rockerd</dc:creator>
      <dc:date>2010-07-08T08:28:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to count no. of unique values only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45748#M9464</link>
      <description>[pre]data test;&lt;BR /&gt;
  input char1 $ char2 $ num1 num2;&lt;BR /&gt;
  datalines;&lt;BR /&gt;
A B 1 2&lt;BR /&gt;
A B 1 2&lt;BR /&gt;
A C 1 2&lt;BR /&gt;
A C 1 2&lt;BR /&gt;
D B 4 2&lt;BR /&gt;
D B 1 2&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=work.test&lt;BR /&gt;
           out=char nodupkey;&lt;BR /&gt;
  by _char_;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=work.test&lt;BR /&gt;
           out=num nodupkey;&lt;BR /&gt;
  by _numeric_;&lt;BR /&gt;
run;</description>
      <pubDate>Thu, 08 Jul 2010 08:46:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45748#M9464</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2010-07-08T08:46:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to count no. of unique values only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45749#M9465</link>
      <description>You could sort the dataset by the variable you want to count and then use the retain function to count every time you find a new value.&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=x;&lt;BR /&gt;
   by CharVar1;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data x1(keep=count);&lt;BR /&gt;
   set x end=eof;&lt;BR /&gt;
   by CharVar1;&lt;BR /&gt;
&lt;BR /&gt;
   retain count 0;&lt;BR /&gt;
&lt;BR /&gt;
   if first.CharVar1 then count=count+1;&lt;BR /&gt;
     &lt;BR /&gt;
   if eof then output;&lt;BR /&gt;
run;</description>
      <pubDate>Thu, 08 Jul 2010 08:49:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45749#M9465</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-07-08T08:49:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to count no. of unique values only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45750#M9466</link>
      <description>Ron Fehd (the Macro Maven) has written a couple of macros that do what you are asking.  Especially his %showall.  See &lt;A href="http://www2.sas.com/proceedings/sugi22/POSTERS/PAPER236.PDF" target="_blank"&gt;http://www2.sas.com/proceedings/sugi22/POSTERS/PAPER236.PDF&lt;/A&gt;</description>
      <pubDate>Thu, 08 Jul 2010 19:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45750#M9466</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-07-08T19:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to count no. of unique values only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45751#M9467</link>
      <description>Thank you for the reply.&lt;BR /&gt;
&lt;BR /&gt;
I had 100 character variables.&lt;BR /&gt;
&lt;BR /&gt;
Do I have to sort every one individually and then follow this procedure of nodupkey for every variable? I feel that would be time consuming. &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Is there any way where I could do it without having to sort the data?&lt;BR /&gt;
&lt;BR /&gt;
Like in a Proc print you could specify :&lt;BR /&gt;
sum var_name;&lt;BR /&gt;
Is there any method similar to this in SAS  where in I could say count(distinct (var_name)) without using proc SQL?</description>
      <pubDate>Thu, 08 Jul 2010 23:35:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45751#M9467</guid>
      <dc:creator>rockerd</dc:creator>
      <dc:date>2010-07-08T23:35:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to count no. of unique values only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45752#M9468</link>
      <description>I think PROC FREQ is what you need.  TABLES _CHAR_;</description>
      <pubDate>Thu, 08 Jul 2010 23:58:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45752#M9468</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2010-07-08T23:58:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to count no. of unique values only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45753#M9469</link>
      <description>actually I was trying for proc freq ...&lt;BR /&gt;
&lt;BR /&gt;
but when I say out = dataset_name .. it outputs only the last char variable it has encountered ... so counting for distinct values is a little difficult with proc freq.</description>
      <pubDate>Fri, 09 Jul 2010 00:28:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45753#M9469</guid>
      <dc:creator>rockerd</dc:creator>
      <dc:date>2010-07-09T00:28:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to count no. of unique values only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45754#M9470</link>
      <description>[pre]ods output onewayfreqs=freq;[/pre]</description>
      <pubDate>Fri, 09 Jul 2010 01:57:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45754#M9470</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2010-07-09T01:57:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to count no. of unique values only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45755#M9471</link>
      <description>Hi&lt;BR /&gt;
&lt;BR /&gt;
As I understand your question you're actually not looking for a count of unique observations but rather for a count of unique values per character variable.&lt;BR /&gt;
&lt;BR /&gt;
If so then you have to scan the data set per variable. Luckily there are PROC's doing this for us - and they do it normally more efficient than we would code it.&lt;BR /&gt;
&lt;BR /&gt;
Below approach uses Proc Freq to get a "record" per character variable and distinct value of this variable (missings included) and then uses ODS Output to collect the result and write it to a data set.&lt;BR /&gt;
&lt;BR /&gt;
ods _all_ close;&lt;BR /&gt;
ods output OneWayFreqs=work.Freqs(keep=table)   ;&lt;BR /&gt;
&lt;BR /&gt;
proc freq data=sashelp.class;&lt;BR /&gt;
  table _character_ /nocum nopercent missing;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
ods output close;&lt;BR /&gt;
ods listing;&lt;BR /&gt;
&lt;BR /&gt;
title "Unique Values per Character Variable";&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  select substr(Table,6) as Variable_Name format=$32. ,count(*) as N_UniqueValues format=8.&lt;BR /&gt;
    from work.Freqs&lt;BR /&gt;
  group by Table&lt;BR /&gt;
  ;&lt;BR /&gt;
quit;&lt;BR /&gt;
title;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
      <pubDate>Fri, 09 Jul 2010 01:57:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45755#M9471</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-07-09T01:57:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to count no. of unique values only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45756#M9472</link>
      <description>Hmmmmm...&lt;BR /&gt;
I think there will be a way to get the result you want.try it.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc transpose data=sashelp.class out=class;&lt;BR /&gt;
  by _character_;&lt;BR /&gt;
  var _character_;&lt;BR /&gt;
run;&lt;BR /&gt;
proc sort data=class(keep= _name_ col1) out=temp;&lt;BR /&gt;
  by _name_ col1;&lt;BR /&gt;
run;&lt;BR /&gt;
data freq;&lt;BR /&gt;
  set temp;&lt;BR /&gt;
  by _name_ col1;&lt;BR /&gt;
  retain count;&lt;BR /&gt;
  if first._name_ then count=0;&lt;BR /&gt;
  if last.col1 then count+1;&lt;BR /&gt;
  if last._name_ then output;&lt;BR /&gt;
  keep _name_ count;&lt;BR /&gt;
run;&lt;BR /&gt;
proc print data=freq(rename=( _name_ = var_name)) noobs;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Fri, 09 Jul 2010 12:57:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45756#M9472</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2010-07-09T12:57:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to count no. of unique values only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45757#M9473</link>
      <description>Patrick and KSharp., thank you for the reply.&lt;BR /&gt;
&lt;BR /&gt;
The method suggested by Patrick works for me. &lt;BR /&gt;
&lt;BR /&gt;
Again thanks &lt;BR /&gt;
&lt;BR /&gt;
Rockerd.</description>
      <pubDate>Sat, 10 Jul 2010 00:54:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45757#M9473</guid>
      <dc:creator>rockerd</dc:creator>
      <dc:date>2010-07-10T00:54:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to count no. of unique values only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45758#M9474</link>
      <description>&amp;gt; The method suggested by Patrick works for me. &lt;BR /&gt;
&lt;BR /&gt;
More easily obtained with PROC FREQ LEVELS &lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
 Number of Variable Levels&lt;BR /&gt;
&lt;BR /&gt;
   Variable      Levels&lt;BR /&gt;
   --------------------&lt;BR /&gt;
   Name              19&lt;BR /&gt;
   Sex                2&lt;BR /&gt;
[/pre]</description>
      <pubDate>Sat, 10 Jul 2010 02:23:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45758#M9474</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2010-07-10T02:23:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to count no. of unique values only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45759#M9475</link>
      <description>Hi . data null;&lt;BR /&gt;
You must work hard on documentation. It is pretty easy.&lt;BR /&gt;
I admire you really. :_)</description>
      <pubDate>Sat, 10 Jul 2010 11:58:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45759#M9475</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2010-07-10T11:58:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to count no. of unique values only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45760#M9476</link>
      <description>Thank you Data _NULL_ for reminding me of these options.  I believe that the LEVELS option is an alias for the NLEVELS option. Without a TABLE statement you not only get the summary shown by Data _NULL_, but also a summary for each level of each variable.  Adding a TABLE statement yields only the overall summary for the selected variables.  Since you want all variables try the following:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc freq data=sashelp.class nlevels;&lt;BR /&gt;
table _all_ / noprint;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Mon, 12 Jul 2010 06:40:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-no-of-unique-values-only/m-p/45760#M9476</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-07-12T06:40:59Z</dc:date>
    </item>
  </channel>
</rss>

