BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ncsthbell
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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

7 REPLIES 7
jklaverstijn
Rhodochrosite | Level 12

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.

Haikuo
Onyx | Level 15

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;
jklaverstijn
Rhodochrosite | Level 12

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.

Haikuo
Onyx | Level 15

@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.

FreelanceReinh
Jade | Level 19

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).

Haikuo
Onyx | Level 15

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

ncsthbell
Quartz | Level 8

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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