<?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: How to set with an if statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-with-an-if-statement/m-p/800143#M314719</link>
    <description>&lt;P&gt;First, please understand the difference between &lt;FONT face="courier new,courier"&gt;IF&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;%IF&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;IF&lt;/FONT&gt; works only on data step variables or constants. &lt;FONT face="courier new,courier"&gt;%IF&lt;/FONT&gt; works only on macro variables. So this line uses &lt;FONT face="courier new,courier"&gt;IF&lt;/FONT&gt; when it should use &lt;FONT face="courier new,courier"&gt;%IF&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;amp;var. = 'yes' &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To fix this, you need appropriate macro statements and syntax: &lt;FONT face="courier new,courier"&gt;%IF %THEN %DO&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;%ELSE %DO&lt;/FONT&gt; and the appropriate &lt;FONT face="courier new,courier"&gt;%END;&lt;/FONT&gt; after each.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var=yes;
data test;
    %if &amp;amp;var=yes %then %do; set lib1.base_1; %end;
    %else %do; set lib2.base_2; %end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: the above code works in SAS 9.4M5 or later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 04 Mar 2022 13:55:56 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2022-03-04T13:55:56Z</dc:date>
    <item>
      <title>How to set with an if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-with-an-if-statement/m-p/800139#M314717</link>
      <description>&lt;P&gt;I'm trying to do a condition for my data sets one of two bases. For that I'm using the if statement, but it's not working. For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data work.test;&lt;BR /&gt;if &amp;amp;var. = 'yes' then set lib1.base_1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else set lib2.base_2;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is always setting lib2.base_2 and creating a variable called yes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I do to automatically sets one of this two bases only changing the variable &amp;amp;var.?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 13:34:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-with-an-if-statement/m-p/800139#M314717</guid>
      <dc:creator>Caíque</dc:creator>
      <dc:date>2022-03-04T13:34:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to set with an if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-with-an-if-statement/m-p/800143#M314719</link>
      <description>&lt;P&gt;First, please understand the difference between &lt;FONT face="courier new,courier"&gt;IF&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;%IF&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;IF&lt;/FONT&gt; works only on data step variables or constants. &lt;FONT face="courier new,courier"&gt;%IF&lt;/FONT&gt; works only on macro variables. So this line uses &lt;FONT face="courier new,courier"&gt;IF&lt;/FONT&gt; when it should use &lt;FONT face="courier new,courier"&gt;%IF&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;amp;var. = 'yes' &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To fix this, you need appropriate macro statements and syntax: &lt;FONT face="courier new,courier"&gt;%IF %THEN %DO&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;%ELSE %DO&lt;/FONT&gt; and the appropriate &lt;FONT face="courier new,courier"&gt;%END;&lt;/FONT&gt; after each.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var=yes;
data test;
    %if &amp;amp;var=yes %then %do; set lib1.base_1; %end;
    %else %do; set lib2.base_2; %end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: the above code works in SAS 9.4M5 or later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 13:55:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-with-an-if-statement/m-p/800143#M314719</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-03-04T13:55:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to set with an if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-set-with-an-if-statement/m-p/800153#M314722</link>
      <description>&lt;P&gt;Also note, macro language merely substitutes for &amp;amp;VAR to generate a SAS language statement.&amp;nbsp; Your SAS program includes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if yes = 'yes' then set lib1.base_1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hopefully, if you saw this statement in your program you would realize that it does the wrong thing.&amp;nbsp; (This also explains why SAS sees the need to create a variable named YES.)&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The solutions already suggested are best.&amp;nbsp; But recognize that you could also get the right result (but at the cost of a longer-running program) by adding double quotes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if "&amp;amp;var" = 'yes' then set lib1.base_1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This would generate a clumsy but workable program containing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if "yes" = 'yes' then set lib1.base_1;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Mar 2022 13:53:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-set-with-an-if-statement/m-p/800153#M314722</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-03-04T13:53:40Z</dc:date>
    </item>
  </channel>
</rss>

