LIKE Operator In SAS
The LIKE operator selects observations by comparing the values of a character variable to a specified pattern, which is referred to as pattern matching. The LIKE operator is case sensitive. There are two special characters available for specifying a pattern:
1. percent sign (%)
It specifies that any number of characters can occupy that position. We have already demonstrated how you can use the LIKE operator with the percent sign(%).
You can select the car model which has “auto” text on “model” variable by using the following code.
/* like operator in SAS*/
data selected_cars;
set cars;
where strip(upcase(model)) like '%AUTO%';
proc print;
run;
2. underscore (_)
It matches just one character in the value for each underscore character. You can specify more than one consecutive underscore character in a pattern, and you can specify a percent sign and an underscore in the same pattern.
For example, you can use different forms of the LIKE operator to select character values from this list of first names:
- Diana
- Diane
- Dianna
- Dianthus
- Dyan
The following table shows which of these names is selected by using various forms of the LIKE operator:
- Pattern : Name Selected
- like ‘D_an’ : Dyan
- like ‘D_an_’ : Diana, Diane
- like ‘D_an__’ : Diannalike ‘D_an%’ : all names from list
The following sample dataset will be used to demonstrate different use cases of using LIKE operator in SAS.
/* create sample dataset */
data cars;
keep Make Model Type Origin DriveTrain MSRP Invoice;
set sashelp.cars;
run;
/* view dataset */
proc print data=cars;
run;
1. Selecting Values That Begins With Char String
In this example data rows are selected based on the model description that begins with the character “A4” from cars dataset. The STRIP function to suppress the blank spaces from the string data.
The STRIP function returns the argument with all leading and trailing blanks removed. It’s always good to use and make sure you don’t miss any rows when you try to filter based on words or character.
/* Selecting Values That Begins With char string */
data selected_cars;
set cars;
where strip(model) like 'A4%';
run;
/* view dataset */
proc print data=selected_cars;
run;
2. Selecting Values That Ends With Char String
In this example data rows are selected based on the model description that ends with the character “2dr” from cars dataset. The STRIP function is also used here to suppress the blank spaces from the string data.
/* Selecting Values That Ends With char string */
data selected_cars;
set cars;
where strip(model) like '%2dr';
run;
/* view dataset */
proc print data=selected_cars;
run;
3. Selecting Values With Multiple Like Conditions
You can use multiple conditions separated by operators such as AND, OR, NOT, etc. The below example shows how to filter data based on the “model” description that ends with the char string “2dr” and also has the character “convertible” .
/* Selecting Values With Multiple Like Conditions */
data selected_cars;
set cars;
where strip(model) like '%2dr' and strip(model) like '%convertible%';
run;
/* view dataset */
proc print data=selected_cars;
run;
Did you know? How To Use WHERE statement In SAS
4. Selecting Values With Underscore (_)
When you use underscore. It means it’s free space where any char, number, or special char can be filled in that particular position.
In the below example “make” variable data is being compared against a pattern that must start with “M”, the second character can be any anything but third character must be “r”. And the rest of the string can be anything if it is there.
/* Selecting Values With Underscore (_) */
data selected_cars;
set cars;
where strip(make) like 'M_r%';
run;
/* view dataset */
proc print data=selected_cars;
run;
5. Selecting Values With UPCASE function
You can use the LIKE operator to compare char string with UPCASE function. It’s important to convert model description into uppercase and compare with the uppercase character string.
In the below example rows are selected based on “model” description which is compared against text “AUTO” .
/* Selecting Values With UPCASE function */
data selected_cars;
set cars;
where upcase(model) like '%AUTO%';
run;
/* view dataset */
proc print data=selected_cars;
run;
6. Selecting Values With Negative Like Condition
You can use the LIKE operator along with the NOT operator to make the condition negative.
You can use the NOT operator just before the set of conditions enclosed in the bracket. Let’s try to use the previous example but with the negative condition.
/* Selecting Values With Negative Like Condition */
data selected_cars;
set cars;
where NOT (upcase(model) like '%AUTO%');
run;
/* view dataset */
proc print data=selected_cars;
run;
7. Selecting Values With NOT LIKE Condition
This method is very similar to the previous example but with the change of NOT operator placement. In the previous example you have mentioned NOT at the beginning of your condition which makes the entire condition negative.
In this method you can individually set the negative condition by using the NOT LIKE operator.
/* Selecting Values With NOT LIKE Condition */
data selected_cars;
set cars;
where upcase(model) not like '%AUTO%';
run;
/* view dataset */
proc print data=selected_cars;
run;
8. Contains Operator In SAS
The CONTAINS operator functions similar to the LIKE operator where you add percent sign ($) at the beginning and end of the character string.
The below two conditions work exactly similar and produce the similar result.
- where upcase(model) like ‘%AUTO%’;
- where upcase(model) contains ‘AUTO’;
Here is the example that shows how you can use CONTAINS instead of LIKE operator.
/* contains operator in sas */
data selected_cars;
set cars;
where upcase(model) contains 'AUTO';
run;
/* view dataset */
proc print data=selected_cars;
run;
FAQ – How To Use LIKE Operator In SAS
The LIKE operator in SAS is used in the WHERE statement to match a specified pattern within a column.
The LIKE operator is typically used in a WHERE statement in SAS to filter rows based on pattern matching in a specific column.
Yes, the LIKE operator in SAS can use wildcard characters. The ‘%’ symbol represents zero or more characters, and the ‘_’ symbol represents a single character.
No, the LIKE operator in SAS can only be used with character variables. For numeric variables, you would use the standard comparison operators (e.g., =, <, >).
The ‘=’ operator in SAS checks for exact matches, while the LIKE operator checks for a pattern match within a column.