gaussblurinc
gaussblurinc

Reputation: 3692

SAS: Expressions in IF statement

does Sas provide mechanism of chain-expressions?

does Sas provide mechanism of In-clause?

Simple examples:

  a = '09MAY2010'd;
  b = '17MAY2010'd;

if (a<=c<=b) then do; /*code*/ end;
if (c in (a:b)) then do; /*code*/ end;

maybe any good techniques of if/where statements?
your suggestions and advises, please.
Thanks!

Upvotes: 1

Views: 298

Answers (2)

S&#248;ren Lassen
S&#248;ren Lassen

Reputation: 61

Apart from the IN operator, which only accepts constant values inside the paranthesis, there is also an (undocumented) IN function, which can be used with variables, so instead of if c in(a,b) you can use if in(c,a,b) which will work also when a and b are variables.

Another possibility is to use WHICHN or WHICHC functions, which has the same syntax, and which return 0 (FALSE) when a match is not found, and otherwise the number of the (first) match.

Upvotes: 0

vasja
vasja

Reputation: 4792

Your example, changed a bit:

data _null_;
    a = '09MAY2010'd;
    b = '17MAY2010'd;
    c = '17MAY2010'd;

    if (a<=c<=b) then do;
        putlog "a<=c<=b";
    end;

    select (c); 
        when (a, b) putlog "in a, b";
        when ('17MAY2010'd) putlog "'17MAY2010'd";/* not used, only first match is executed */
        otherwise;
    end;

run;

IN operator used with IF or in WHERE clause requires constants in the list.

Upvotes: 2

Related Questions