SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Creating variable conditional on string part

Accepted Solution Solved
Reply
Super Contributor
Posts: 413
Accepted Solution

Creating variable conditional on string part

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


Accepted Solutions
Solution
‎01-01-2015 08:03 PM
Respected Advisor
Posts: 3,890

Re: Creating variable conditional on string part

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


All Replies
Respected Advisor
Posts: 3,124

Re: Creating variable conditional on string part

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

Super User
Posts: 9,676

Re: Creating variable conditional on string part

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

Super Contributor
Posts: 413

Re: Creating variable conditional on string part

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!

Solution
‎01-01-2015 08:03 PM
Respected Advisor
Posts: 3,890

Re: Creating variable conditional on string part

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;

Super Contributor
Posts: 413

Re: Creating variable conditional on string part

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

Super User
Posts: 9,676

Re: Creating variable conditional on string part

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

Super Contributor
Posts: 413

Re: Creating variable conditional on string part

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!

Super User
Posts: 9,676

Re: Creating variable conditional on string part

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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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