<?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 a string in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544875#M7953</link>
    <description>&lt;P&gt;Here is another version. Look at the first character of PHONE. Count it. Compress the first character from the Phone. Repeat the above step until the phone has left with one or no character.&lt;/P&gt;
&lt;PRE&gt;data have;
input NAME $ PHONE $;
datalines;
Ana 21212
Pablo 234234
Hana 1111111
;
run;

data want;
   set have;
   ph = phone;
   count = 0;
   do until(len &amp;lt;= 1);
      d = char(ph,1);
      count + 1;
      ph = compress(ph,d);
      len = length(ph);
   end;
drop ph len d;
run;

Obs 	NAME 	PHONE 	count
1 	Ana 	21212 	2
2 	Pablo 	234234 	3
3 	Hana 	1111111 	1
&lt;/PRE&gt;</description>
    <pubDate>Thu, 21 Mar 2019 14:34:42 GMT</pubDate>
    <dc:creator>KachiM</dc:creator>
    <dc:date>2019-03-21T14:34:42Z</dc:date>
    <item>
      <title>Count distinct values ​​in a string</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544711#M7913</link>
      <description>&lt;P&gt;I would like to know how I can count distinct characters in a string&lt;/P&gt;&lt;P&gt;example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NAME&amp;nbsp; &amp;nbsp;PHONE&amp;nbsp; &amp;nbsp;CHAR_DISTINCT&lt;/P&gt;&lt;P&gt;Ana&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;21212&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;Pablo&amp;nbsp; &amp;nbsp; 234234&amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;Hana&amp;nbsp; &amp;nbsp; 1111111&amp;nbsp; &amp;nbsp; 1&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2019 21:33:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544711#M7913</guid>
      <dc:creator>a_ramos</dc:creator>
      <dc:date>2019-03-20T21:33:34Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct values ​​in a string</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544716#M7914</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input NAME $ PHONE $;
datalines;
Ana 21212
Pablo 234234
Hana 1111111
;

data want(keep=NAME PHONE CHAR_DISTINCT);
   set have;
   newphone=substr(PHONE, 1, 1);
   do i=2 to length(PHONE);
      found=find(newphone, substr(PHONE, i, 1), 'it');
      if found=0 then newphone=cats(newphone, substr(PHONE, i, 1));
   end;
   CHAR_DISTINCT=length(newphone);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Mar 2019 22:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544716#M7914</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-03-20T22:54:56Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct values ​​in a string</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544723#M7918</link>
      <description>&lt;P&gt;Or using PRXCHANGE Function&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   array t(100) $  _temporary_;
   call missing(of t(*));
   do _n_=1 to length(PHONE);
      t(_n_)=char(PHONE,_n_);
   end;
   call sortc(of t(*));
   CHAR_DISTINCT=length(prxchange('s/([0-9])\1+/$1/', -1, cats(of t(*))));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Mar 2019 07:41:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544723#M7918</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-03-21T07:41:18Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct values ​​in a string</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544775#M7930</link>
      <description>&lt;P&gt;Just for completeness, a solution with SQL.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input NAME $ PHONE $;
datalines;
Ana 21212
Pablo 234234
Hana 1111111
;
run;

data trans;
set have;
length digit $1;
do i = 1 to length(phone);
  digit = substr(phone,i,1);
  output;
end;
drop i;
run;

proc sql;
create table want as
select
  name,
  phone,
  count(distinct digit) as char_distinct
from trans
group by name, phone;
quit;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;                      char_
NAME      PHONE     distinct

Ana      21212          2   
Hana     1111111        1   
Pablo    234234         3   
&lt;/PRE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;'s first solution is the best, as it solves everything in one sequential pass through have.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Mar 2019 07:52:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544775#M7930</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-21T07:52:01Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct values ​​in a string</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544820#M7937</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Just for completeness, a solution with Hash Table.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input NAME $ PHONE $;
datalines;
Ana 21212
Pablo 234234
Hana 1111111
;
run;
data want;
 if _n_=1 then do;
  length k $ 1;
  declare hash h();
  h.definekey('k');
  h.definedone();
 end;
set have;
 do i = 1 to length(phone);
  k = char(phone,i);
  h.ref(); 
end;
count=h.num_items;
h.clear();
drop k i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Mar 2019 11:57:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544820#M7937</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-03-21T11:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct values ​​in a string</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544875#M7953</link>
      <description>&lt;P&gt;Here is another version. Look at the first character of PHONE. Count it. Compress the first character from the Phone. Repeat the above step until the phone has left with one or no character.&lt;/P&gt;
&lt;PRE&gt;data have;
input NAME $ PHONE $;
datalines;
Ana 21212
Pablo 234234
Hana 1111111
;
run;

data want;
   set have;
   ph = phone;
   count = 0;
   do until(len &amp;lt;= 1);
      d = char(ph,1);
      count + 1;
      ph = compress(ph,d);
      len = length(ph);
   end;
drop ph len d;
run;

Obs 	NAME 	PHONE 	count
1 	Ana 	21212 	2
2 	Pablo 	234234 	3
3 	Hana 	1111111 	1
&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Mar 2019 14:34:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544875#M7953</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-03-21T14:34:42Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct values ​​in a string</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544904#M7958</link>
      <description>&lt;P&gt;Note that among the previous solutions only &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;'s produces a count of zero in the case of an empty input string. Of course, you could adapt the other solutions correspondingly if needed. For example, using COMPRESS (like in &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17813"&gt;@KachiM&lt;/a&gt;'s solution):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_t);
set have;
_t=phone;
do CHAR_DISTINCT=0 by 1 while(_t ne ' ');
  _t=compress(_t,first(_t));
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To obtain CHAR_DISTINCT=1 for missing PHONE replace the DO statement with:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do CHAR_DISTINCT=1 by 1 until(_t=' ');&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Mar 2019 15:42:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/544904#M7958</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-03-21T15:42:21Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct values ​​in a string</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/545217#M7990</link>
      <description>&lt;BR /&gt;Thank you! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Fri, 22 Mar 2019 13:36:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-distinct-values-in-a-string/m-p/545217#M7990</guid>
      <dc:creator>a_ramos</dc:creator>
      <dc:date>2019-03-22T13:36:13Z</dc:date>
    </item>
  </channel>
</rss>

