<?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: Why am I getting error when using function 'length' to test a variable without a value? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261818#M51017</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21089"&gt;@ncsthbell﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When either of these &lt;EM&gt;macro&lt;/EM&gt; variables resolves to a null string, the &lt;EM&gt;data step&lt;/EM&gt; function LENGTH is called like&lt;/P&gt;
&lt;PRE&gt;length()&lt;/PRE&gt;
&lt;P&gt;which is invalid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To resolve the issue, you can use the appropriate &lt;EM&gt;macro&lt;/EM&gt; function %LENGTH&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if %length(&amp;amp;From_Amt) &amp;gt; 0 AND %length(&amp;amp;To_Amt) &amp;gt; 0 then do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or use Haikuo's solution (with "length&lt;STRONG&gt;n&lt;/STRONG&gt;" in &lt;EM&gt;both&lt;/EM&gt; places).&lt;/P&gt;</description>
    <pubDate>Wed, 06 Apr 2016 16:26:21 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2016-04-06T16:26:21Z</dc:date>
    <item>
      <title>Why am I getting error when using function 'length' to test a variable without a value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261797#M51008</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have created a variable that will be&amp;nbsp;a&amp;nbsp;prompt/filter&amp;nbsp;for a user to&amp;nbsp;enter a value into.&amp;nbsp;&amp;nbsp;I am testing that variable to see if the length is&amp;nbsp;&amp;gt; than 0 to tell me if a value was entered and based on that, write some output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My problem is that if I set the prompt/filter to have a value (below), the code runs fine.&lt;/P&gt;
&lt;P&gt;%LET FROM_AMT = 1;&lt;/P&gt;
&lt;P&gt;%LET TO_AMT = 500;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%LET FROM_AMT = ;
%LET TO_AMT =;


data FILTERS;
    if length(&amp;amp;From_Amt) &amp;gt; 0 AND length(&amp;amp;To_Amt) &amp;gt; 0 then do;
       filter_name = "Gross Receipts";
       filter_from = "&amp;amp;from_amt";
       filter_to = "&amp;amp;to_amt";
       output;   
    end; 
run;When I leave the variable blank which indicates the user has not&amp;nbsp;entered a value and then test the length, I get the following error:&amp;nbsp;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I leave the variable blank which indicates the user has not&amp;nbsp;entered a value and then test the length, I get the following error:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;291&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data FILTERS;&lt;/P&gt;
&lt;P&gt;292&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;if length(&amp;amp;DISTRICT) &amp;gt; 0 then do;&lt;/P&gt;
&lt;P&gt;293&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; length filter_name filter_from filter_to $50;&lt;/P&gt;
&lt;P&gt;294&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; filter_name = "District";&lt;/P&gt;
&lt;P&gt;295&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; filter_from = "&amp;amp;DISTRICT";&lt;/P&gt;
&lt;P&gt;296&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;297&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;298&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if length(&amp;amp;From_Amt) &amp;gt; 0 AND length(&amp;amp;To_Amt) &amp;gt; 0 then do;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ______&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ______&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 71&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 71&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;ERROR 71-185: The LENGTH function call does not have enough arguments.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2016 16:01:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261797#M51008</guid>
      <dc:creator>ncsthbell</dc:creator>
      <dc:date>2016-04-06T16:01:02Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I getting error when using function 'length' to test a variable without a value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261808#M51011</link>
      <description>&lt;P&gt;You get the error because the macro variable you use as an argument resolves to an empty string. Length() requires exactly one argument but gets zero.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can add extra code to take care of the situation where TO_AMT is empty. Something along the lines of (untested):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if length(&amp;amp;From_Amt) &amp;gt; 0 

%if %not &amp;amp;TO_AMT= %then %do

AND length(&amp;amp;To_Amt) &amp;gt; 0 

%end; 

then do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;should do the trick. Using macro %if/%then/%else logic requires your code to be in a macro definition. You will have to add %macro / %mend around your code and then call the macro (%macname). This way you enter a world that may be new to you. The web has many good resouyrces om SAS macros. Very powerfull but it requires some study.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2016 16:13:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261808#M51011</guid>
      <dc:creator>jklaverstijn</dc:creator>
      <dc:date>2016-04-06T16:13:23Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I getting error when using function 'length' to test a variable without a value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261809#M51012</link>
      <description>&lt;P&gt;First, for your purpose, LENGTHN() will be a better choice, as if encountering a blank string, LENGTHN() gives you 0, while length gives you 1. Second, both&amp;nbsp;functions will require something instead of complete nothing. So&amp;nbsp;you need to quote it to give them a char blank/null string.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if lengthn("&amp;amp;From_Amt") &amp;gt; 0 AND lengthn("&amp;amp;To_Amt") &amp;gt; 0 then do;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Apr 2016 16:28:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261809#M51012</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-04-06T16:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I getting error when using function 'length' to test a variable without a value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261812#M51015</link>
      <description>&lt;P&gt;Ah yes of course. Much better answer &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4877"&gt;@Haikuo﻿&lt;/a&gt;. Also, without the quotes the function will interpret the argument as a variable name. Likely an other source of errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2016 16:16:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261812#M51015</guid>
      <dc:creator>jklaverstijn</dc:creator>
      <dc:date>2016-04-06T16:16:22Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I getting error when using function 'length' to test a variable without a value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261817#M51016</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12460"&gt;@jklaverstijn&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Also, without the quotes the function will interpret the argument as a variable name. Likely an other source of errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- Jan.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Depends. without quotes,&amp;nbsp;If the macro variable resovles to something in compliance with SAS variable naming convention, then yes; while if it resovles to a number, then it will be treated as a number, this will be when you want it to be a number instead of digits string.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2016 16:25:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261817#M51016</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-04-06T16:25:36Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I getting error when using function 'length' to test a variable without a value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261818#M51017</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21089"&gt;@ncsthbell﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When either of these &lt;EM&gt;macro&lt;/EM&gt; variables resolves to a null string, the &lt;EM&gt;data step&lt;/EM&gt; function LENGTH is called like&lt;/P&gt;
&lt;PRE&gt;length()&lt;/PRE&gt;
&lt;P&gt;which is invalid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To resolve the issue, you can use the appropriate &lt;EM&gt;macro&lt;/EM&gt; function %LENGTH&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if %length(&amp;amp;From_Amt) &amp;gt; 0 AND %length(&amp;amp;To_Amt) &amp;gt; 0 then do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or use Haikuo's solution (with "length&lt;STRONG&gt;n&lt;/STRONG&gt;" in &lt;EM&gt;both&lt;/EM&gt; places).&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2016 16:26:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261818#M51017</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-04-06T16:26:21Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I getting error when using function 'length' to test a variable without a value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261821#M51019</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh﻿&lt;/a&gt;, thanks and updated. Wonder why I&amp;nbsp;never could&amp;nbsp;get high scores in school.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2016 16:29:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261821#M51019</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-04-06T16:29:06Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I getting error when using function 'length' to test a variable without a value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261850#M51031</link>
      <description>&lt;P&gt;Thanks for all the suggestions!&amp;nbsp; You guys are great to be so helpful. I LOVE this forum!&amp;nbsp;&amp;nbsp; You guys are the best!!&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2016 17:15:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-am-I-getting-error-when-using-function-length-to-test-a/m-p/261850#M51031</guid>
      <dc:creator>ncsthbell</dc:creator>
      <dc:date>2016-04-06T17:15:03Z</dc:date>
    </item>
  </channel>
</rss>

