<?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 values in each row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261560#M50930</link>
    <description>Sorry. I can not . It seems that Chinese Government has already block some Java Script at this forum . I can't see the running man icon or any icon above, I even can't edit my response . I have to use HTML code to reply. Fortunately I can click the POST button ,otherwise I have to leave this forum .</description>
    <pubDate>Wed, 06 Apr 2016 01:13:18 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2016-04-06T01:13:18Z</dc:date>
    <item>
      <title>how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261203#M50776</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm wondering if sas could count distinct values in each row. For example, my data looks similar like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID Col1 Col2 Col3 Col4&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; 12 &amp;nbsp; &amp;nbsp; 12 &amp;nbsp; &amp;nbsp;13 &amp;nbsp; &amp;nbsp; 14&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp;23 &amp;nbsp; &amp;nbsp;34 &amp;nbsp; &amp;nbsp;34 &amp;nbsp; &amp;nbsp; 34&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp;55 &amp;nbsp; &amp;nbsp;67 &amp;nbsp; &amp;nbsp;. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;45&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;33 &amp;nbsp; &amp;nbsp;33 &amp;nbsp; &amp;nbsp;33 &amp;nbsp; &amp;nbsp; 33&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I woud like to have the count of unique values that each ID has, something like:&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; # Uniquevalues&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciate any ideas that may help. Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 19:28:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261203#M50776</guid>
      <dc:creator>huhuhu</dc:creator>
      <dc:date>2016-04-04T19:28:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261222#M50784</link>
      <description>&lt;P&gt;Assumptions make a difference in how complex the code becomes.&amp;nbsp; I'm going to assume:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;You may have missing values in your data&lt;/LI&gt;
&lt;LI&gt;The sample data is representative, but you actually have more variables than 4.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Since the original variables are not being kept in the output, they can be changed by the program&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Here's one way to go about it (assuming you actually have 50 variables):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array col {50};&lt;/P&gt;
&lt;P&gt;call sortn (of col1-col50);&lt;/P&gt;
&lt;P&gt;unique_vals = (col1 &amp;gt; .);&lt;/P&gt;
&lt;P&gt;do _n_=2 to 50;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if col{_n_} &amp;gt; col{_n_-1} then unique_vals + 1;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;keep id unique_vals;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the assumptions should be different, we can always adjust.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 20:23:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261222#M50784</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-04-04T20:23:51Z</dc:date>
    </item>
    <item>
      <title>Re: how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261231#M50786</link>
      <description>&lt;P&gt;Thank you for your input and assumptions. I tried your codes, but in the new dataset it showed all zeros in column "&lt;SPAN&gt; unique_vals". I just change the number 50 to exact columns I have. Any suggestions?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Those assumptions are correct. I also would like to be clear that the numbers are randomly organized and not in any orders in each row (like NOT col4&amp;gt;col3&amp;gt;col2&amp;gt;col1); at the same time, there could be more than one missing values in each row, which can be at any columns.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 20:58:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261231#M50786</guid>
      <dc:creator>huhuhu</dc:creator>
      <dc:date>2016-04-04T20:58:01Z</dc:date>
    </item>
    <item>
      <title>Re: how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261232#M50787</link>
      <description>&lt;P&gt;Another way using transpose with sql:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc transpose data=have out=want;&lt;BR /&gt;by id;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table uniq as&lt;BR /&gt;select id, count(distinct col1) as Unique_values from want group by id;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 21:04:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261232#M50787</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2016-04-04T21:04:14Z</dc:date>
    </item>
    <item>
      <title>Re: how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261238#M50791</link>
      <description>&lt;P&gt;OK, the program is short enough that you could probably post your log.&amp;nbsp; It should be an easy fix.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2016 21:17:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261238#M50791</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-04-04T21:17:37Z</dc:date>
    </item>
    <item>
      <title>Re: how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261256#M50797</link>
      <description>&lt;P&gt;Another array option:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input ID Col1 Col2 Col3 Col4;
	cards;
1   12     12    13     14
2    23    34    34     34
3    55    67    .        45
4    33    33    33     33
;
run;

data want;
	set have;
	array temp(50) _temporary_;
	array col(50) col1-col50;

	do i=1 to dim(col);
		if col(i) not in temp then
			temp(i)=col(i);
	end;

	ct=n(of temp(*));
	call missing (of temp(*));
	keep id ct;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Apr 2016 01:17:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261256#M50797</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-04-05T01:17:39Z</dc:date>
    </item>
    <item>
      <title>Re: how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261261#M50799</link>
      <description>&lt;PRE&gt;
