<?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 derived variable with multiple AND and OR conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777025#M247155</link>
    <description>&lt;P&gt;Thank you very much&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, I had overlapping conditions when I missed adding (var1var2_ratio &amp;gt; 0) in the var2 dominant category. You solved my problem and I learned I should not use multiple IF's to create derived variables.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Thu, 28 Oct 2021 16:18:13 GMT</pubDate>
    <dc:creator>Emma_at_SAS</dc:creator>
    <dc:date>2021-10-28T16:18:13Z</dc:date>
    <item>
      <title>Creating a derived variable with multiple AND and OR conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/776999#M247139</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	Category=.;
	if (var1var2_ratio &amp;gt;= 5) OR (var1 &amp;gt; 0 AND (var2 = . OR var2 = 0)) then Category = 1; *var1 was dominant;
	if (var1var2_ratio &amp;gt; 0.2 AND var1var2_ratio &amp;lt;= 2) 	then Category = 2;*var1 and var2 were balanced;
	if (var1var2_ratio &amp;lt;= 0.2) OR (var2 &amp;gt; 0 AND (var2 = . OR var2 = 0)) then Category = 3;*var2 was dominant;
	if (var1var2_ratio &amp;gt; 2 AND var1var2_ratio &amp;lt; 5)  then Category = 4;*var1 is higher but not dominant;
	run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have two variables var1 and var2 and I created a var1var2_ratio (var1/var2). Now I want to create a category variable using var1, var2, and var1var2_ratio.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The way I am using OR and AND does not create the outcome I want. I appreciate it if you have any suggestions to fix my code.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Oct 2021 15:18:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/776999#M247139</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-10-28T15:18:43Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a derived variable with multiple AND and OR conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777000#M247140</link>
      <description>&lt;P&gt;What is the outcome you want? Explain the rules in words.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Please show us a small portion of the data and the desired outcomes for a number of cases.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Oct 2021 15:27:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777000#M247140</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-10-28T15:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a derived variable with multiple AND and OR conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777005#M247143</link>
      <description>&lt;P&gt;First thing to change is to not have separate IF statements.&amp;nbsp; That can cause trouble when the conditions overlap.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Either link them using ELSE clauses or switch to using SELECT statement instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if (...) then category=1;
else if (...) then category=2;
else if (...) then category=3;
else if (...) then category=4;
else put 'Category not defined.';
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Oct 2021 15:29:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777005#M247143</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-10-28T15:29:18Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a derived variable with multiple AND and OR conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777007#M247144</link>
      <description>&lt;P&gt;Start by changing to ELSE IF not just IF.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then explain your rules and show some examples of where you are not getting expected values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	Category=.;
	if (var1var2_ratio &amp;gt;= 5) OR (var1 &amp;gt; 0 AND (var2 = . OR var2 = 0)) then Category = 1; *var1 was dominant;
	else if (var1var2_ratio &amp;gt; 0.2 AND var1var2_ratio &amp;lt;= 2) 	then Category = 2;*var1 and var2 were balanced;
	else if (var1var2_ratio &amp;lt;= 0.2) OR (var2 &amp;gt; 0 AND (var2 = . OR var2 = 0)) then Category = 3;*var2 was dominant;
	else if (var1var2_ratio &amp;gt; 2 AND var1var2_ratio &amp;lt; 5)  then Category = 4;*var1 is higher but not dominant;
	run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84351"&gt;@Emma_at_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	Category=.;
	if (var1var2_ratio &amp;gt;= 5) OR (var1 &amp;gt; 0 AND (var2 = . OR var2 = 0)) then Category = 1; *var1 was dominant;
	if (var1var2_ratio &amp;gt; 0.2 AND var1var2_ratio &amp;lt;= 2) 	then Category = 2;*var1 and var2 were balanced;
	if (var1var2_ratio &amp;lt;= 0.2) OR (var2 &amp;gt; 0 AND (var2 = . OR var2 = 0)) then Category = 3;*var2 was dominant;
	if (var1var2_ratio &amp;gt; 2 AND var1var2_ratio &amp;lt; 5)  then Category = 4;*var1 is higher but not dominant;
	run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have two variables var1 and var2 and I created a var1var2_ratio (var1/var2). Now I want to create a category variable using var1, var2, and var1var2_ratio.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The way I am using OR and AND does not create the outcome I want. I appreciate it if you have any suggestions to fix my code.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Oct 2021 15:31:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777007#M247144</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-28T15:31:43Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a derived variable with multiple AND and OR conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777025#M247155</link>
      <description>&lt;P&gt;Thank you very much&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, I had overlapping conditions when I missed adding (var1var2_ratio &amp;gt; 0) in the var2 dominant category. You solved my problem and I learned I should not use multiple IF's to create derived variables.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 28 Oct 2021 16:18:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777025#M247155</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-10-28T16:18:13Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a derived variable with multiple AND and OR conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777027#M247156</link>
      <description>I did not know I can use SELECT to derive such variables. Do you recommend any document I can use to learn more about how to derive variables? Thanks!</description>
      <pubDate>Thu, 28 Oct 2021 16:20:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777027#M247156</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-10-28T16:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a derived variable with multiple AND and OR conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777030#M247157</link>
      <description>&lt;P&gt;Read the documentation and its examples.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p09213s9jc2t99n1vx0omk2rh9ps.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p09213s9jc2t99n1vx0omk2rh9ps.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Oct 2021 16:21:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777030#M247157</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-10-28T16:21:43Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a derived variable with multiple AND and OR conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777052#M247173</link>
      <description>Thank you Tom!</description>
      <pubDate>Thu, 28 Oct 2021 17:07:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777052#M247173</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-10-28T17:07:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a derived variable with multiple AND and OR conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777125#M247195</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84351"&gt;@Emma_at_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I did not know I can use SELECT to derive such variables. Do you recommend any document I can use to learn more about how to derive variables? Thanks!&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Select works best when you have a single value or expression to evaluate such as&lt;/P&gt;
&lt;PRE&gt;Select (state);
   when ('AK') rate= 0.3;
   when ('AL') rate= 0.01;
   when ('AR') rate= 0.08;
   when ('AZ')  rate= 1.1;
...
   otherwise  rate= 0.5;
end;
    &lt;/PRE&gt;
&lt;P&gt;which is a bit easier then 50 (or more) IF/ Then/Else. Where there is Rate= there could be an entire block of code defined by a do;&amp;nbsp; statement1; statement2; end; or similar. So it might work for the outer comparisons.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The basic SAS documentation on the data step select statement should be sufficient. NOTE: this not the Proc SQL select which is an entirely different beast.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Oct 2021 21:29:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-derived-variable-with-multiple-AND-and-OR-conditions/m-p/777125#M247195</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-10-28T21:29:23Z</dc:date>
    </item>
  </channel>
</rss>

