<?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: anything wrong with my if statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570806#M160998</link>
    <description>&lt;P&gt;else is the "other" branch for the if/then statement&amp;nbsp;&lt;EM&gt;immediately preceding&lt;/EM&gt; it. So you get 7 for all values not in ('FAZ','FDA','FIA').&lt;/P&gt;
&lt;P&gt;In your case, the select() statement is the tool of choice:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select (risktype);
  when ('FHO','FHW','FHZ') class = 1;
  when ('FHH','FHI','FHY') class = 2;
  when ('FDP','FFZ','FHX','FMD','FMP') class = 3;
  when ('FCL','FCZ','FLP') class = 5;
  when ('FAZ','FDA','FIA') class = 6;
  otherwise class=7;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can see how this cleans up the code quite nicely.&lt;/P&gt;
&lt;P&gt;If you have to do this transformation more than once, I recommend to create an informat.&lt;/P&gt;</description>
    <pubDate>Wed, 03 Jul 2019 04:14:28 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-07-03T04:14:28Z</dc:date>
    <item>
      <title>anything wrong with my if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570803#M160997</link>
      <description>&lt;P&gt;Hi, i am currently facing a problem regarding the if statement, i don't no why the result keep showing the class column is all 7 instead of mix with other class number.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However if i remove the last part of the code which is the &lt;STRONG&gt;else,&lt;/STRONG&gt; the class column result will be mix class number depend on the risktype.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's my code below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/******Houseowners******/&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FHO'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;or&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FHW'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;or&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FHZ'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;CLASS=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;/******Householders*****/&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktyp=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FHH'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;or&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FHI'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;or&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FHY'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;CLASS=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*****Marital Damage*****/&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FDP'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;or&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FFZ'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;or&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FHX'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;or&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FMD'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;or&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FMP'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;CLASS=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;3&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*****Consequential Loss*****/&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FCL'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;or&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FCZ'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;or&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FLP'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;CLASS=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;5&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*****Industrial All Risks*****/&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FAZ'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;or&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FDA'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;or&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; risktype=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'FIA'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;CLASS=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;6&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;/*****Others*****/&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;else&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; class=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;7&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 03:57:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570803#M160997</guid>
      <dc:creator>Kayla_Tan222</dc:creator>
      <dc:date>2019-07-03T03:57:58Z</dc:date>
    </item>
    <item>
      <title>Re: anything wrong with my if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570806#M160998</link>
      <description>&lt;P&gt;else is the "other" branch for the if/then statement&amp;nbsp;&lt;EM&gt;immediately preceding&lt;/EM&gt; it. So you get 7 for all values not in ('FAZ','FDA','FIA').&lt;/P&gt;
&lt;P&gt;In your case, the select() statement is the tool of choice:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select (risktype);
  when ('FHO','FHW','FHZ') class = 1;
  when ('FHH','FHI','FHY') class = 2;
  when ('FDP','FFZ','FHX','FMD','FMP') class = 3;
  when ('FCL','FCZ','FLP') class = 5;
  when ('FAZ','FDA','FIA') class = 6;
  otherwise class=7;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can see how this cleans up the code quite nicely.&lt;/P&gt;
&lt;P&gt;If you have to do this transformation more than once, I recommend to create an informat.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 04:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570806#M160998</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-03T04:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: anything wrong with my if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570807#M160999</link>
      <description>&lt;PRE&gt;/******Houseowners******/

if risktype='FHO' or risktype='FHW' or risktype='FHZ' then

CLASS=1;

 

　

/******Householders*****/

if risktyp='FHH' or risktype='FHI' or risktype='FHY' then

CLASS=2;

 

/*****Marital Damage*****/

if risktype='FDP' or risktype='FFZ' or risktype='FHX' or risktype='FMD' or risktype='FMP' then

CLASS=3;

 

/*****Consequential Loss*****/

if risktype='FCL' or risktype='FCZ' or risktype='FLP' then

CLASS=5;

 

/*****Industrial All Risks*****/

if risktype='FAZ' or risktype='FDA' or risktype='FIA' then

CLASS=6;

 

　

/*****Others*****/

else class=7;&lt;/PRE&gt;
&lt;P&gt;You have used 5 IF conditions before the ELSE statement. I think all the conditions are mutually exclusive. So use ELSE IF for the 2nd to 5th IFs.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 04:18:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570807#M160999</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-03T04:18:47Z</dc:date>
    </item>
    <item>
      <title>Re: anything wrong with my if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570808#M161000</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/274500"&gt;@Kayla_Tan222&lt;/a&gt;&amp;nbsp;I think you should leave the first IF statement as it is and the subsequent IF statement should be ELSE IF rather. However without seeing the data, I might be wrong.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 04:19:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570808#M161000</guid>
      <dc:creator>koyelghosh</dc:creator>
      <dc:date>2019-07-03T04:19:11Z</dc:date>
    </item>
    <item>
      <title>Re: anything wrong with my if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570809#M161001</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;is 100% right as usual.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These 2 statements void the prior statements:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/*****Industrial All Risks*****/&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;risktype=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'FAZ'&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;or&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;risktype=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'FDA'&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;or&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;risktype=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'FIA'&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;then&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;CLASS=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#008080"&gt;6&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;/*****Others*****/&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;else&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;CLASS=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#008080"&gt;7&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 04:22:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570809#M161001</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-07-03T04:22:46Z</dc:date>
    </item>
    <item>
      <title>Re: anything wrong with my if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570816#M161003</link>
      <description>There is an alternative to if and select: you could use a format. That way you would move the logic setting class to the format definition, some people think, that this is just code obfuscation, but you should consider it.</description>
      <pubDate>Wed, 03 Jul 2019 05:21:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570816#M161003</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-07-03T05:21:12Z</dc:date>
    </item>
    <item>
      <title>Re: anything wrong with my if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570826#M161008</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;There is an alternative to if and select: you could use a format. That way you would move the logic setting class to the format definition, some people think, that this is just code obfuscation, but you should consider it.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;My rule-of-thumb: if you only do it once, do it in the code; otherwise move it to a format, document that, and Bob's your uncle.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example for using a format:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input risktype $3.;
datalines;
FHO
FHW
FHZ
FHH
FHI
FHY
FDP
FFZ
FHX
FMD
FMP
FCL
FCZ
FLP
FAZ
FDA
FIA
XXX
;

proc format library=work;
invalue risktype
  'FHO','FHW','FHZ' = 1
  'FHH','FHI','FHY' = 2
  'FDP','FFZ','FHX','FMD','FMP' = 3
  'FCL','FCZ','FLP' = 5
  'FAZ','FDA','FIA' = 6
  other = 7
;
run;

data want;
set have;
class = input(risktype,risktype.);
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;risktype    class

  FHO         1  
  FHW         1  
  FHZ         1  
  FHH         2  
  FHI         2  
  FHY         2  
  FDP         3  
  FFZ         3  
  FHX         3  
  FMD         3  
  FMP         3  
  FCL         5  
  FCZ         5  
  FLP         5  
  FAZ         6  
  FDA         6  
  FIA         6  
  XXX         7  
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 05:49:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/anything-wrong-with-my-if-statement/m-p/570826#M161008</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-03T05:49:45Z</dc:date>
    </item>
  </channel>
</rss>

