<?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: Basic IF-statement driving me to insanity soon in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Basic-IF-statement-driving-me-to-insanity-soon/m-p/813476#M321066</link>
    <description>If you are using SAS 9.4, you can use code like this in open code, but you need to use %if/%if%then syntax when you are using conditional logic with macro variables.</description>
    <pubDate>Mon, 16 May 2022 14:43:02 GMT</pubDate>
    <dc:creator>svh</dc:creator>
    <dc:date>2022-05-16T14:43:02Z</dc:date>
    <item>
      <title>Basic IF-statement driving me to insanity soon</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Basic-IF-statement-driving-me-to-insanity-soon/m-p/813468#M321060</link>
      <description>&lt;P&gt;I feel like I'm going insane trying to figure something out. Hoping that someone can explain the logic for me.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":weary_face:"&gt;😩&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We've got a data step with this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;amp;create_testvar_flg. = 1 then do;
	attrib testvar_nr format=11.0;
	testvar_nr = input(testvar.,best.);
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;When &amp;amp;create_testvar_flg is set to 1, this runs as intended. The variable is created and filled with values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However if&amp;nbsp;&amp;amp;create_testvar_flg &lt;STRONG&gt;isn't&lt;/STRONG&gt; 1, then the variable is &lt;STRONG&gt;still created&lt;/STRONG&gt;. It just isn't loaded. And the log does say "create_testvar_flg&amp;nbsp;resolves to 0".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've tried removing the that whole part of the code, and can confirm that the new variable isn't created at all then. So there's definitely something &lt;EM&gt;iffy&lt;/EM&gt; about the IF-statement above (pun intended). Is there any reason why it would run the attrib-line even though&amp;nbsp;&amp;amp;create_testvar_flg=0? How can that be prevented?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is part of reusable code used to load multiple tables. We want the new attribute to be created conditionally, only for some of them.&lt;/P&gt;</description>
      <pubDate>Mon, 16 May 2022 14:30:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Basic-IF-statement-driving-me-to-insanity-soon/m-p/813468#M321060</guid>
      <dc:creator>EinarRoed</dc:creator>
      <dc:date>2022-05-16T14:30:16Z</dc:date>
    </item>
    <item>
      <title>Re: Basic IF-statement driving me to insanity soon</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Basic-IF-statement-driving-me-to-insanity-soon/m-p/813470#M321062</link>
      <description>&lt;P&gt;I am not sure what you are talking about but you have code inside the IF statement that is NOT EXECUTABLE.&amp;nbsp; So it is NOT impacted by whether or not the IF condition is TRUE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you are running this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;attrib testvar_nr format=11.0;
if &amp;amp;create_testvar_flg. = 1 then do;
  testvar_nr = input(testvar,32.);
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do not want to generate the ATTRIB statement then use a macro %IF to prevent it from being generated.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;create_testvar_flg. = 1 %then %do;
  attrib testvar_nr format=11.0;
  testvar_nr = input(testvar,32.);
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS You cannot have a variable with a period on the end of its name.&amp;nbsp; There is no BEST informat, the concept makes no sense.&amp;nbsp; If you use BEST as an informat then SAS just defaults to the normal numeric informat.&lt;/P&gt;</description>
      <pubDate>Mon, 16 May 2022 14:37:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Basic-IF-statement-driving-me-to-insanity-soon/m-p/813470#M321062</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-16T14:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: Basic IF-statement driving me to insanity soon</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Basic-IF-statement-driving-me-to-insanity-soon/m-p/813476#M321066</link>
      <description>If you are using SAS 9.4, you can use code like this in open code, but you need to use %if/%if%then syntax when you are using conditional logic with macro variables.</description>
      <pubDate>Mon, 16 May 2022 14:43:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Basic-IF-statement-driving-me-to-insanity-soon/m-p/813476#M321066</guid>
      <dc:creator>svh</dc:creator>
      <dc:date>2022-05-16T14:43:02Z</dc:date>
    </item>
    <item>
      <title>Re: Basic IF-statement driving me to insanity soon</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Basic-IF-statement-driving-me-to-insanity-soon/m-p/813479#M321068</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/33000"&gt;@EinarRoed&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The structure of a dataset created with the usual (implicit or explicit) OUTPUT statement of a DATA step cannot depend on IF conditions because these are evaluated during &lt;EM&gt;execution&lt;/EM&gt; whereas the dataset structure is determined during &lt;EM&gt;compilation&lt;/EM&gt; of the DATA step. This is also the reason why the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1wxb7p9jkxycin16lz2db7idbnt.htm" target="_blank" rel="noopener"&gt;ATTRIB statement&lt;/A&gt; is not &lt;EM&gt;executable&lt;/EM&gt;, but &lt;EM&gt;declarative&lt;/EM&gt;&amp;nbsp;(see "Type" in the statement documentation, same for LENGTH, FORMAT, etc.). The %IF-%THEN &lt;EM&gt;macro&lt;/EM&gt; statement (as suggested by Tom) is processed before the DATA step is compiled. Thus the result of the compilation (and hence the dataset structure) will depend on the &lt;STRONG&gt;%&lt;/STRONG&gt;IF condition, as desired.&lt;/P&gt;</description>
      <pubDate>Mon, 16 May 2022 15:06:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Basic-IF-statement-driving-me-to-insanity-soon/m-p/813479#M321068</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-05-16T15:06:31Z</dc:date>
    </item>
    <item>
      <title>Re: Basic IF-statement driving me to insanity soon</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Basic-IF-statement-driving-me-to-insanity-soon/m-p/813493#M321077</link>
      <description>&lt;P&gt;The SAS data step language can &lt;EM&gt;&lt;STRONG&gt;conditionally change values&lt;/STRONG&gt;&lt;/EM&gt; of a variable, but it does not offer a way to &lt;EM&gt;&lt;STRONG&gt;conditionally include/exclude&lt;/STRONG&gt;&lt;/EM&gt; a variable from the resulting data set(s).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And consider the underlying principle - the sas complier, which transforms your code into executable statements, must make provision for the "conditional" variable, because the compiler must complete its work before any data is processed. The compiler has no way of knowing the data values.&amp;nbsp; Consequently, it must define the complete list of variables intended for the output data set prior to processing even one observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, there is a way to use hash object programming within a SAS data step to do what you want, but it involves code that is "masked" from the regular data step compiler.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 May 2022 16:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Basic-IF-statement-driving-me-to-insanity-soon/m-p/813493#M321077</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-05-16T16:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: Basic IF-statement driving me to insanity soon</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Basic-IF-statement-driving-me-to-insanity-soon/m-p/813993#M321292</link>
      <description>Thanks to everyone for great explanations. The code runs as intended now. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Wed, 18 May 2022 06:24:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Basic-IF-statement-driving-me-to-insanity-soon/m-p/813993#M321292</guid>
      <dc:creator>EinarRoed</dc:creator>
      <dc:date>2022-05-18T06:24:31Z</dc:date>
    </item>
  </channel>
</rss>

