<?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: If and then statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-and-then-statement/m-p/809548#M319262</link>
    <description>&lt;P&gt;Look at the changes I've made. This will hopefully explain to you why things didn't work as desired.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1650836933355.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70775iE13947E77EB66CD5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_1-1650836933355.png" alt="Patrick_1-1650836933355.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The added ELSE statements are cleaner coding practice, what really hit you was the wrong condition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
  do m_num=.,149,150,151,499,500,501;
    output;
  end;
run;

data b;
  set  a;
  if m_num &amp;lt; 150 then count= 0;
  else if 150 &amp;lt;= m_num  &amp;lt;=500 then count= 1;
  else if m_num  &amp;gt; 500 then count= 2;
run;

proc print data=b;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1650836882812.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70774i0708B2CC96F0A075/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1650836882812.png" alt="Patrick_0-1650836882812.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 25 Apr 2022 07:50:49 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2022-04-25T07:50:49Z</dc:date>
    <item>
      <title>If and then statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-and-then-statement/m-p/809539#M319258</link>
      <description>&lt;P&gt;Hello guys,&lt;BR /&gt;&lt;BR /&gt;I wanted to build this logic but for some reasons it's been giving me a missing. The variable N_NUM is a continuous variable (some values have decimals eg 0.5. 0.6 etc.) that I'm trying to recategorize to 0-149, 150-500, and &amp;gt; 500. I ran this code but it's been giving me missing values (.) instead of '0', while the other cases worked. What could be the cause of this?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Data B; set  A;
If M_NUM &amp;lt; 150 then count= 0;
if 150 &amp;gt;= M_NUM  &amp;lt;=500 then count= 1;
if M_NUM  &amp;gt; 500 then count= 2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 Apr 2022 21:15:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-and-then-statement/m-p/809539#M319258</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2022-04-24T21:15:53Z</dc:date>
    </item>
    <item>
      <title>Re: If and then statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-and-then-statement/m-p/809540#M319259</link>
      <description>&lt;P&gt;Here's a useful debugging technique. You look at the data set B with your own eyes, and see if there is a pattern as to what values of M_NUM produce the missing values instead of 0 (also your code will never produce '0' as the value of COUNT).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are still stumped, SHOW US several of the M_NUM values that result in a value of COUNT=.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Apr 2022 21:41:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-and-then-statement/m-p/809540#M319259</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-24T21:41:56Z</dc:date>
    </item>
    <item>
      <title>Re: If and then statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-and-then-statement/m-p/809548#M319262</link>
      <description>&lt;P&gt;Look at the changes I've made. This will hopefully explain to you why things didn't work as desired.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1650836933355.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70775iE13947E77EB66CD5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_1-1650836933355.png" alt="Patrick_1-1650836933355.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The added ELSE statements are cleaner coding practice, what really hit you was the wrong condition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
  do m_num=.,149,150,151,499,500,501;
    output;
  end;
run;

data b;
  set  a;
  if m_num &amp;lt; 150 then count= 0;
  else if 150 &amp;lt;= m_num  &amp;lt;=500 then count= 1;
  else if m_num  &amp;gt; 500 then count= 2;
run;

proc print data=b;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1650836882812.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70774i0708B2CC96F0A075/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1650836882812.png" alt="Patrick_0-1650836882812.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Apr 2022 07:50:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-and-then-statement/m-p/809548#M319262</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-04-25T07:50:49Z</dc:date>
    </item>
    <item>
      <title>Re: If and then statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-and-then-statement/m-p/809550#M319263</link>
      <description>&lt;P&gt;because in SAS, . is less than 0. you could add a where statement for where ^missing(m_num). otherwise, you could put your statements in a do block: if ^missing(m_num) then do; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also highly recommend checking your work with proc freq or proc means like this:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=b n nmiss min max;
class count / missing;
var m_num;
run;

proc freq data=B;
tables m_num*count / list missing;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;both of these would allow you to see if the count variable was derived correctly.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Apr 2022 22:23:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-and-then-statement/m-p/809550#M319263</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-04-24T22:23:33Z</dc:date>
    </item>
    <item>
      <title>Re: If and then statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-and-then-statement/m-p/809562#M319269</link>
      <description>&lt;P&gt;If you are unsure how a complex condition will be evaluated on trick you can do is to format it as a WHERE statement and run it.&amp;nbsp; &amp;nbsp;SAS will then print the its interpretation of the condition as a NOTE in the log.&lt;/P&gt;
&lt;PRE&gt;10    Data B; set  A;
11    where 150 &amp;gt;= M_NUM  &amp;lt;=500 ;
12    run;

NOTE: There were 1 observations read from the data set WORK.A.
      WHERE M_NUM&amp;lt;=150;
NOTE: The data set WORK.B has 1 observations and 1 variables.
&lt;/PRE&gt;
&lt;P&gt;You can see that SAS has reduced the test for whether M_NUM is both less than or equal to 150 and less than or equal to 500 to the simpler single test that M_NUM is less than or equal to 150.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect that you meant 150 &amp;lt;= M_NUM &amp;lt;= 500 instead.&lt;/P&gt;
&lt;PRE&gt;14    Data B; set  A;
15    where 150 &amp;lt;= M_NUM  &amp;lt;=500 ;
16    run;

NOTE: There were 0 observations read from the data set WORK.A.
      WHERE (M_NUM&amp;gt;=150 and M_NUM&amp;lt;=500);
NOTE: The data set WORK.B has 0 observations and 1 variables.
&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Apr 2022 01:16:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-and-then-statement/m-p/809562#M319269</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-25T01:16:24Z</dc:date>
    </item>
  </channel>
</rss>

