<?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 IF then else statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/IF-then-else-statement/m-p/725023#M225137</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;Am sorry I do not have a sample data for this question. I hope someone can still help me. I created the following code to identify the variable agegroup within the&amp;nbsp;if-then-else&amp;nbsp;statement&amp;nbsp;but my output isn't what I wanted. My output only have &amp;nbsp;'&amp;lt; 15 yrs'. I wanted&amp;nbsp; newborn, months and days to be in one agegroup category for "0-14 yrs" while other patient_age should identify patient age in years.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data pt_age;&lt;BR /&gt;set patient_x;&lt;BR /&gt;if PATIENT_AGE &amp;lt;= 120 and pt_record in ("months" "days") then agegroup = '&amp;lt; 15 yrs';&lt;BR /&gt;if pt_record = "newborn" then agegroup = '&amp;lt; 15 yrs';&lt;BR /&gt;if PATIENT_AGE &amp;gt;=1 and PATIENT_AGE &amp;lt;=15 and pt_age_recode in ("years") then agegroup = '&amp;lt; 15 yrs';&lt;/P&gt;&lt;P&gt;if PATIENT_AGE &amp;gt;=16 and PATIENT_AGE &amp;lt;=34 and pt_age_recode in ("years") then agegroup = '16-34 yrs';&lt;BR /&gt;if PATIENT_AGE &amp;gt;=35 and PATIENT_AGE &amp;lt;=54 and pt_age_recode in ("years") then agegroup = '35-54 yrs';&lt;BR /&gt;if PATIENT_AGE &amp;gt;=55 and pt_age_recode in ("years") then agegroup = '55+ yrs';&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Wed, 10 Mar 2021 01:50:51 GMT</pubDate>
    <dc:creator>CathyVI</dc:creator>
    <dc:date>2021-03-10T01:50:51Z</dc:date>
    <item>
      <title>IF then else statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-then-else-statement/m-p/725023#M225137</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;Am sorry I do not have a sample data for this question. I hope someone can still help me. I created the following code to identify the variable agegroup within the&amp;nbsp;if-then-else&amp;nbsp;statement&amp;nbsp;but my output isn't what I wanted. My output only have &amp;nbsp;'&amp;lt; 15 yrs'. I wanted&amp;nbsp; newborn, months and days to be in one agegroup category for "0-14 yrs" while other patient_age should identify patient age in years.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data pt_age;&lt;BR /&gt;set patient_x;&lt;BR /&gt;if PATIENT_AGE &amp;lt;= 120 and pt_record in ("months" "days") then agegroup = '&amp;lt; 15 yrs';&lt;BR /&gt;if pt_record = "newborn" then agegroup = '&amp;lt; 15 yrs';&lt;BR /&gt;if PATIENT_AGE &amp;gt;=1 and PATIENT_AGE &amp;lt;=15 and pt_age_recode in ("years") then agegroup = '&amp;lt; 15 yrs';&lt;/P&gt;&lt;P&gt;if PATIENT_AGE &amp;gt;=16 and PATIENT_AGE &amp;lt;=34 and pt_age_recode in ("years") then agegroup = '16-34 yrs';&lt;BR /&gt;if PATIENT_AGE &amp;gt;=35 and PATIENT_AGE &amp;lt;=54 and pt_age_recode in ("years") then agegroup = '35-54 yrs';&lt;BR /&gt;if PATIENT_AGE &amp;gt;=55 and pt_age_recode in ("years") then agegroup = '55+ yrs';&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Mar 2021 01:50:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-then-else-statement/m-p/725023#M225137</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2021-03-10T01:50:51Z</dc:date>
    </item>
    <item>
      <title>Re: IF then else statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-then-else-statement/m-p/725029#M225141</link>
      <description>&lt;P&gt;I don't think it's a problem with the SAS code, it's just that there are not enough conditional branches.&lt;BR /&gt;What I'm wondering is if there are records for age 0, 121-179 months, and 121-5474 days, the condition seems to be missing. If the data doesn't exist, it might not be a problem.&lt;/P&gt;
