<?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: elseif statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/elseif-statement/m-p/916760#M361101</link>
    <description>&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers/ta-p/352068/show-comments/false" target="_self"&gt;#Maxim4&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes it will work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;lt;a condition&amp;gt; then
  do;
    &amp;lt;code to run when true&amp;gt;
  end;
else
  do;
    &amp;lt;code to run when false&amp;gt;
  end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Mon, 19 Feb 2024 10:25:10 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2024-02-19T10:25:10Z</dc:date>
    <item>
      <title>elseif statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/elseif-statement/m-p/916758#M361100</link>
      <description>&lt;PRE&gt;data test;
    set test2;
    if card_type = 'F' then product = 'EC';
    else if customer = 'C' then product = 'bc_card';
    else if card_type in ('dg', 'dp', 'bg') then product = 'others';
run;&lt;/PRE&gt;
&lt;P&gt;does these statement make sense?&lt;/P&gt;
&lt;P&gt;else if does not have to focus on the same variable?&lt;/P&gt;
&lt;P&gt;here was referring to card_type but suddenly changed to customer&lt;/P&gt;
&lt;P&gt;does SAS allow this?&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2024 10:00:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/elseif-statement/m-p/916758#M361100</guid>
      <dc:creator>HeatherNewton</dc:creator>
      <dc:date>2024-02-19T10:00:47Z</dc:date>
    </item>
    <item>
      <title>Re: elseif statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/elseif-statement/m-p/916760#M361101</link>
      <description>&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers/ta-p/352068/show-comments/false" target="_self"&gt;#Maxim4&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes it will work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;lt;a condition&amp;gt; then
  do;
    &amp;lt;code to run when true&amp;gt;
  end;
else
  do;
    &amp;lt;code to run when false&amp;gt;
  end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2024 10:25:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/elseif-statement/m-p/916760#M361101</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-02-19T10:25:10Z</dc:date>
    </item>
    <item>
      <title>Re: elseif statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/elseif-statement/m-p/916762#M361102</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416388"&gt;@HeatherNewton&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;does these statement make sense?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Since we don't know the subject matter or the purpose of the code, this is impossible for us to answer.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2024 10:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/elseif-statement/m-p/916762#M361102</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-02-19T10:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: elseif statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/elseif-statement/m-p/916778#M361110</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416388"&gt;@HeatherNewton&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;data test;
    set test2;
    if card_type = 'F' then product = 'EC';
    else if customer = 'C' then product = 'bc_card';
    else if card_type in ('dg', 'dp', 'bg') then product = 'others';
run;&lt;/PRE&gt;
&lt;P&gt;does these statement make sense?&lt;/P&gt;
&lt;P&gt;else if does not have to focus on the same variable?&lt;/P&gt;
&lt;P&gt;here was referring to card_type but suddenly changed to customer&lt;/P&gt;
&lt;P&gt;does SAS allow this?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Correct: the "else if" condition need not have the same variable as its preceding "if" or preceding "else if".&amp;nbsp; &amp;nbsp;Not only does "SAS allow this" but most programing languages would allow it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the restriction you are concerned about is true with certain forms of the SELECT group. such as:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  select (color);
    when ('red') cx=1;
    when ('blue') cx=2;
    when ('green') cx=3;
    when ('yellow') cx=4;
    otherwise cx=0;
  end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "when" statements are effectively "else if" tests - i.e. once a when condition is true, all subsequent tests are skipped.&amp;nbsp; &amp;nbsp;And in this case, where the SELECT statement has a single test expression, all the subsequent WHENs must evaluate that same expression.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note if there is no argument to the select statement, you could use different test variables in the WHEN statements, just like your "else if" sample.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2024 13:17:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/elseif-statement/m-p/916778#M361110</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-02-19T13:17:28Z</dc:date>
    </item>
    <item>
      <title>Re: elseif statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/elseif-statement/m-p/916782#M361112</link>
      <description>&lt;P&gt;This is a good one to play with to figure out if/how it works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can make work.test2, then run your step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
    set test2;
    if card_type = 'F' then product = 'EC';
    else if customer = 'C' then product = 'bc_card';
    else if card_type in ('dg', 'dp', 'bg') then product = 'others';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and check the log to make sure there are no errors/warnings/bad notes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then check the results with something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=test;
  tables card_type*customer*product/missing list;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Feb 2024 13:43:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/elseif-statement/m-p/916782#M361112</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-02-19T13:43:37Z</dc:date>
    </item>
    <item>
      <title>Re: elseif statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/elseif-statement/m-p/916786#M361114</link>
      <description>&lt;P&gt;As others have indicated, the statements will run without error.&amp;nbsp; However, there is a flaw.&amp;nbsp; If those statements are creating a new variable named PRODUCT, it will be defined as two characters long.&amp;nbsp; The first mention of PRODUCT (= 'EC') defines its length.&amp;nbsp; So the values you will get are "EC", "bc", and "ot".&amp;nbsp; The fix is simple:&amp;nbsp; add your own LENGTH statement before SAS sets the length for you:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
    set test2;
    length product $ 7;
    if card_type = 'F' then product = 'EC';
    else if customer = 'C' then product = 'bc_card';
    else if card_type in ('dg', 'dp', 'bg') then product = 'others';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The length of 7 allows room for the longest value being assigned ("bc_card").&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2024 14:02:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/elseif-statement/m-p/916786#M361114</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-02-19T14:02:16Z</dc:date>
    </item>
  </channel>
</rss>

