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.
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).
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.
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;
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.
@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.
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).
@FreelanceReinh, thanks and updated. Wonder why I never could get high scores in school.
Thanks for all the suggestions! You guys are great to be so helpful. I LOVE this forum! You guys are the best!!
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.