It is IML thing.



data have;
	input ID Col1 Col2 Col3 Col4;
	cards;
1   12     12    13     14
2    23    34    34     34
3    55    67    .        45
4    33    33    33     33
5    .    .    .     .
;
run;
proc iml;
use have(keep=id);
read all var {id};
close;

use have(keep=col:);
read all var _num_ into x;
close;

n=countunique(x,'row')-(countmiss(x,'row')^=0);

create want var{id n};
append;
close;

quit;


&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Apr 2016 02:34:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261261#M50799</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-04-05T02:34:26Z</dc:date>
    </item>
    <item>
      <title>Re: how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261262#M50800</link>
      <description>&lt;PRE&gt;
OR Hash Table.




data have;
	input ID Col1 Col2 Col3 Col4;
	cards;
1   12     12    13     14
2    23    34    34     34
3    55    67    .        45
4    33    33    33     33
5    .    .    .     .
;
run;

data want;
 if _n_ eq 1 then do;
  declare hash h();
  h.definekey('k');
  h.definedone();
 end;
set have;
 array x{*} col:;
 do i=1 to dim(x);
  if not missing(x{i}) then do;k=x{i};h.replace();end;
 end;
 n=h.num_items;
 h.clear();
drop i k col:;
run;




&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Apr 2016 02:38:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261262#M50800</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-04-05T02:38:49Z</dc:date>
    </item>
    <item>
      <title>Re: how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261376#M50856</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp﻿&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;For some reason both of your posts in this thread&amp;nbsp;are truncated. Maybe you can try posting your code as SAS code?&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2016 13:26:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261376#M50856</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-04-05T13:26:46Z</dc:date>
    </item>
    <item>
      <title>Re: how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261385#M50863</link>
      <description>&lt;P&gt;smart way!&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2016 13:40:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261385#M50863</guid>
      <dc:creator>huhuhu</dc:creator>
      <dc:date>2016-04-05T13:40:48Z</dc:date>
    </item>
    <item>
      <title>Re: how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261388#M50865</link>
      <description>&lt;P&gt;It works! Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2016 13:42:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261388#M50865</guid>
      <dc:creator>huhuhu</dc:creator>
      <dc:date>2016-04-05T13:42:12Z</dc:date>
    </item>
    <item>
      <title>Re: how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261422#M50879</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4877"&gt;@Haikuo&lt;/a&gt;&amp;nbsp;; I suspect you are using Internet Explorer. IE has some "feature" such that code involving braces does not render correctly from this forum.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4877"&gt;@Haikuo&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;For some reason both of your posts in this thread&amp;nbsp;are truncated. Maybe you can try posting your code as SAS code?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2016 14:52:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261422#M50879</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-04-05T14:52:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261427#M50880</link>
      <description>&lt;P&gt;Yes, indeed, I was using IE. IMHO, the quality of this forum is not in par with SAS reputation.&amp;nbsp;If I use Chrome, I sometimes have a login issue. Another one&amp;nbsp;is of course, &amp;nbsp;The notorious Hash colon problem, the colon gets to turn into literal spelling. Wonder will it get ever fixed.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2016 15:00:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261427#M50880</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-04-05T15:00:02Z</dc:date>
    </item>
    <item>
      <title>Re: how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261495#M50906</link>
      <description>&lt;P&gt;Thanks to everyone's help! The problem already been solved.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2016 19:05:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261495#M50906</guid>
      <dc:creator>huhuhu</dc:creator>
      <dc:date>2016-04-05T19:05:46Z</dc:date>
    </item>
    <item>
      <title>Re: how to count distinct values in each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261560#M50930</link>
      <description>Sorry. I can not . It seems that Chinese Government has already block some Java Script at this forum . I can't see the running man icon or any icon above, I even can't edit my response . I have to use HTML code to reply. Fortunately I can click the POST button ,otherwise I have to leave this forum .</description>
      <pubDate>Wed, 06 Apr 2016 01:13:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-count-distinct-values-in-each-row/m-p/261560#M50930</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-04-06T01:13:18Z</dc:date>
    </item>
  </channel>
</rss>

