<?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: If statement not executing correctly in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-statement-not-executing-correctly/m-p/594694#M170910</link>
    <description>&lt;P&gt;Right now, SAS interprets the dot in &amp;amp;format. as the end of the macro variable. Not the end of a format specification. Therefore, put an extra dot efter your format macro variable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	set test;

	have_format = 0;

	if have_format then do;
		formatted_var = put(unformatted_var, &amp;amp;format..);
	end; else do;
		formatted_var = unformatted_var;
	end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will issue a note, not an error, that the format was not found because the Put Function looks for the format at compile time.&lt;/P&gt;</description>
    <pubDate>Tue, 08 Oct 2019 11:04:22 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-10-08T11:04:22Z</dc:date>
    <item>
      <title>If statement not executing correctly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-not-executing-correctly/m-p/594690#M170906</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was wondering if someone could explain to me why my code isn't working correctly. I think it's something to do with how the SAS processor handles macro variables but I'm not entirely sure.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	input unformatted_var $;
	DATALINES;
	1
	;
run;

%let format = not_a_format;

data test;
	set test;

	have_format = 0;

	if have_format then do;
		formatted_var = put(unformatted_var, &amp;amp;format.);
	end; else do;
		formatted_var = unformatted_var;
	end;

run;


PROC PRINT DATA = test; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code is a simplified version of my main code which runs within a macro. It's purpose is to take unformatted_var and output it as formatted_var. Sometimes the code will have a format stored in the "&lt;CODE class=" language-sas"&gt;format"&amp;nbsp;macro&amp;nbsp;&lt;/CODE&gt;variable in which case a format is applied to the formatted _var, other times there is no format supplied and the formatted_var is the same as unformatted_var.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the case above there is no format supplied so the have_format variable is set to 0. However despite this the code produces an error because it is trying to execute the line&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;formatted_var = put(unformatted_var, &amp;amp;format.);&lt;/PRE&gt;&lt;P&gt;Which can't run correctly because the "format" macro variable does not contain a format, even though that line should not be executed within the data step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am able to solve my problem by coding it differently, but I'm not sure why it's not working in the first place!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 10:50:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-not-executing-correctly/m-p/594690#M170906</guid>
      <dc:creator>StephenM</dc:creator>
      <dc:date>2019-10-08T10:50:11Z</dc:date>
    </item>
    <item>
      <title>Re: If statement not executing correctly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-not-executing-correctly/m-p/594694#M170910</link>
      <description>&lt;P&gt;Right now, SAS interprets the dot in &amp;amp;format. as the end of the macro variable. Not the end of a format specification. Therefore, put an extra dot efter your format macro variable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	set test;

	have_format = 0;

	if have_format then do;
		formatted_var = put(unformatted_var, &amp;amp;format..);
	end; else do;
		formatted_var = unformatted_var;
	end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will issue a note, not an error, that the format was not found because the Put Function looks for the format at compile time.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 11:04:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-not-executing-correctly/m-p/594694#M170910</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-08T11:04:22Z</dc:date>
    </item>
    <item>
      <title>Re: If statement not executing correctly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-not-executing-correctly/m-p/594698#M170917</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83690"&gt;@StephenM&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The correctness of SAS syntax gets checked during compilation time. Because the format doesn't exist this leads to an error.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The IF conditions comes only into play during execution time - and that's after the syntax check during compilation time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On top of this: A format needs to end with a dot. Resolving a macro variable consumes a dot. So even if there is a format supplied in your macro variable you need to use two dots for the resolved value of the macro variable (the format name) still to end with a dot.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;formatted_var &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;put&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;unformatted_var&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token procnames"&gt;format&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;..&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;CODE&gt;&lt;/CODE&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could avoid the error for a non existing format via option&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options nofmterr;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or - and better in my opinion - don't generate the SAS code at all if there is no value supplied for the format (assuming that macro variable &amp;amp;format exists but is empty if no format gets supplied).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input unformatted_var $;
  DATALINES;
1
;

%let format =;
%macro doit();
  data test;
    set test;
    %if %bquote(&amp;amp;format) = %bquote() %then
      %do;
        formatted_var = unformatted_var;
      %end;
    %else
      %do;
        formatted_var = put(unformatted_var, &amp;amp;format..);
      %end;
  run;
%mend;
%doit()

PROC PRINT DATA = test;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you're on a very recent SAS version then you can use SAS macro %IF %then %else even without a macro definition.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input unformatted_var $;
  DATALINES;
1
;

%let format =;

data test;
  set test;
  %if %bquote(&amp;amp;format) = %bquote() %then
    %do;
      formatted_var = unformatted_var;
    %end;
  %else
    %do;
      formatted_var = put(unformatted_var, &amp;amp;format..);
    %end;
run;

PROC PRINT DATA = test;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 11:10:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-not-executing-correctly/m-p/594698#M170917</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-10-08T11:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: If statement not executing correctly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-not-executing-correctly/m-p/594705#M170924</link>
      <description>&lt;P&gt;If you want to conditionally use a format, use either putc or putn:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	set test;

	have_format = 0;

	if have_format then do;
		formatted_var = putc(unformatted_var, "&amp;amp;format..");
	end; else do;
		formatted_var = unformatted_var;
	end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since the format can be dynamic with these functions, the check occurs during runtime only when the function is actually called.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 11:32:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-not-executing-correctly/m-p/594705#M170924</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-08T11:32:10Z</dc:date>
    </item>
    <item>
      <title>Re: If statement not executing correctly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-not-executing-correctly/m-p/594712#M170927</link>
      <description>&lt;P&gt;Thanks for the replies everyone. I didn't realise SAS checked syntax before executing, I'll take this into account with my code.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 11:45:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-not-executing-correctly/m-p/594712#M170927</guid>
      <dc:creator>StephenM</dc:creator>
      <dc:date>2019-10-08T11:45:12Z</dc:date>
    </item>
    <item>
      <title>Re: If statement not executing correctly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-not-executing-correctly/m-p/594715#M170930</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83690"&gt;@StephenM&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using PUTC/PUTN as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;suggests is another option closer to the code you've posted as there the check of the format only happens during execution time. For this reason PUTC/PUTN also doesn't perform as good as PUT&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 12:00:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-not-executing-correctly/m-p/594715#M170930</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-10-08T12:00:38Z</dc:date>
    </item>
  </channel>
</rss>

