What to do IN BETWEEN dates

1

I saw a suggestion arrive from a SAS customer who would like to see the IN operator extended to allow ranges of date values. For example, you can currently write a program that checks for values IN a collection or range of numbers:

data check;
  if x in (1:10) then result="match";
run;

This matches on the set of numbers 1 through 10, inclusive. The customer would like to see something like this supported, to match on the dates that fall within a given range.

data check;
   if x in ('01JAN2010'd : '01FEB2010'd) then result="match";
run;

It's a great suggestion. But in the meantime, you can satisfy this simple example by using the shorthand for the AND operator:

data check;
  if '01JAN2010'd <= x <='01FEB2010'd then result="match";
run;

There are many other ways to "skin this cat", including using PROC SQL and the BETWEEN condition. What's your favorite?

Share

About Author

Chris Hemedinger

Director, SAS User Engagement

+Chris Hemedinger is the Director of SAS User Engagement, which includes our SAS Communities and SAS User Groups. Since 1993, Chris has worked for SAS as an author, a software developer, an R&D manager and a consultant. Inexplicably, Chris is still coasting on the limited fame he earned as an author of SAS For Dummies

1 Comment

  1. I assume from the context that x is supposed to be integer-valued. If so, your workaround is fine. However, for non-integer values of x, the two approaches give different results:

    data check;
    input x @@;
    if x in (1:10) then InResult="match"; else InResult="NOT";
    datalines;
    1 1.5 5 7.5 10 11
    ;
    run;
    proc print; run;

    To robustly mimic the IN operator with the "less than" code, you should check that x is an integer as part of the IF condition:

    if (int(x)=x) & (less-than-comparison-code) then...

Back to Top