SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
stogez13
Calcite | Level 5

Hi,

 

I'm trying to filter some data based on a variable.

 

The code I think should be something like this:

DATA need;

SET have;

IF variable1=4 THEN DO; variable2 BETWEEN TODAY()-1 AND TODAY()-3;

RUN;

but I can't use the BETWEEN in the IF THEN statement

 

 

Any suggestions?

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

I'm not sure what you want to achieve with this statement: 

variable2 BETWEEN TODAY()-1 AND TODAY()-3;

Please be more specific 

Astounding
PROC Star

If your intent is just to filter the observations, you could use:

 

if variable1=4 and (today()-3 <= variable2 <= today()-1);

If you need to accomplish something different, you would have to explain what it is.

Tom
Super User Tom
Super User

Note that you do not have an IF/THEN statement.  Your statement is close to a subsetting IF statement (if you replaced the spurious DO; in the middle of it with an AND operator).  If your intent was a subsetting IF and the variables you are referencing are coming directly from the input dataset (that is you did not modify them with other data step code before the IF statement) then you can replace the subsetting IF with a WHERE and SAS will filter the data before it gets to the data step.

 

You cannot use BETWEEN in an IF/THEN statement.  You can use it in a WHERE statement.  BETWEEN is an SQL construct, not a SAS operator, but the WHERE statement uses essentially the SQL engine.

1     data test;
2      set sashelp.class ;
3      where age between 12 and 13 ;
4     run;

NOTE: There were 8 observations read from the data set SASHELP.CLASS.
      WHERE (age>=12 and age<=13);
NOTE: The data set WORK.TEST has 8 observations and 5 variables.

 

If you do need to use an IF statement then just use normal range test instead of BETWEEN.

5     data test;
6      set sashelp.class ;
7      if 12 <= age <= 13 ;
8     run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TEST has 8 observations and 5 variables.

 Also notice that the values you are using in your BETWEEN clause are not in ascending order.  SAS is smart enough to notice that and fix it, at least for constants, but other software's implementation of SQL might require that the values be in the proper order.

9     data test;
10     set sashelp.class ;
11     where age between 13 and 12 ;
12    run;

NOTE: There were 8 observations read from the data set SASHELP.CLASS.
      WHERE (age>=12 and age<=13);
NOTE: The data set WORK.TEST has 8 observations and 5 variables.
ballardw
Super User

@stogez13 wrote:

Hi,

 

I'm trying to filter some data based on a variable.

 

The code I think should be something like this:

DATA need;

SET have;

IF variable1=4 THEN DO; variable2 BETWEEN TODAY()-1 AND TODAY()-3;

RUN;

but I can't use the BETWEEN in the IF THEN statement

 

 

Any suggestions?


When your code generates errors, such as this would, you should copy the code and the error messages from the LOG and paste into a code box to preserve the formatting of the error messages.

Your code generates an error message similar to:

                        ------
                        180
ERROR 180-322: Statement is not valid or it is used out of proper order.

with the underscores appearing underneath your VARIABLE2. That tells you that "between" actually wasn't the issue but that a statement involving a variable name is not valid in the position of the statement.

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1897 views
  • 0 likes
  • 5 in conversation