HI,
I am trying to find a way to find character fields that contains some special keywords.
I tried using something like this:
IF TINT='2' and NAME CONTAINS (' TRUST ', ' TR ');
TINT | NAME |
---|---|
1 | JOSEPH BROWN TRUST |
2 | THE TRUSTWORTHDY TRUST CORP |
2 | JOHN ADAMS TR |
I would like to find a way to identify , in this example, the last two observations because they meet the conditions I listed on the IF statement. I do have millions of records, so this is just an example. Any ideas how to make this easy?
Thanks...
How many key "words" are you needing to search for? You aren't going to find a list type function though it may be possible to write one.
FINDW lets you specify only to match words, which are separated by delimiters such as a space so you do not need to try to specify a search string such as ' TR ' with spaces to find the TR at the end of line 3 but not match if in the middle of a word.
if tint='2' then do;
/* multiple individual searches*/
if findw(name,'TRUST')>0 then do; <whatever>; end;
if findw(name,'TR')>0 then do; <whatever>; end;
end;
And from @DBailey:
Others have given data step suggestions...you could also use proc sql (especially if the data are in a rdbms somewhere else)...
proc sql; select * from have where TINT='2' and ( NAME like '% TRUST %' or name like '% TR %'); quit;
IF TINT='2'
and ( INDEXW( NAME,'TRUST')
or INDEXW( NAME,'TR')
)
;
How many key "words" are you needing to search for? You aren't going to find a list type function though it may be possible to write one.
FINDW lets you specify only to match words, which are separated by delimiters such as a space so you do not need to try to specify a search string such as ' TR ' with spaces to find the TR at the end of line 3 but not match if in the middle of a word.
if tint='2' then do;
/* multiple individual searches*/
if findw(name,'TRUST')>0 then do; <whatever>; end;
if findw(name,'TR')>0 then do; <whatever>; end;
end;
And from @DBailey:
Others have given data step suggestions...you could also use proc sql (especially if the data are in a rdbms somewhere else)...
proc sql; select * from have where TINT='2' and ( NAME like '% TRUST %' or name like '% TR %'); quit;
data want(drop=_name keywords);
set have;
_NAME=NAME;
length keywords $20;
do keywords='TRUST', 'TR';
_NAME = tranwrd(_NAME,strip(keywords),'***');
end;
if TINT=2 and findw(_name,'***');
run;
Others have given data step suggestions...you could also use proc sql (especially if the data are in a rdbms somewhere else)...
proc sql;
select * from have where
TINT='2' and
( NAME like '% TRUST %' or name like '% TR %');
quit;
IF TINT='2'
and prxmatch('/TRUST|TR/',NAME)
;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.