&lt;P&gt;If you use if-then-do-end, or select-when-other, I think it will be a little more clear!&lt;BR /&gt;Please refer to the following&lt;BR /&gt;(This is untested, so please don't be offended)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* use select */
  select (pt_record);
    when("newborn") do;
      agegroup = '&amp;lt; 15 yrs';
    end;
    when("days") do;
      if PATIENT_AGE &amp;lt;= 120 agegroup = '&amp;lt; 15 yrs';
    end;
    when("months") do;
      if PATIENT_AGE &amp;lt;= 120 agegroup = '&amp;lt; 15 yrs';
    end;
    when("years") do;
      select;
        when( 1 &amp;lt;= PATIENT_AGE &amp;lt;= 15) agegroup = '&amp;lt; 15 yrs';
        when(16 &amp;lt;= PATIENT_AGE &amp;lt;= 34) agegroup = '16-34 yrs';
        when(35 &amp;lt;= PATIENT_AGE &amp;lt;= 54) agegroup = '35-54 yrs';
        when(55 &amp;lt;= PATIENT_AGE      ) agegroup = '55+ yrs';
        otherwise;/* otherwise is same like else in if-else statements. But it should be written in the select statement, as it will cause errors for other conditions. */
      end;
    end;
    otherwise;
  end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* use if then else */
  if pt_record = "newborn" then agegroup = '&amp;lt; 15 yrs';
  else if pt_age_recode in ("years") then begin
    if       1 &amp;lt;= PATIENT_AGE &amp;lt;= 15 then agegroup = '&amp;lt; 15 yrs';
    else if 16 &amp;lt;= PATIENT_AGE &amp;lt;= 34 then agegroup = '16-34 yrs';
    else if 35 &amp;lt;= PATIENT_AGE &amp;lt;= 54 then agegroup = '35-54 yrs';
    else if 55 &amp;lt;= PATIENT_AGE       then agegroup = '55+ yrs';
  end else
  if pt_record in ("months" "days") then begin
    if PATIENT_AGE &amp;lt;= 120 agegroup = '&amp;lt; 15 yrs';
  end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Mar 2021 02:40:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-then-else-statement/m-p/725029#M225141</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-03-10T02:40:24Z</dc:date>
    </item>
    <item>
      <title>Re: IF then else statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-then-else-statement/m-p/725061#M225163</link>
      <description>&lt;P&gt;Can you please post an excerpt of the data you have, so that we actually see what you start with? Most likely using a format is the best way to solve the problem.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Mar 2021 06:20:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-then-else-statement/m-p/725061#M225163</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-03-10T06:20:27Z</dc:date>
    </item>
    <item>
      <title>Re: IF then else statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-then-else-statement/m-p/725241#M225224</link>
      <description>&lt;P&gt;So I'm still working on this question. I created a sample data for better understanding.&lt;/P&gt;&lt;P&gt;So this purpose is to identify age values in different 'agegroups' categories. For example,&lt;/P&gt;&lt;P&gt;0 is present in both years, months and days and I will like to have that in one group '&amp;lt; 15 yrs'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; test;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; patient_id patient_age patient_age_unit pt_age_code $;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1231 24 2 months&lt;/P&gt;&lt;P&gt;1232 30 1 years&lt;/P&gt;&lt;P&gt;1233 3 1 years&lt;/P&gt;&lt;P&gt;1234 33 1 years&lt;/P&gt;&lt;P&gt;1235 0 0 newborn&lt;/P&gt;&lt;P&gt;1236 4 1 years&lt;/P&gt;&lt;P&gt;1237 0 1 years&lt;/P&gt;&lt;P&gt;1238 11 1 years&lt;/P&gt;&lt;P&gt;1239 90 1 years&lt;/P&gt;&lt;P&gt;1230 1 0 newborn&lt;/P&gt;&lt;P&gt;1212 0 2 months&lt;/P&gt;&lt;P&gt;1213 6 1 years&lt;/P&gt;&lt;P&gt;1214 11 2 months&lt;/P&gt;&lt;P&gt;1215 12 1 years&lt;/P&gt;&lt;P&gt;1216 0 3 days&lt;/P&gt;&lt;P&gt;1217 1 1 years&lt;/P&gt;&lt;P&gt;1218 34 1 years&lt;/P&gt;&lt;P&gt;1219 30 3 days&lt;/P&gt;&lt;P&gt;1220 1 2 months&lt;/P&gt;&lt;P&gt;1221 94 1 years&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I created this new codes but am not sure what else am doing wrong. Please help&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data pt_age_grp (compress=yes keep= &lt;FONT face="Courier New" size="2"&gt;patient_id patient_age patient_age_unit pt_age_code &lt;/FONT&gt;agegroup );&lt;BR /&gt;set test;&lt;BR /&gt;&amp;nbsp; if pt_age_recode = "newborn" then&lt;BR /&gt;do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; agegroup = '&amp;lt;15yrs';&lt;BR /&gt;end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if &lt;FONT face="Courier New" size="2"&gt;patient_age&lt;/FONT&gt; &amp;lt;=30 and pt_age_recode = "days" then&lt;BR /&gt;do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; agegroup = '&amp;lt;15yrs';&lt;BR /&gt;end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if &lt;FONT face="Courier New" size="2"&gt;patient_age&lt;/FONT&gt; &amp;lt;=11 and pt_age_recode = "months" then&lt;BR /&gt;do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; agegroup = '&amp;lt;15yrs';&lt;BR /&gt;end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if 1 &amp;lt;= &lt;FONT face="Courier New" size="2"&gt;patient_age&lt;/FONT&gt; &amp;lt;=15 and pt_age_recode = "years" then&lt;BR /&gt;do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; agegroup = '&amp;lt;15yrs';&lt;BR /&gt;end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if 16 &amp;lt;= &lt;FONT face="Courier New" size="2"&gt;patient_age&lt;/FONT&gt; &amp;lt;= 34 and pt_age_recode = "years" then&lt;BR /&gt;do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; agegroup = '16-34yrs';&lt;BR /&gt;end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if 35 &amp;lt;= &lt;FONT face="Courier New" size="2"&gt;patient_age&lt;/FONT&gt; &amp;lt;= 54 and pt_age_recode = "years" then&lt;BR /&gt;do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; agegroup = '35-54yrs';&lt;BR /&gt;end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if &lt;FONT face="Courier New" size="2"&gt;patient_age&lt;/FONT&gt; &amp;gt;= 55 and pt_age_recode = "years" then&lt;BR /&gt;do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; agegroup = '55+yrs';&lt;BR /&gt;end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; agegroup = 'Others';&lt;/P&gt;&lt;P&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Mar 2021 18:58:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-then-else-statement/m-p/725241#M225224</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2021-03-10T18:58:04Z</dc:date>
    </item>
    <item>
      <title>Re: IF then else statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-then-else-statement/m-p/725318#M225266</link>
      <description>&lt;P&gt;If is not being evaluated correctly because pt_age_code and pt_age_recode are mixed up.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Mar 2021 01:27:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-then-else-statement/m-p/725318#M225266</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-03-11T01:27:19Z</dc:date>
    </item>
  </channel>
</rss>

