Hi,
suppose I have the following table:
company |
---|
ABC (delisted in 1/1/2010) |
DEF |
Is it possible to get the following:
company | delist |
---|---|
ABC (delisted in 1/1/2010) | yes |
DEF | no |
That is, if in the company's name the word "delisted" is present, I would like to have a new column that says "yes", and if the company wan't delisted, then "no" will appear
Thank you
I love regular expressions but in your case I would use a simple findw() function.
With a RegEx I would use the 'i' switch so the search pattern is not case sensitive and I would use the \b metacharacter to only find "delisted" if it's a word on it's own.
data have;
input x & $40.;
if findw(x,'delisted','( ','i') then found1=1; else found1=0;
if prxmatch('/\bdelisted\b/oi',x) then found2=1;else found2=0;
cards;
ABC (delisted in 1/1/2010)
DEF
CompABC (public 2007)
;
run;
Please read Docs on FIND(), FINDW(), INDEX, INDEXW(), COUNT() or you want something spicier, PRXMATCH().
If you use Proc SQL, you can also explore CONTAINS or LIKE clause.
Good luck,
Haikuo
data have;
input x & $40.;
if prxmatch('/\(.*\)/',x) then found=1;else found=0;
cards;
ABC (delisted in 1/1/2010)
DEF
;
run;
Xia Keshan
Hi Xia Keshan,
Thank you for the code, I did it and it worked.
I was just wandering, in your code the prxmatch is looking for parentheses, but if I do have for some company parentheses like this:
CompABC (public 2007)
Then found will be 1 for this as well, so what I did is the following:
if prxmatch("/delisted /",company) then found=1;else found=0;
I ran the code on a small sample and it seems to be working.
Thank you and a happy new year!
I love regular expressions but in your case I would use a simple findw() function.
With a RegEx I would use the 'i' switch so the search pattern is not case sensitive and I would use the \b metacharacter to only find "delisted" if it's a word on it's own.
data have;
input x & $40.;
if findw(x,'delisted','( ','i') then found1=1; else found1=0;
if prxmatch('/\bdelisted\b/oi',x) then found2=1;else found2=0;
cards;
ABC (delisted in 1/1/2010)
DEF
CompABC (public 2007)
;
run;
Hi Patrick,
thank you for your code, I ran it and obtained what I wanted.
I would like to ask you something else if I may:
If a given company was delisted, how to create another column which will contain the date when the company was delisted?
Thank you
Here is :
data have; input x & $40.; if prxmatch('/\bdelisted\b/oi',x) then do; found2=1; date=prxchange('s/.*(\d+\/\d+\/\d+).*/$1/oi',-1,x); end; else found2=0; cards; ABC (delisted in 1/1/2010) DEF CompABC (public 2007) ; run;
Xia Keshan
Hi Xia Keshan,
Thank you for your code!!!
What I especially find interesting is that it will give me the date even if I have something like this:
ABC (delisted in 1/1/2010 33.AA)
I guess that this is because the prxchange function, with the arguments you gave it, is searching for the first from the left string element that is in the date form as specified by the argument.
Thank you!
That is a big advantage of Perl Regular Expression than the SAS function. I would prefer to use PRX firstly , then after that considering about SAS function.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.