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

Hi,

suppose I have the following table:

company
ABC (delisted in 1/1/2010)
DEF

Is it possible to get the following:

companydelist
ABC (delisted in 1/1/2010)yes
DEFno

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

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

View solution in original post

8 REPLIES 8
Haikuo
Onyx | Level 15

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

Ksharp
Super User

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

ilikesas
Barite | Level 11

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!

Patrick
Opal | Level 21

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;

ilikesas
Barite | Level 11

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

Ksharp
Super User

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

ilikesas
Barite | Level 11

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!

Ksharp
Super User

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.

sas-innovate-2024.png

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.

 

Register now!

How to connect to databases in SAS Viya

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.

Discussion stats
  • 8 replies
  • 1404 views
  • 8 likes
  • 4 in conversation