Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

Replace remainder of string from shifting starting point

Accepted Solution Solved
Reply
Contributor
Posts: 42
Accepted Solution

Replace remainder of string from shifting starting point

I hope someone can give me an assist here.

I have text data in one variable that varies in length. The field begins with a name and contains the word "ZIP" somewhere in the string. I need to find the occurrence of "ZIP" and remove all text after that occurrence to the end of the string.

Is there anyone who could point me in the right direction?

Thank you very much.

Data Have

John M. Smith ZIP 99999 any town any city

Mary M. Smith any town any city ZIP 88888 some other text

Bill Murray title book ZIP

Data Want:

John M. Smith

Mary M. Smith

Bill Murray


Accepted Solutions
Solution
‎06-19-2015 05:39 PM
Super User
Posts: 11,343

Re: Replace remainder of string from shifting starting point

,(indexw(string,'ZIP')-1

View solution in original post


All Replies
Super User
Posts: 11,343

Re: Replace remainder of string from shifting starting point

data want;

     set have;

     string = substr(string,1,(indexw(string,'ZIP')));

run;

Where String is the name of your variable with the text. Note that the search is case sensitive, if you have values of Zip or zip they won't match.

Contributor
Posts: 42

Re: Replace remainder of string from shifting starting point

This mostly works. However, what I end up with is:

John M. Smith Z

Mary M. Smith Z

Bill Murray Z

Solution
‎06-19-2015 05:39 PM
Super User
Posts: 11,343

Re: Replace remainder of string from shifting starting point

,(indexw(string,'ZIP')-1

Contributor
Posts: 42

Re: Replace remainder of string from shifting starting point

Thank you very, very much. That works perfectly.

Jeff

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 245 views
  • 3 likes
  • 2 in conversation