Is there a way to use the IN statement w/ a continuous variable?
With categorical variables, I often subset using the WHERE and IN statements:
Example: WHERE race IN(2,3,4);
In this scenario, the variable "race" is categorical/nominal.
Is there a way to do this with a continuous variable, like age? Age: 0-59
Can you input the values you desire within the parentheses?
Example: WHERE age IN(0:18); or WHERE age IN(0:<19)
Or can you use a format?
Example: WHERE age IN(" ≤ 18 YOA");
Format
Value
age_fmt
0 - 18 = " ≤ 18 YOA"
19 - 35 = "19 to 25 YOA"
36 to High = "> 35";
Is your variable truly continuous or a range of INTEGER values?
If your values are integers you can use in (0:18) .
If you compare a numeric variable to a formatted value you better apply the format to the variable such as
if put(age,age_fmt.) in ('<the formatted value>')
otherwise you are comparing a number to text and SAS will attempt to turn one of them into the proper type for the comparison and you will get 1) conversion notes in the log and very likely 2) missing values as they aren't compatible.
Can you input the values you desire within the parentheses?
Example: WHERE age IN(0:18); or WHERE age IN(0:<19)
The answer is very simple. Try it and see. (Maxim 4)
Is there a way to do this with a continuous variable, like age? Age: 0-59
More generally, you can use
where age<=59;
or if there is a lower limit
where 12 <= age <=59;
Is your variable truly continuous or a range of INTEGER values?
If your values are integers you can use in (0:18) .
If you compare a numeric variable to a formatted value you better apply the format to the variable such as
if put(age,age_fmt.) in ('<the formatted value>')
otherwise you are comparing a number to text and SAS will attempt to turn one of them into the proper type for the comparison and you will get 1) conversion notes in the log and very likely 2) missing values as they aren't compatible.
Good point about a numeric variable.
I'm a little confused by "if put(age,age_fmt.)..."
Will the "in ('<the formatted value>')" work for a numeric variable, independent of the "if put(age,age_fmt.)"?
Example: WHERE age IN(" ≤ 18 YOA");
Assuming age is numeric (e.g., 21.3 years) and the format " ≤ 18 YOA".
No. If you are using a formatted value comparison you have to apply the format using the PUT function. If you use just a value comparison, you use the actual numeric values stored.
You cannot compare a NUMBER, the actual value of the variable AGE, with a STRING, the text generated by the format attached to the variable AGE.
But you don't have to use PUT(). If the format is attached to the variable you can use VVALUE() instead. But then you cannot use a WHERE statement. So use IF instead.
1 data test; 2 set sashelp.class; 3 where age = 13; 4 run; NOTE: There were 3 observations read from the data set SASHELP.CLASS. WHERE age=13; NOTE: The data set WORK.TEST has 3 observations and 5 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 5 data test; 6 set sashelp.class; 7 if left(vvalue(age)) = '13'; 8 run; NOTE: There were 19 observations read from the data set SASHELP.CLASS. NOTE: The data set WORK.TEST has 3 observations and 5 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.01 seconds
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.