I am new to SAS and I know this is a simple question but any help is appreciated.
I have a dataset and I want to filter it based on specific column.
So the code I have is as follow:
data filtered; set have; where col1 in ("x", "y", "z"); run;
This way of filtering is logical as long as there is not a large number of parameters to filter on. But lets say instead of x , y, and z I have a list of 20 different parameters. For example, x, y, z, a, b, ...., n. I think the easier way is that I create a table such as bellow:
data selected_par; input parameter_name $1. ; datalines; x y z n ;
and then I filter my data set based on this table.
But I don't know how to filter my dataset using this selected_par SAS table.
Any help is appreciated.
Hi Al_senior,
You can do this:
proc sql;
create table filtered as
select * from have
where col1 in (select parameter_name from selected_par);
quit;
Hope this helps.
A concrete example of how you expect things to work might help.
You could use Proc SQL and a join. Something like this might work:
Proc sql; create table filtered as select a.* from have as a inner join selected_par as b on a.col1 = b.parameter_name ; quit;
Hi Al_senior,
You can do this:
proc sql;
create table filtered as
select * from have
where col1 in (select parameter_name from selected_par);
quit;
Hope this helps.
Thank you so much for the solution. I know that might be a bit crazy but would please have a look at my latest question and tell me if what I want to do is even feasible?
Another approach is to create and use a format
proc format;
value $myfilter
'x'='1'
'y'='1'
'z'='1'
'n'='1'
other='0'
;
run;
data have;
do col1='a','x','b','z';
output;
end;
run;
data filtered;
set have;
if put(col1,$myfilter.) ne '1';
run;
Format can also get created dynamically from tables and one could also use hash table lookups for filtering data based on matches to another table. But these are both things for a bit later in your SAS journey.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.