<?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: Creating a categorical variable conditioned on a series of if-then-else statements in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764541#M242176</link>
    <description>&lt;P&gt;What type of values are in that plethora of variables? Are they all 0/1 coded ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, you might consider evaluating some of the repeated things such as&lt;BR /&gt;sum (R1strong, EP1strong, PL1strong, DC3strong). If that is ge 2 then the values of the other variables don't matter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With 1/0 coded variables then MAX(of list)=1 tells you that ate least one of the variables is 1.&lt;/P&gt;</description>
    <pubDate>Fri, 27 Aug 2021 18:42:31 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-08-27T18:42:31Z</dc:date>
    <item>
      <title>Creating a categorical variable conditioned on a series of if-then-else statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764513#M242165</link>
      <description>&lt;P&gt;Hello! I am looking for help creating a 0/1 categorical variable, where 0 = negative for the specified condition and 1 = positive for the specified condition. The statement I am using (pasted below) pulls in a bunch of different variables that must meet certain criteria to be slated into the "1" category. I want to verify that 1) what I am doing is correct, and my statements aren't overwriting any previous statements, and 2) see if there is a better way to do this, perhaps using an array of some kind (?) Any advice is welcome, and I would be happy to provide further explanations if necessary. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#339966"&gt;*/ Number of persons identified as having experienced FM3strong = 1 &lt;EM&gt;plus&lt;/EM&gt;&amp;nbsp;VALUE = 1 &lt;EM&gt;o&lt;/EM&gt;r&amp;nbsp;LABOR = 1; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#339966"&gt;OR&amp;nbsp;&amp;nbsp;number of persons answering yes to 2 or more of the following: R1strong, EP1strong, PL1strong, DC3strong, FM1strong &lt;EM&gt;or&lt;/EM&gt; FM2strong, V3strong &lt;EM&gt;or&lt;/EM&gt; V4strong, &lt;EM&gt;plus&lt;/EM&gt; VALUE = 1 &lt;EM&gt;or&lt;/EM&gt; LABOR = 1;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#339966"&gt;OR number of persons answering yes to at least 1 of the following: R1strong, EP1strong, PL1strong, DC3strong, FM1strong, FM2strong, V3strong, V4strong PLUS answering yes to 3 or more of the following: EP5med, EP6med, EP7med, FM4med, V8med, &lt;EM&gt;plus&lt;/EM&gt; VALUE = 1 &lt;EM&gt;or&lt;/EM&gt; LABOR = 1.&amp;nbsp;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#339966"&gt;*/&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TIPlifenum =.;&lt;BR /&gt;if FM3strong = 1 and VALUE = 1 then TIPlifenum = 1;&lt;BR /&gt;else if FM3strong = 1 and LABOR = 1 then TIPlifenum = 1;&lt;BR /&gt;else if sum(R1strong, EP1strong, PL1strong, DC3strong, FM1strong, V3strong) ge 2 or&lt;BR /&gt;sum (R1strong, EP1strong, PL1strong, DC3strong, FM1strong, V4strong) ge 2 or&lt;BR /&gt;sum(R1strong, EP1strong, PL1strong, DC3strong, FM2strong, V3strong) ge 2 or&lt;BR /&gt;sum(R1strong, EP1strong, PL1strong, DC3strong, FM2strong, V4strong) ge 2&lt;BR /&gt;and VALUE = 1 then TIPlifenum = 1;&lt;BR /&gt;else if sum(R1strong, EP1strong, PL1strong, DC3strong, FM1strong, V3strong) ge 2 or&lt;BR /&gt;sum (R1strong, EP1strong, PL1strong, DC3strong, FM1strong, V4strong) ge 2 or&lt;BR /&gt;sum(R1strong, EP1strong, PL1strong, DC3strong, FM2strong, V3strong) ge 2 or&lt;BR /&gt;sum(R1strong, EP1strong, PL1strong, DC3strong, FM2strong, V4strong) ge 2&lt;BR /&gt;and LABOR = 1 then TIPlifenum = 1;&lt;BR /&gt;else if sum (R1strong, EP1strong, PL1strong, FM1strong, FM2strong, V3strong, V4strong) ge 1 and sum(EP5med, EP6med, EP7med, FM4med, V8med) ge 3&lt;BR /&gt;and VALUE = 1 then TIPlifenum = 1;&lt;BR /&gt;else if sum (R1strong, EP1strong, PL1strong, FM1strong, FM2strong, V3strong, V4strong) ge 1 and sum(EP5med, EP6med, EP7med, FM4med, V8med) ge 3&lt;BR /&gt;and LABOR = 1 then TIPlifenum = 1;&lt;BR /&gt;else TIPlifenum = 0;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Aug 2021 17:18:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764513#M242165</guid>
      <dc:creator>ab81</dc:creator>
      <dc:date>2021-08-27T17:18:55Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a categorical variable conditioned on a series of if-then-else statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764533#M242173</link>
      <description>&lt;P&gt;I see a way to simplify this. (edit: I'm assuming that the "or'd sums" have parentheses.)&amp;nbsp; This reminds me of my college Discrete Mathematics class, my Algebra classes, and both Boolean algebra and the&amp;nbsp;&lt;A href="https://en.wikipedia.org/wiki/Distributive_property" target="_self"&gt;Distributive property&lt;/A&gt;.&amp;nbsp; I'll leave it to you or someone else to prove:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;TIPlifenum = (FM3strong OR sum_strong  OR sum_strong2 ) and (VALUE OR LABOR ) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;where:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;sum_strong0= sum(R1strong, EP1strong, PL1strong);
sum_strong=
   sum (
     sum_strong0 + DC3strong,
     max ( sum(FM1strong, V3strong), 
           sum(FM1strong, V4strong),
           sum(FM2strong, V3strong),
           sum(FM2strong, V4strong)  )) ge 2;
sum_strong2=
   sum(sum_strong0, FM1strong, FM2strong, V3strong, V4strong) ge 1 and
   sum(EP5med, EP6med, EP7med, FM4med, V8med) ge 3;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Aug 2021 19:03:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764533#M242173</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2021-08-27T19:03:06Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a categorical variable conditioned on a series of if-then-else statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764536#M242175</link>
      <description>&lt;P&gt;Question: you are not running this algorithm on super large datasets are you?&amp;nbsp; Or IOW, you are not optimizing this code for purposes of increased speed?&lt;/P&gt;</description>
      <pubDate>Fri, 27 Aug 2021 18:18:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764536#M242175</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2021-08-27T18:18:40Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a categorical variable conditioned on a series of if-then-else statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764541#M242176</link>
      <description>&lt;P&gt;What type of values are in that plethora of variables? Are they all 0/1 coded ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, you might consider evaluating some of the repeated things such as&lt;BR /&gt;sum (R1strong, EP1strong, PL1strong, DC3strong). If that is ge 2 then the values of the other variables don't matter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With 1/0 coded variables then MAX(of list)=1 tells you that ate least one of the variables is 1.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Aug 2021 18:42:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764541#M242176</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-08-27T18:42:31Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a categorical variable conditioned on a series of if-then-else statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764546#M242179</link>
      <description>&lt;P&gt;One more thing, Order of Operations, you'll have to take care are using these correctly.&amp;nbsp; AND is higher on the order of operations so the AND here happens first&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="lia-indent-padding-left-30px"&gt;else if sum(R1strong, EP1strong, PL1strong, DC3strong, FM1strong, V3strong) ge 2 or&lt;BR /&gt;sum (R1strong, EP1strong, PL1strong, DC3strong, FM1strong, V4strong) ge 2 or&lt;BR /&gt;sum(R1strong, EP1strong, PL1strong, DC3strong, FM2strong, V3strong) ge 2 or&lt;BR /&gt;&lt;STRONG&gt;sum(R1strong, EP1strong, PL1strong, DC3strong, FM2strong, V4strong) ge 2&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="5"&gt;AND&amp;nbsp;&lt;/FONT&gt;VALUE = 1&lt;/STRONG&gt; then TIPlifenum = 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you might have intended for the ORs to happen before the AND.&amp;nbsp; If so, you'll need parentheses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="lia-indent-padding-left-30px"&gt;else if &lt;STRONG&gt;sum(R1strong, EP1strong, PL1strong, DC3strong, FM1strong, V3strong) ge 2 &lt;FONT size="5"&gt;OR&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;sum (R1strong, EP1strong, PL1strong, DC3strong, FM1strong, V4strong) ge 2 &lt;FONT size="5"&gt;OR&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;sum(R1strong, EP1strong, PL1strong, DC3strong, FM2strong, V3strong) ge 2 &lt;FONT size="5"&gt;OR&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;sum(R1strong, EP1strong, PL1strong, DC3strong, FM2strong, V4strong) ge 2&lt;/STRONG&gt;&lt;BR /&gt;&lt;FONT size="3"&gt;and VALUE = 1&lt;/FONT&gt; then TIPlifenum = 1;&lt;/P&gt;
&lt;P class="lia-indent-padding-left-30px"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p1n8bsqqd03xppn17pgvjpjlbhhs.htm#p1ces81yre845mn1rg0jkr2uwxbg" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p1n8bsqqd03xppn17pgvjpjlbhhs.htm#p1ces81yre845mn1rg0jkr2uwxbg&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Aug 2021 18:53:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764546#M242179</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2021-08-27T18:53:52Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a categorical variable conditioned on a series of if-then-else statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764564#M242189</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/382486"&gt;@ab81&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In addition to the missing parentheses around the Boolean expressions combined with OR (as pointed out by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15329"&gt;@PhilC&lt;/a&gt;) you missed variable&amp;nbsp;&lt;SPAN&gt;DC3strong in two of the last four sums. It seems that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15329"&gt;@PhilC&lt;/a&gt;&amp;nbsp;has copied this mistake into his code.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/382486"&gt;@ab81&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;(...) see if there is a better way to do this, perhaps using an array of some kind (?)&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, you can use arrays to abbreviate repetitive lists of variables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array _a[*] R1strong EP1strong PL1strong DC3strong;
array _b[*] FM1strong FM2strong;
array _c[*] V3strong V4strong;
TIPlifenum=
  (FM3strong = 1 
     | sum(0, of _a[*], max(0, of _b[*]), max(0, of _c[*]))&amp;gt;=2
     | sum(0, of _a[*], of _b[*], of _c[*])&amp;gt;=1 &amp;amp; sum(0, EP5med, EP6med, EP7med, FM4med, V8med)&amp;gt;=3)
  &amp;amp; (VALUE = 1 | LABOR = 1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes 0-1 coded variables, but also allows for missing values. If you are sure that no missing values occur, you can delete the zeros in the SUM and MAX function arguments. Their only purpose is to avoid notes "&lt;FONT face="courier new,courier"&gt;Missing value were generated ...&lt;/FONT&gt;" in the log. They do not change the results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general,&amp;nbsp;missing values can be harmful. For example, the expression&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;sum_strong0 + DC3strong&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(contained in &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15329"&gt;@PhilC&lt;/a&gt;'s code) would be missing if &lt;FONT face="courier new,courier"&gt;sum_strong0&lt;/FONT&gt;&amp;nbsp;was missing and thus lead to ignoring a valid value &lt;FONT face="courier new,courier"&gt;DC3strong=1&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used this input dataset HAVE and then PROC COMPARE on the "WANT" datasets to compare the suggested solutions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
do FM3strong = .,0,1;
do VALUE     = .,0,1;
do LABOR     = .,0,1;
do R1strong  = .,0,1;
do EP1strong = .,0,1;
do PL1strong = .,0,1;
do DC3strong = .,0,1;
do FM1strong = .,0,1;
do FM2strong = .,0,1;
do V3strong  = .,0,1;
do V4strong  = .,0,1;
do EP5med    = .,0,1;
do EP6med    = .,0,1;
do EP7med    = .,0,1;
do FM4med    = .,0,1;
do V8med     = .,0,1;
  output;
end; end; end; end; end; end; end; end; end; end; end; end; end; end; end; end;
run; /* 3**16=43046721 obs. */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Aug 2021 20:40:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764564#M242189</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-08-27T20:40:23Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a categorical variable conditioned on a series of if-then-else statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764754#M242245</link>
      <description>No, the dataset is currently &amp;lt; 450 observations - just figured there was an easier/less verbose way to write this code.</description>
      <pubDate>Sun, 29 Aug 2021 19:41:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764754#M242245</guid>
      <dc:creator>ab81</dc:creator>
      <dc:date>2021-08-29T19:41:12Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a categorical variable conditioned on a series of if-then-else statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764755#M242246</link>
      <description>Ah, that is good to know - and what I was concerned about - wondering if the AND canceled out all the OR statements from the prior lines of code.</description>
      <pubDate>Sun, 29 Aug 2021 19:42:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764755#M242246</guid>
      <dc:creator>ab81</dc:creator>
      <dc:date>2021-08-29T19:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a categorical variable conditioned on a series of if-then-else statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764756#M242247</link>
      <description>Yes, each of the other type of variables in that gignatic list are coded 0/1 too.</description>
      <pubDate>Sun, 29 Aug 2021 19:42:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-categorical-variable-conditioned-on-a-series-of-if/m-p/764756#M242247</guid>
      <dc:creator>ab81</dc:creator>
      <dc:date>2021-08-29T19:42:31Z</dc:date>
    </item>
  </channel>
</rss>

