- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I'm not sure what you want to achieve with this statement:
variable2 BETWEEN TODAY()-1 AND TODAY()-3;
Please be more specific
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.