<?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: Empty String in IF Statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Empty-String-in-IF-Statement/m-p/475855#M122388</link>
    <description>&lt;P&gt;If you are using SAS 9.4M5 then you can use a %IF block in open code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  &amp;amp;var1._1 = &amp;amp;var1 / 2;
%if %length(&amp;amp;var2) %then %do;
  &amp;amp;var2._2 = &amp;amp;var2. / 2;
%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 05 Jul 2018 22:40:27 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-07-05T22:40:27Z</dc:date>
    <item>
      <title>Empty String in IF Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Empty-String-in-IF-Statement/m-p/475837#M122381</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following code works when macro var2 is defined as an non-missing existing variable name. But when I only want var1 and leave var2 empty (basically let the if condition fail and don't do the var2 transformation), it fails. How should I correct it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var1 = weight;
%let var2 = age; &lt;BR /&gt;/*%let var2 = ;*/

data want;
set have;
&amp;amp;var1._1 = &amp;amp;var1 / 2;
if "%str(&amp;amp;var2.)" ^= "" then do;
	&amp;amp;var2._2 = &amp;amp;var2. / 2;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 21:24:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Empty-String-in-IF-Statement/m-p/475837#M122381</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2018-07-05T21:24:56Z</dc:date>
    </item>
    <item>
      <title>Re: Empty String in IF Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Empty-String-in-IF-Statement/m-p/475848#M122385</link>
      <description>&lt;P&gt;I don't think you can&amp;nbsp;generate conditional code without a macro, such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro a(dsn, var1, var2);
data &amp;amp;dsn;
set sashelp.class;
&amp;amp;var1._1 = &amp;amp;var1 / 2;
%if %length(&amp;amp;var2) &amp;gt; 0 %then &amp;amp;var2._2 = &amp;amp;var2. / 2 ;;
run;
%mend;

%a(want1, weight, age);
%a(want2, weight,);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 22:18:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Empty-String-in-IF-Statement/m-p/475848#M122385</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-07-05T22:18:18Z</dc:date>
    </item>
    <item>
      <title>Re: Empty String in IF Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Empty-String-in-IF-Statement/m-p/475851#M122386</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/69320"&gt;@apolitical&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following code works when macro var2 is defined as an non-missing existing variable name. But when I only want var1 and leave var2 empty (basically let the if condition fail and don't do the var2 transformation), it fails. How should I correct it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var1 = weight;
%let var2 = age; &lt;BR /&gt;/*%let var2 = ;*/

data want;
set have;
&amp;amp;var1._1 = &amp;amp;var1 / 2;
if "%str(&amp;amp;var2.)" ^= "" then do;
	&amp;amp;var2._2 = &amp;amp;var2. / 2;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Of course it fails.&amp;nbsp; Just replace the macro variable references with their values and look at the statements that result.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
weight_1 = weight / 2;
if "" ^= "" then do;
	_2 =  / 2;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could get the SAS code to run if you changed it like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if "%str(&amp;amp;var2.)" ^= "" then do;
	&amp;amp;var2._2 = (&amp;amp;var2.+0) / 2;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then it would generate valid SAS code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if "" ^= "" then do;
	_2 =  (+0)/ 2;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course I am not sure why you would want to generate the variable _2 , but at least the step will run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 22:38:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Empty-String-in-IF-Statement/m-p/475851#M122386</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-05T22:38:32Z</dc:date>
    </item>
    <item>
      <title>Re: Empty String in IF Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Empty-String-in-IF-Statement/m-p/475853#M122387</link>
      <description>&lt;P&gt;Since you seem to be doing the same thing to each variable why not just use ARRAY processing?&lt;/P&gt;
&lt;P&gt;Here is code that takes a list of variables and creates new variables by adding _2 to the variable name and dividing the value of the corresponding old value by 2.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let varlist = weight age;

data want;
  set sashelp.class;
  array old &amp;amp;varlist ;
  array new %sysfunc(tranwrd(%sysfunc(compbl(&amp;amp;varlist)),%str( ),%str(_2 )))_2;
  do _n_=1 to dim(old);
    new(_n_)=old(_n_)/2 ;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 402px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21593i79647B8EB0FAF48B/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 22:35:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Empty-String-in-IF-Statement/m-p/475853#M122387</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-05T22:35:12Z</dc:date>
    </item>
    <item>
      <title>Re: Empty String in IF Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Empty-String-in-IF-Statement/m-p/475855#M122388</link>
      <description>&lt;P&gt;If you are using SAS 9.4M5 then you can use a %IF block in open code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  &amp;amp;var1._1 = &amp;amp;var1 / 2;
%if %length(&amp;amp;var2) %then %do;
  &amp;amp;var2._2 = &amp;amp;var2. / 2;
%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Jul 2018 22:40:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Empty-String-in-IF-Statement/m-p/475855#M122388</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-05T22:40:27Z</dc:date>
    </item>
    <item>
      <title>Re: Empty String in IF Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Empty-String-in-IF-Statement/m-p/476542#M122653</link>
      <description>&lt;P&gt;Thanks everyone!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jul 2018 17:58:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Empty-String-in-IF-Statement/m-p/476542#M122653</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2018-07-09T17:58:34Z</dc:date>
    </item>
  </channel>
</rss>

