<?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 of unique values output as new variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-of-unique-values-output-as-new-variable/m-p/796266#M255497</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;THANK YOU. Both of these solutions work perfectly and are quite clever. Greatly appreciate your help- love learning from this community.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 15 Feb 2022 13:33:59 GMT</pubDate>
    <dc:creator>luch25</dc:creator>
    <dc:date>2022-02-15T13:33:59Z</dc:date>
    <item>
      <title>Count of unique values output as new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-unique-values-output-as-new-variable/m-p/796204#M255478</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I have a dataset where I need to count the number of unique values of the variable "class" for each "studyid". I know that I can do this within proc sql, but I'm getting stuck because I need to output the counts of unique classes by studyid as a new column in the original dataset structure. So for the dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id class;
cards;
1 1
1 2
1 3
1 3
1 4
1 5
1 5
1 5
2 1
2 2
2 2
2 3
2 4
2 4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would want the resulting dataset to look like this:&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
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t;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Reasoning for the "count" variable:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;id=1 has a single value of class=1, so count=1&lt;/LI&gt;
&lt;LI&gt;id=1 has a single value of class=2, so count=1&lt;/LI&gt;
&lt;LI&gt;id=1 has two values of class=3, so count=2 for both of the rows with id=1 and class=3&lt;/LI&gt;
&lt;LI&gt;id=1 has three values of class=5, so count=3 for all the rows with id=1 and class=5&lt;/LI&gt;
&lt;LI&gt;etc&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Any guidance would be greatly appreciated!!&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 05:58:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-unique-values-output-as-new-variable/m-p/796204#M255478</guid>
      <dc:creator>luch25</dc:creator>
      <dc:date>2022-02-15T05:58:24Z</dc:date>
    </item>
    <item>
      <title>Re: Count of unique values output as new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-unique-values-output-as-new-variable/m-p/796209#M255479</link>
      <description>Try a double loop, first to count and second to output:&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;count = 0;&lt;BR /&gt;do until (last.class);&lt;BR /&gt;  set have;&lt;BR /&gt;  by ID class;&lt;BR /&gt;  count + 1;&lt;BR /&gt;end;&lt;BR /&gt;do until (last.class);&lt;BR /&gt;   set have;&lt;BR /&gt;   by id class;&lt;BR /&gt;   output;&lt;BR /&gt;end;&lt;BR /&gt;run;</description>
      <pubDate>Tue, 15 Feb 2022 06:59:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-unique-values-output-as-new-variable/m-p/796209#M255479</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-02-15T06:59:16Z</dc:date>
    </item>
    <item>
      <title>Re: Count of unique values output as new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-unique-values-output-as-new-variable/m-p/796217#M255481</link>
      <description>&lt;P&gt;An SQL alternative. Though, I like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;double DoW approach better &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&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 id class;
cards;
1 1
1 2
1 3
1 3
1 4
1 5
1 5
1 5
2 1
2 2
2 2
2 3
2 4
2 4
;

proc sql;
   create table want as
   select id
        , class
		, (select count(class) 
           from have a 
           where a.id = b.id and a.class = b.class) as count
   from have b
   ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;id  class  count 
1   1      1 
1   2      1 
1   3      2 
1   3      2 
1   4      1 
1   5      3 
1   5      3 
1   5      3 
2   1      1 
2   2      2 
2   2      2 
2   3      1 
2   4      2 
2   4      2 &lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Feb 2022 07:39:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-unique-values-output-as-new-variable/m-p/796217#M255481</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-02-15T07:39:04Z</dc:date>
    </item>
    <item>
      <title>Re: Count of unique values output as new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-unique-values-output-as-new-variable/m-p/796223#M255482</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/343488"&gt;@luch25&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I was reading your question, I also thought of the&amp;nbsp;&lt;A title="http://support.sas.com/resources/papers/proceedings09/038-2009.pdf" href="http://support.sas.com/resources/papers/proceedings09/038-2009.pdf" target="_blank" rel="noopener"&gt;double DOW-loop&lt;/A&gt;&amp;nbsp;as a solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I mainly wanted to commend you on the way you asked your question, from the data step with &lt;FONT face="courier new,courier"&gt;datalines&lt;/FONT&gt; for input, the output for the &lt;EM&gt;given&lt;/EM&gt; input, and, as importantly, your reasoning showing the rules behind how you want the output to be achieved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let this be a good lesson to others asking questions, as evidenced by the respondents who were able to reply with solutions and not a single follow up question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Well done.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 08:02:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-unique-values-output-as-new-variable/m-p/796223#M255482</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2022-02-15T08:02:52Z</dc:date>
    </item>
    <item>
      <title>Re: Count of unique values output as new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-unique-values-output-as-new-variable/m-p/796266#M255497</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;THANK YOU. Both of these solutions work perfectly and are quite clever. Greatly appreciate your help- love learning from this community.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 13:33:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-unique-values-output-as-new-variable/m-p/796266#M255497</guid>
      <dc:creator>luch25</dc:creator>
      <dc:date>2022-02-15T13:33:59Z</dc:date>
    </item>
  </channel>
</rss>

