<?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 new variables with if then else statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-with-if-then-else-statement/m-p/729090#M226866</link>
    <description>&lt;P&gt;Your KEEP statement only says to keep A, B and a VAR variable, not C so C is dropped.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;data new;

set old;


if A=. and B ne . then C=B;
else if A ne . then C=A;

Keep var A B; &lt;FONT size="4" color="#FF6600"&gt;&lt;STRONG&gt;*MISSING C;&lt;/STRONG&gt;&lt;/FONT&gt;

run;&lt;/PRE&gt;
&lt;P&gt;Note that your logic is essentially the COALESCE function, so your IF/THEN can become:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;C = coalesce(a, b);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/375336"&gt;@Mion&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I started SAS several days ago and I have a problem with if then else statement. What I tried to do was to keep some of the variables from the old dataset, which indicate exactly the same data but asked differently, and to create new variables from filtered ones.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the code I used:&lt;/P&gt;
&lt;P&gt;data new;&lt;/P&gt;
&lt;P&gt;set old;&lt;/P&gt;
&lt;P&gt;Keep var A B;&lt;/P&gt;
&lt;P&gt;if A=. and B ne . then C=B;&lt;BR /&gt;else if A ne . then C=A;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was able to see A and B in my new dataset but C has never been created. Could you let me know if there's something went wrong with it? Thank you in advance!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 25 Mar 2021 15:20:46 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-03-25T15:20:46Z</dc:date>
    <item>
      <title>Creating new variables with if then else statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-with-if-then-else-statement/m-p/729088#M226865</link>
      <description>&lt;P&gt;Hi, I started SAS several days ago and I have a problem with if then else statement. What I tried to do was to keep some of the variables from the old dataset, which indicate exactly the same data but asked differently, and to create new variables from filtered ones.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the code I used:&lt;/P&gt;&lt;P&gt;data new;&lt;/P&gt;&lt;P&gt;set old;&lt;/P&gt;&lt;P&gt;Keep var A B;&lt;/P&gt;&lt;P&gt;if A=. and B ne . then C=B;&lt;BR /&gt;else if A ne . then C=A;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was able to see A and B in my new dataset but C has never been created. Could you let me know if there's something went wrong with it? Thank you in advance!&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 15:14:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-with-if-then-else-statement/m-p/729088#M226865</guid>
      <dc:creator>Mion</dc:creator>
      <dc:date>2021-03-25T15:14:36Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables with if then else statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-with-if-then-else-statement/m-p/729090#M226866</link>
      <description>&lt;P&gt;Your KEEP statement only says to keep A, B and a VAR variable, not C so C is dropped.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;data new;

set old;


if A=. and B ne . then C=B;
else if A ne . then C=A;

Keep var A B; &lt;FONT size="4" color="#FF6600"&gt;&lt;STRONG&gt;*MISSING C;&lt;/STRONG&gt;&lt;/FONT&gt;

run;&lt;/PRE&gt;
&lt;P&gt;Note that your logic is essentially the COALESCE function, so your IF/THEN can become:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;C = coalesce(a, b);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/375336"&gt;@Mion&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I started SAS several days ago and I have a problem with if then else statement. What I tried to do was to keep some of the variables from the old dataset, which indicate exactly the same data but asked differently, and to create new variables from filtered ones.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the code I used:&lt;/P&gt;
&lt;P&gt;data new;&lt;/P&gt;
&lt;P&gt;set old;&lt;/P&gt;
&lt;P&gt;Keep var A B;&lt;/P&gt;
&lt;P&gt;if A=. and B ne . then C=B;&lt;BR /&gt;else if A ne . then C=A;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was able to see A and B in my new dataset but C has never been created. Could you let me know if there's something went wrong with it? Thank you in advance!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 15:20:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-with-if-then-else-statement/m-p/729090#M226866</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-25T15:20:46Z</dc:date>
    </item>
  </channel>
</rss>

