Scan capitalized words at the end of a string

Accepted Solution Solved
Reply
Regular Contributor
Posts: 161
Accepted Solution

Scan capitalized words at the end of a string

Dear All:

I have  RegEx question.  I have a variable with observations like below.  Notice that the last word of the string is capitalized usually.  I'm wondering how to use character function in SAS to identify and delete the last words with all letters capitalized?   Thank you -

HAVE

Farmers Capital Bank Corporation FFKT

Fidelity D D Bancorp Inc FDBC

Fidelity Federal Bancorp FDLB

Ivanhoe Mines Ltd IVN

KaloBios Pharmaceuticals Inc NasdaqGM KBIO

Kindred Healthcare Inc KND

WANT

Farmers Capital Bank Corporation

Fidelity D D Bancorp Inc

Fidelity Federal Bancorp

Ivanhoe Mines Ltd

KaloBios Pharmaceuticals Inc NasdaqGM

Kindred Healthcare Inc

Interactive Brand Development Inc


Accepted Solutions
Solution
‎12-14-2013 05:45 PM
Respected Advisor
Posts: 4,925

Re: Scan capitalized words at the end of a string

Posted in reply to caveman529

PRXCHANGE is the tool.

 

data want;

length newStr $200;

set have;

newStr = prxchange("s/[A-Z]{3,}$//", 1, trim(str));

run;

PG

PG

View solution in original post


All Replies
Solution
‎12-14-2013 05:45 PM
Respected Advisor
Posts: 4,925

Re: Scan capitalized words at the end of a string

Posted in reply to caveman529

PRXCHANGE is the tool.

 

data want;

length newStr $200;

set have;

newStr = prxchange("s/[A-Z]{3,}$//", 1, trim(str));

run;

PG

PG
Regular Contributor
Posts: 161

Re: Scan capitalized words at the end of a string

Thank you so much for your kind help. Smiley Happy

Super Contributor
Super Contributor
Posts: 444

Re: Scan capitalized words at the end of a string

any good tutorial on prxchange,PGStats?

Respected Advisor
Posts: 4,925

Re: Scan capitalized words at the end of a string

I learned mostly by trial and error from the skimpy SAS doc and this forum. But lookup "regular expressions" and Regex on Google for lots of material. Most of it is roughly pertinent since it is based on POSIX standards - PG

PG
Super Contributor
Posts: 276

Re: Scan capitalized words at the end of a string

Posted in reply to caveman529

HI @caveman529,

As PGStats states, PRXCHANGE is the best way to deal with matching stings.i always recomend that method.

But if you are not sure about the Perl Expressions,here is a way to achieve with datastep functions.

Data Test;

input String $50.;

cards;

Farmers Capital Bank Corporation FFkt

Fidelity D D Bancorp Inc FDBC

Fidelity Federal Bancorp FDLB

Ivanhoe Mines Ltd IVN

KaloBios Pharmaceuticals Inc NasdaqGM KBIO

Kindred Healthcare Inc KND

;

run;

Data Have(drop=n l );

set Test;

n=countw(String);

l=length(String);

If scan(String,n)=upcase(scan(String,n)) then

New_=substr(String,1,l-length(scan(String,n)));

else new_=string;

run;

Proc print;

run;

Thanks,

Sanjeev.K

Regular Contributor
Posts: 161

Re: Scan capitalized words at the end of a string

Posted in reply to kuridisanjeev

Great!  Thank you, Sanjeev!

I'll give it a try and do a comparison.  Smiley Happy

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 320 views
  • 3 likes
  • 4 in conversation