<?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: variable categorization in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/variable-categorization/m-p/874029#M345286</link>
    <description>&lt;P&gt;There seem to be some holes in your rules.&lt;/P&gt;
&lt;P&gt;First let's try it with only 2 input variables.&amp;nbsp; First let's make a dataset with all possible combinations of 0,1 and 2.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do drug1=0,1,2; do drug2=0,1,2; 
    output;
  end;end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's apply your rules (in the order you state them).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list=drug1 drug2 ;

data want;
  set have;
/* 1. if any of the drugs = 0 then drug = 0 */
  if countc(cats(of &amp;amp;list),'0') then drug=0;
/* 2. if only one drug=1 then drug =1 */
  else if 1=countc(cats(of &amp;amp;list),'1') then drug=1;
/* 3. if More than one drug= 2 then drug= 2 */
  else if 1&amp;lt;countc(cats(of &amp;amp;list),'2') then drug=2;
/* 4. ???? */
  else drug=.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1683246358606.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83640iD406BB9F65409803/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1683246358606.png" alt="Tom_0-1683246358606.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;So none of your rules covers the case when there are no zeros and more than 1 one and less than 2 twos.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 05 May 2023 00:27:15 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-05-05T00:27:15Z</dc:date>
    <item>
      <title>variable categorization</title>
      <link>https://communities.sas.com/t5/SAS-Programming/variable-categorization/m-p/874012#M345274</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;I am trying to generate the following drug categories. I made an attempt but not sure if I'm right. Any help will be very appreciated.&lt;BR /&gt;1. if any of the drugs = 0 then drug = 0&lt;BR /&gt;2. if only one drug=1 then drug =1&lt;BR /&gt;3. if More than one drug= 2 then drug= 2&lt;BR /&gt;Here is my code attempt&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
set have;
if acet_drug=0 &amp;amp; tri_drug=0 &amp;amp; BARBS_drug=0 &amp;amp; mari_drug then drug =0;
else if acet_drug=1 | tri_drug=1 | BARBS_drug=1 | mari_drug=1 then drug=1;
else if acet_drug=2 &amp;amp; tri_drug=2 &amp;amp; BARBS_drug=2 &amp;amp; mari_drug=2 then drug=2 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 May 2023 21:22:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/variable-categorization/m-p/874012#M345274</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2023-05-04T21:22:46Z</dc:date>
    </item>
    <item>
      <title>Re: variable categorization</title>
      <link>https://communities.sas.com/t5/SAS-Programming/variable-categorization/m-p/874013#M345275</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your categories are not mutually exclusive.&amp;nbsp; It's possible for a single record to be true for all three categories.&amp;nbsp; It looks like you want to categorize the record into the first true category, correct? i.e. in your code you use if/else if/else if, but that is not stated in the logic description.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't think your current code will give you what you want.&amp;nbsp; One good way to check is with PROC FREQ.&amp;nbsp; If you run:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=want;
  tables drug*acet_drug*tri_drug*BARBS_drug*mari_drug /missing list;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That will produce a nice table showing how each combination of values in your data was mapped to a drug category.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just reading your logic, it's confusing.&amp;nbsp; I wonder if you might want:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1. if all the drugs = 0 then drug = 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2. if only one drug=1 then drug =1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3. if More than one drug=1 then drug= 2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are the values you have for the drug variables.&amp;nbsp; Are they just coded 0 or 1?&amp;nbsp; Or are they coded 0, 1, 2?&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 21:35:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/variable-categorization/m-p/874013#M345275</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-05-04T21:35:29Z</dc:date>
    </item>
    <item>
      <title>Re: variable categorization</title>
      <link>https://communities.sas.com/t5/SAS-Programming/variable-categorization/m-p/874025#M345283</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp; Thank you for your great suggestion. You are right about the data not mutually exclusive. Here is the snapshot of the proc freq you suggested&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CathyVI_0-1683243905863.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83639i10B7CE14F2DF84C4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="CathyVI_0-1683243905863.png" alt="CathyVI_0-1683243905863.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I think doing this might be better as you've suggested. My data is just 0,1,2&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1. if all the drugs = 0 then drug = 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2. if only one drug=1 then drug =1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3. if More than one drug=1 then drug= 2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;What code will be a better code?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 23:47:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/variable-categorization/m-p/874025#M345283</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2023-05-04T23:47:30Z</dc:date>
    </item>
    <item>
      <title>Re: variable categorization</title>
      <link>https://communities.sas.com/t5/SAS-Programming/variable-categorization/m-p/874026#M345284</link>
      <description>&lt;P&gt;Hmm, I was hoping your data was just 0/1 for No/Yes.&amp;nbsp; What do 0, 1, and 2 mean?&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 23:50:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/variable-categorization/m-p/874026#M345284</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-05-04T23:50:16Z</dc:date>
    </item>
    <item>
      <title>Re: variable categorization</title>
      <link>https://communities.sas.com/t5/SAS-Programming/variable-categorization/m-p/874029#M345286</link>
      <description>&lt;P&gt;There seem to be some holes in your rules.&lt;/P&gt;
&lt;P&gt;First let's try it with only 2 input variables.&amp;nbsp; First let's make a dataset with all possible combinations of 0,1 and 2.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do drug1=0,1,2; do drug2=0,1,2; 
    output;
  end;end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's apply your rules (in the order you state them).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list=drug1 drug2 ;

data want;
  set have;
/* 1. if any of the drugs = 0 then drug = 0 */
  if countc(cats(of &amp;amp;list),'0') then drug=0;
/* 2. if only one drug=1 then drug =1 */
  else if 1=countc(cats(of &amp;amp;list),'1') then drug=1;
/* 3. if More than one drug= 2 then drug= 2 */
  else if 1&amp;lt;countc(cats(of &amp;amp;list),'2') then drug=2;
/* 4. ???? */
  else drug=.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1683246358606.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83640iD406BB9F65409803/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1683246358606.png" alt="Tom_0-1683246358606.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;So none of your rules covers the case when there are no zeros and more than 1 one and less than 2 twos.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 May 2023 00:27:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/variable-categorization/m-p/874029#M345286</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-05T00:27:15Z</dc:date>
    </item>
  </channel>
</rss>

