DATA Step, Macro, Functions and more

Why am I getting error when using function 'length' to test a variable without a value?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

Why am I getting error when using function 'length' to test a variable without a value?

Hello,

I have created a variable that will be a prompt/filter for a user to enter a value into.  I am testing that variable to see if the length is > than 0 to tell me if a value was entered and based on that, write some output.

 

My problem is that if I set the prompt/filter to have a value (below), the code runs fine.

%LET FROM_AMT = 1;

%LET TO_AMT = 500;

 

%LET FROM_AMT = ;
%LET TO_AMT =;


data FILTERS;
    if length(&From_Amt) > 0 AND length(&To_Amt) > 0 then do;
       filter_name = "Gross Receipts";
       filter_from = "&from_amt";
       filter_to = "&to_amt";
       output;   
    end; 
run;When I leave the variable blank which indicates the user has not entered a value and then test the length, I get the following error: 

 

 

When I leave the variable blank which indicates the user has not entered a value and then test the length, I get the following error: 

291       data FILTERS;

292            if length(&DISTRICT) > 0 then do;

293               length filter_name filter_from filter_to $50;

294               filter_name = "District";

295               filter_from = "&DISTRICT";

296               output;

297           end;

298           if length(&From_Amt) > 0 AND length(&To_Amt) > 0 then do;

                  ______                        ______

                  71                            71

ERROR 71-185: The LENGTH function call does not have enough arguments.


Accepted Solutions
Solution
‎04-06-2016 01:15 PM
Trusted Advisor
Posts: 1,115

Re: Why am I getting error when using function 'length' to test a variable without a value?

Hello @ncsthbell,

 

When either of these macro variables resolves to a null string, the data step function LENGTH is called like

length()

which is invalid.

 

To resolve the issue, you can use the appropriate macro function %LENGTH

if %length(&From_Amt) > 0 AND %length(&To_Amt) > 0 then do;

or use Haikuo's solution (with "lengthn" in both places).

View solution in original post


All Replies
Super Contributor
Posts: 408

Re: Why am I getting error when using function 'length' to test a variable without a value?

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.

 

You can add extra code to take care of the situation where TO_AMT is empty. Something along the lines of (untested):

 

if length(&From_Amt) > 0 

%if %not &TO_AMT= %then %do

AND length(&To_Amt) > 0 

%end; 

then do;

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.

 

Regards,

- Jan.

Respected Advisor
Posts: 3,124

Re: Why am I getting error when using function 'length' to test a variable without a value?

[ Edited ]

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 functions will require something instead of complete nothing. So you need to quote it to give them a char blank/null string. 

if lengthn("&From_Amt") > 0 AND lengthn("&To_Amt") > 0 then do;
Super Contributor
Posts: 408

Re: Why am I getting error when using function 'length' to test a variable without a value?

Ah yes of course. Much better answer @Haikuo. Also, without the quotes the function will interpret the argument as a variable name. Likely an other source of errors.

 

- Jan.

Respected Advisor
Posts: 3,124

Re: Why am I getting error when using function 'length' to test a variable without a value?


jklaverstijn wrote:

Also, without the quotes the function will interpret the argument as a variable name. Likely an other source of errors.

 

- Jan.


Depends. without quotes, 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.

Solution
‎04-06-2016 01:15 PM
Trusted Advisor
Posts: 1,115

Re: Why am I getting error when using function 'length' to test a variable without a value?

Hello @ncsthbell,

 

When either of these macro variables resolves to a null string, the data step function LENGTH is called like

length()

which is invalid.

 

To resolve the issue, you can use the appropriate macro function %LENGTH

if %length(&From_Amt) > 0 AND %length(&To_Amt) > 0 then do;

or use Haikuo's solution (with "lengthn" in both places).

Respected Advisor
Posts: 3,124

Re: Why am I getting error when using function 'length' to test a variable without a value?

@FreelanceReinhard, thanks and updated. Wonder why I never could get high scores in school.

Frequent Contributor
Posts: 82

Re: Why am I getting error when using function 'length' to test a variable without a value?

Thanks for all the suggestions!  You guys are great to be so helpful. I LOVE this forum!   You guys are the best!!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 309 views
  • 0 likes
  • 4 in conversation