SAS Scan doesn't like the second argument (a do loop counter)

1.1k Views Asked by At

Need a second pair of eyes on this:

%MACRO FIND_CLOSEST_LIMIT_OR_DEDUCTIBLE(limOrDedOption,limOrDedValue);
    %LET numOptions = %Sysfunc(Countw(&limOrDedOption.));

    limOrDedSetFlag = 'N';
    %DO curLimOrDed = 1 %TO &numOptions.;
        IF limOrDedSetFlag = 'N' AND &curLimOrDed = 1 AND &limOrDedValue. <= %Scan(&limOrDedOption.,&curLimOrDed.) THEN DO;
            &limOrDedValue. = %Scan(&limOrDedOption,1);
            limOrDedSetFlag = 'Y';
        END;
        IF limOrDedSetFlag = 'N' AND &curLimOrDed. >= 2 THEN DO;
            lowerLimOrDed = %Scan(&limOrDedOption,prevLimOrDed, &curLimOrDed. - 1);
            upperLimOrDed = %Scan(&limOrDedOption.,&curLimOrDed);
            IF &limOrDedValue. < %Scan(&limOrDedOption.,&curLimOrDed) THEN DO;
                IF (&limOrDedValue. - lowerLimOrDed)/ (upperLimOrDed - lowerLimOrDed) < 0.5 THEN 
                    &limOrDedValue. = lowerLimOrDed;
                ELSE &limOrDedValue. = upperLimOrDed;
                limOrDedSetFlag = 'Y';
            END;
        END;
    %END;

    IF MISSING(&limOrDedValue.) THEN &limOrDedValue. = 0;
/*  DROP lowerLimOrDed upperLimOrDed;*/
%MEND FIND_CLOSEST_LIMIT_OR_DEDUCTIBLE;

Receiving the following error:

MPRINT(FIND_CLOSEST_LIMIT_OR_DEDUCTIBLE):   IF limOrDedSetFlag = 'N' AND 1 >= 2 THEN DO;
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 
       prevLimOrDed 
ERROR: Argument 2 to macro function %SCAN is not a number.
NOTE 137-205: Line generated by the invoked macro "FIND_CLOSEST_LIMIT_OR_DEDUCTIBLE".
104         DO;    lowerLimOrDed = %Scan(&limOrDedOption,prevLimOrDed, &curLimOrDed. - 1);    upperLimOrDed =
                                                                                         _
                                                                                         22
104      ! %Scan(&limOrDedOption.,&curLimOrDed);    IF &limOrDedValue. < %Scan(&limOrDedOption.,&curLimOrDed) THEN DO;     IF
104      ! (&limOrDedValue. - lowerLimOrDed)/ (
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, 
              a missing value, INPUT, PUT.  

It looks like SAS doesn't like the &curLimOrDed - 1. Any help?

Thank you!

2

There are 2 best solutions below

1
vasja On

I'd guess you need to enclose &curLimOrDed - 1

into %EVAL() function in order to have it evaluated as you expect: %EVAL(&curLimOrDed - 1)

Except of this, the error message actually states the problem:

The condition was: prevLimOrDed 

That's because in your statement:

lowerLimOrDed = %Scan(&limOrDedOption,prevLimOrDed, &curLimOrDed. - 1);

prevLimOrDed is not used as a macro variable, but its used inside macro function. Is it a data step variable? For %SCAN function it represent just text "prevLimOrDed", while the function expects a numeric argument.

0
Bharath T N On

Scan function's third parameter is used for delimiter which has to be a character value. You are doing some calculations in the third parameter.