<?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: Calculate distinct string and N of Distinct string at row level in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-distinct-string-and-N-of-Distinct-string-at-row-level/m-p/602319#M174366</link>
    <description>&lt;P&gt;is this what you're after?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table cat_count as&lt;BR /&gt;select cats(name1,name2,name3,name4) as ConcatenatedString&lt;BR /&gt;,count(*) as Volume&lt;BR /&gt;from have&lt;BR /&gt;group by ConcatenatedString&lt;BR /&gt;order by ConcatenatedString;&lt;BR /&gt;quit;&lt;/P&gt;</description>
    <pubDate>Thu, 07 Nov 2019 05:20:01 GMT</pubDate>
    <dc:creator>jarg</dc:creator>
    <dc:date>2019-11-07T05:20:01Z</dc:date>
    <item>
      <title>Calculate distinct string and N of Distinct string at row level</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-distinct-string-and-N-of-Distinct-string-at-row-level/m-p/602291#M174349</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;length name1 name2 name3 name4 $20;&lt;BR /&gt;name1='B'; name2= ''; name3= 'A'; name4='A';&lt;BR /&gt;output;&lt;BR /&gt;name1=' '; name2= 'B'; name3= ''; name4='';&lt;BR /&gt;output;&lt;BR /&gt;name1='A'; name2= 'A'; name3= 'A'; name4='';&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Want output as&lt;/P&gt;&lt;P&gt;row DistinctName DistinctN&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; A, B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;3&amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2019 01:18:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-distinct-string-and-N-of-Distinct-string-at-row-level/m-p/602291#M174349</guid>
      <dc:creator>Ram_SAS</dc:creator>
      <dc:date>2019-11-07T01:18:13Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate distinct string and N of Distinct string at row level</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-distinct-string-and-N-of-Distinct-string-at-row-level/m-p/602319#M174366</link>
      <description>&lt;P&gt;is this what you're after?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table cat_count as&lt;BR /&gt;select cats(name1,name2,name3,name4) as ConcatenatedString&lt;BR /&gt;,count(*) as Volume&lt;BR /&gt;from have&lt;BR /&gt;group by ConcatenatedString&lt;BR /&gt;order by ConcatenatedString;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2019 05:20:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-distinct-string-and-N-of-Distinct-string-at-row-level/m-p/602319#M174366</guid>
      <dc:creator>jarg</dc:creator>
      <dc:date>2019-11-07T05:20:01Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate distinct string and N of Distinct string at row level</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-distinct-string-and-N-of-Distinct-string-at-row-level/m-p/602403#M174421</link>
      <description>No. you are basically concatenating and counting. Instead, the concatenated string should have unique sorted string separated by comma (e.g. A,B or A) and the distinct N should count distinct strings (e.g. 2 or 1).</description>
      <pubDate>Thu, 07 Nov 2019 14:36:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-distinct-string-and-N-of-Distinct-string-at-row-level/m-p/602403#M174421</guid>
      <dc:creator>Ram_SAS</dc:creator>
      <dc:date>2019-11-07T14:36:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate distinct string and N of Distinct string at row level</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-distinct-string-and-N-of-Distinct-string-at-row-level/m-p/602489#M174458</link>
      <description>&lt;P&gt;Not pretty or elegant but should be robust and scalable if you actually have more variables:&lt;/P&gt;
&lt;PRE&gt;data have;
length name1 name2 name3 name4 $20;
input  name1 name2 name3 name4;
row+1;
datalines;
B .  A A
. B .  .
A A A .
;

proc transpose data=have out=trans (where=( not missing(col1)) drop=_name_);
by row;
var name1-name4;
run;
proc sql;
   create table temp as
   select distinct row,col1
   from trans;
quit;
data want;
   set temp;
   by row;
   /* need to set length of a target to string
      to at least longest possible so 20*number of variables+ number of variables
      minus 1 for commas.
   */
   length DistinctName $85; 
   retain DistinctName;
   if first.row then DistinctName=col1;
   else DistinctName= catx(',',DistinctName,col1);
   distinctn= countw(DistinctName,',');
   if last.row;
   drop col1;
run;&lt;/PRE&gt;
&lt;P&gt;Time to learn how to write a data step. That list of variables and output statements gets cumbersome quickly.&lt;/P&gt;
&lt;P&gt;Also use the code box on the forum opened with the {I} or "running man" icons for posting code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And just for curiosity how will that comma delimited list of "names" actually be used?&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2019 17:03:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-distinct-string-and-N-of-Distinct-string-at-row-level/m-p/602489#M174458</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-07T17:03:29Z</dc:date>
    </item>
  </channel>
</rss>

