Hello,
I would like to limit my data set to a specific list of county names. Is there a quicker way than repeated IF statements?
Thanks!
When you want to compare a single variable to a list of values then the IN operator is your tool of choice:
countyname in ('Thisname' 'Thatname' 'Another name');
Subsetting the data could be done with either a Where statement or dataset option or an IF statement:
Data want; set have; where countyname not in ('Thisname' 'Thatname' 'Another name'); run; or Data want; set have; if countyname not in ('Thisname' 'Thatname' 'Another name'); run;
If the list you want to keep is shorter then don't use the NOT in the above examples.
The IN operator for character values is equivalent to a bunch of equals, so if you have case differences such as your values with "Name" "name" "nAme" etc, for spelling you will need to address that somehow.
Yes. Please specify your problem more carefully and you'll have a usable code answer in no time.
So, I'm working with a data set of about 1M records. A variable in the set specifies the county name. I would like to keep only the records in which the county name belongs to a specific list of 39 counties and delete the rest.
I usually use IF statements, IF county_name="so and so" THEN delete. But since there is a long list of count names I wish to delete I was hoping there would be a shorter way.
Thank you!
When you want to compare a single variable to a list of values then the IN operator is your tool of choice:
countyname in ('Thisname' 'Thatname' 'Another name');
Subsetting the data could be done with either a Where statement or dataset option or an IF statement:
Data want; set have; where countyname not in ('Thisname' 'Thatname' 'Another name'); run; or Data want; set have; if countyname not in ('Thisname' 'Thatname' 'Another name'); run;
If the list you want to keep is shorter then don't use the NOT in the above examples.
The IN operator for character values is equivalent to a bunch of equals, so if you have case differences such as your values with "Name" "name" "nAme" etc, for spelling you will need to address that somehow.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.