<?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 counting unique versions of a string across variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/counting-unique-versions-of-a-string-across-variables/m-p/389179#M277499</link>
    <description>&lt;P&gt;I have a dataset that looks like this:&lt;/P&gt;&lt;P&gt;obs ID1 ID2 ID3&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; X&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Y&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Z&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;B&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;C&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;D&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; D&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; E&lt;/P&gt;&lt;P&gt;4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; M&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; N&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to produce a new variable that counts the number of unique strings across the ID variables. So it would produce this:&lt;/P&gt;&lt;P&gt;obs ID1 ID2 ID3 Count&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; X&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Y&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Z&amp;nbsp;&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;B&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;C&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;D&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; D&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; M&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; N&amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suggestions?&lt;/P&gt;</description>
    <pubDate>Fri, 18 Aug 2017 18:03:09 GMT</pubDate>
    <dc:creator>TechRA</dc:creator>
    <dc:date>2017-08-18T18:03:09Z</dc:date>
    <item>
      <title>counting unique versions of a string across variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-unique-versions-of-a-string-across-variables/m-p/389179#M277499</link>
      <description>&lt;P&gt;I have a dataset that looks like this:&lt;/P&gt;&lt;P&gt;obs ID1 ID2 ID3&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; X&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Y&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Z&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;B&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;C&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;D&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; D&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; E&lt;/P&gt;&lt;P&gt;4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; M&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; N&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to produce a new variable that counts the number of unique strings across the ID variables. So it would produce this:&lt;/P&gt;&lt;P&gt;obs ID1 ID2 ID3 Count&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; X&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Y&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Z&amp;nbsp;&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;B&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;C&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;D&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; D&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; E&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; M&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; N&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; N&amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suggestions?&lt;/P&gt;</description>
      <pubDate>Fri, 18 Aug 2017 18:03:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-unique-versions-of-a-string-across-variables/m-p/389179#M277499</guid>
      <dc:creator>TechRA</dc:creator>
      <dc:date>2017-08-18T18:03:09Z</dc:date>
    </item>
    <item>
      <title>Re: counting unique versions of a string across variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-unique-versions-of-a-string-across-variables/m-p/389191#M277500</link>
      <description>&lt;P&gt;1. Sort the array&lt;/P&gt;
&lt;P&gt;2. Loop over array and check if it matches the previous value.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't want to sort your data you can copy the array to another temporary array and drop it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input obs ID1 $ ID2 $ ID3 $;
cards;
1      X     Y     Z
2      A     B     C
3      D     D     E
4      M     N     N
;
run;

data want;
set have;

array _id(3) id1-id3;

call sortc(of _id(*));
count=1;
do i=2 to dim(_id);
if _id(i) ne _id(i-1) then count+1;
end;

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Another solution is to transpose your data to a long format and use PROC SQL with COUNT DISTINCT or a double PROC FREQ.&lt;/P&gt;
&lt;P&gt;An example of that is here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/count_distinct_by_group" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/count_distinct_by_group&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Aug 2017 18:40:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-unique-versions-of-a-string-across-variables/m-p/389191#M277500</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-18T18:40:50Z</dc:date>
    </item>
    <item>
      <title>Re: counting unique versions of a string across variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-unique-versions-of-a-string-across-variables/m-p/389203#M277501</link>
      <description>&lt;P&gt;thank you! this helps because I wanted to also create some flags based on the uniqueness conditions. these will be easy to add to this set up.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Aug 2017 19:07:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-unique-versions-of-a-string-across-variables/m-p/389203#M277501</guid>
      <dc:creator>TechRA</dc:creator>
      <dc:date>2017-08-18T19:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: counting unique versions of a string across variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-unique-versions-of-a-string-across-variables/m-p/389310#M277502</link>
      <description>&lt;PRE&gt;
It is very easy for IML code due to the function  COUNTUNIQUE().
Or Hash Table can do it though.



data have;
input obs (ID1 ID2 ID3) ($);
cards;
1      X     Y     Z
2      A     B     C
3      D     D     E
4      M     N     N
;
run;

data want;
 if _n_=1 then do;
  length k $ 200;
  declare hash h();
  h.definekey('k');
  h.definedone();
 end;
set have;
array x{*} $ id1-id3;
do i=1 to dim(x);
 if not missing(x{i}) then do;k=x{i};h.ref();end;
end;
count=h.num_items;
h.clear();
drop i;
run;

&lt;/PRE&gt;</description>
      <pubDate>Sat, 19 Aug 2017 12:11:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-unique-versions-of-a-string-across-variables/m-p/389310#M277502</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-08-19T12:11:50Z</dc:date>
    </item>
  </channel>
</rss>

