<?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: Create new variable to count values within a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766370#M242869</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id visit_num diag :$9. age;
cards;
01 1 diabetes 42 
02 1 stroke 78 
02 2 stress . 
02 3 stroke . 
03 1 infection 61 
03 2 . . 
;
run;
proc sort data=have;
    by id diag;
run;
proc freq data=have(where=(diag not in ('stress','infection')));
    tables id*diag/noprint out=_counts_;
run;

data want;
    merge have _counts_(drop=percent rename=(count=frequency));
    by id diag;
run;
proc sort data=want;
    by id visit_num;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 07 Sep 2021 13:15:23 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-09-07T13:15:23Z</dc:date>
    <item>
      <title>Create new variable to count values within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766368#M242867</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to create a new variable in my dataset which counts the frequency of unique diagnoses per ID. I want the frequency variable to count any unique diagnosis - except "stress" or "infection". Below is an example of my dataset and desired output. (Note: in reality, my actual dataset has many different types of diagnoses, not just those shown below, but I still do not want to include "stress" or "infection" in my new frequency variable).&lt;/P&gt;
&lt;P&gt;Any help would be much appreciated. Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Desired output:&lt;/P&gt;
&lt;TABLE border="1" width="56.250000000000014%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;ID&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;visit_num&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;diag&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;age&lt;/TD&gt;
&lt;TD width="8.333333333333334%" height="30px"&gt;frequency&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;diabetes&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;42&lt;/TD&gt;
&lt;TD width="8.333333333333334%" height="30px"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;stroke&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;78&lt;/TD&gt;
&lt;TD width="8.333333333333334%" height="30px"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;stress&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="8.333333333333334%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%"&gt;2&lt;/TD&gt;
&lt;TD width="16.666666666666668%"&gt;3&lt;/TD&gt;
&lt;TD width="16.666666666666668%"&gt;stroke&lt;/TD&gt;
&lt;TD width="16.666666666666668%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="8.333333333333334%"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;infection&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;61&lt;/TD&gt;
&lt;TD width="8.333333333333334%" height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="16.666666666666668%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="8.333333333333334%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data:&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;data have;
input id visit_num diag$ age;
cards;
01 1 diabetes 42 
02 1 stroke 78 
02 2 stress . 
02 3 stroke . 
03 1 infection 61 
03 2 . . 

;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Sep 2021 19:27:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766368#M242867</guid>
      <dc:creator>monsterpie</dc:creator>
      <dc:date>2021-09-07T19:27:43Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable to count values within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766370#M242869</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id visit_num diag :$9. age;
cards;
01 1 diabetes 42 
02 1 stroke 78 
02 2 stress . 
02 3 stroke . 
03 1 infection 61 
03 2 . . 
;
run;
proc sort data=have;
    by id diag;
run;
proc freq data=have(where=(diag not in ('stress','infection')));
    tables id*diag/noprint out=_counts_;
run;

data want;
    merge have _counts_(drop=percent rename=(count=frequency));
    by id diag;
run;
proc sort data=want;
    by id visit_num;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Sep 2021 13:15:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766370#M242869</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-07T13:15:23Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable to count values within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766373#M242872</link>
      <description>&lt;P&gt;are you also not counting strokes?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Sep 2021 13:14:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766373#M242872</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-09-07T13:14:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable to count values within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766376#M242873</link>
      <description>&lt;P&gt;said not to count stress or infection though. should use a where statement to avoid counting those.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id visit_num diag :$9. age;
cards;
01 1 diabetes 42 
02 1 stroke 78 
02 2 stress . 
02 3 stroke . 
03 1 infection 61 
03 2 . . 
;
run;
proc sort data=have;
    by id diag;
run;
proc freq data=have;
	where diag not in ('stress','infection');
    tables id*diag/noprint out=_counts_;
run;

data want;
    merge have(in=a) _counts_(drop=percent rename=(count=frequency));
    by id diag;
    if a;
run;
proc sort data=want;
    by id visit_num;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Sep 2021 13:16:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766376#M242873</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-09-07T13:16:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable to count values within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766380#M242876</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/181158"&gt;@tarheel13&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;said not to count stress or infection though. should use a where statement to avoid counting those.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Already fixed&lt;/P&gt;</description>
      <pubDate>Tue, 07 Sep 2021 13:22:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766380#M242876</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-07T13:22:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable to count values within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766459#M242910</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt; for your help. I think that almost does what I need it to, but to clarify, I actually want the count of the unique diagnoses per ID, regardless of what the actual diagnosis is, but not counting stress and infection. (E.g., for ID2, the count would be 1, for ID3 the count would be 0).&lt;/P&gt;</description>
      <pubDate>Tue, 07 Sep 2021 18:57:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766459#M242910</guid>
      <dc:creator>monsterpie</dc:creator>
      <dc:date>2021-09-07T18:57:46Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable to count values within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766460#M242911</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/318138"&gt;@monsterpie&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I actually want the count of the unique diagnoses per ID, regardless of what the actual diagnosis is, but not counting stress and infection. (E.g., for ID2, the count would be 1, for ID3 the count would be 0).&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This contradicts your stated desired output, where ID2 shows a frequency of 2. So which is it?&lt;/P&gt;</description>
      <pubDate>Tue, 07 Sep 2021 19:01:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766460#M242911</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-07T19:01:05Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable to count values within a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766467#M242914</link>
      <description>&lt;P&gt;My apologies &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;. I edited the original post to fix this error, I am looking to count the unique diganoses per ID. So for ID2, the count should be 1.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Sep 2021 19:29:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-to-count-values-within-a-variable/m-p/766467#M242914</guid>
      <dc:creator>monsterpie</dc:creator>
      <dc:date>2021-09-07T19:29:26Z</dc:date>
    </item>
  </channel>
</rss>

