BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PetePatel
Quartz | Level 8

Hi all,

 

I have a large SAS dataset with >100k records and it contains variables such as:

forename, surname, company, email address...etc.

 

I would like to search for a particular word (example 'TSE') in ANY of the variables and keep these records only.

 

How do I go about writing this? I tried some code like the below, but it only returns records for one variable (i.e surname).

 

data want;
set have (where=(prxmatch("/\b(TSE)\b/io", surname)));
run;

 

Any help would be greatly appreciated.

 

Cheers,

Pete

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

If you knew ahead of time that there was a short list of character variables, you could replace SURNAME with an expression that smooshes them all together, such as:

 

catx(' ', surname, forename, company, email)

 

With longer lists of variable names, or with lists that change from data set to data set, you would need to jump through some massive hoops using preprocessing of the data to find the names of all the character variables,.  It would be much easier to switch from WHERE to IF, which would allow the use of the word _CHARACTER_ to represent all character variables.  If you do switch to an IF statement you should be able to replace SURNAME with:

 

catx(' ', of _character_)

 

 

View solution in original post

2 REPLIES 2
Astounding
PROC Star

If you knew ahead of time that there was a short list of character variables, you could replace SURNAME with an expression that smooshes them all together, such as:

 

catx(' ', surname, forename, company, email)

 

With longer lists of variable names, or with lists that change from data set to data set, you would need to jump through some massive hoops using preprocessing of the data to find the names of all the character variables,.  It would be much easier to switch from WHERE to IF, which would allow the use of the word _CHARACTER_ to represent all character variables.  If you do switch to an IF statement you should be able to replace SURNAME with:

 

catx(' ', of _character_)

 

 

SuryaKiran
Meteorite | Level 14

There are many function in SAS to find a string, I prefer INDEX or FIND if I'm searching for a single string and PRXMATCH for multiple strings. 

First you need to find all the character variables in your table and then find the string in each variable.

proc sql;
select "FIND(strip(upcase("||name||")),'AUDI')<>0" INTO: Check_Vars separated by " OR "
	from dictionary.columns
	where libname="SASHELP" and MEMNAME="CARS"
		and type='char';
quit;

data want;
set sashelp.cars(where=(&Check_Vars.));
run;
Thanks,
Suryakiran

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 830 views
  • 1 like
  • 3 in conversation