Removing a certain part of a word

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Removing a certain part of a word

obsPARKSTYRESTABACRES
1ArchesUT197173000
2CanyonlandsUT1964338000
3Capitol ReefUT1971242000
4ZionUT1919147000
5Bryce CanyonUT192436000

Hello. I was told to remove the word "Canyon" in "PARK".

So this is what I did:

DATA UT;

Infile 'C:\SAS\natlpark.txt';
PARK $ 1- 20 ST $ 21-22 YRESTAB 25-28 ACRES COMMA10.;

If st ^= "UT" then delete;

Acres = round(acres,1000);

t = indexw(park, 'Canyon')';

If t then PARK = substr(park,1,t-1);

run;

proc print data=UT;

run;

obsPARKSTYRESTABACRESt
1ArchesUT1971730000
2CanyonlandsUT19643380000
3Capitol ReefUT19712420000
4ZionUT19191470000
5Bryce UT1924360007

As you can see I successfully removed the word Canyon from "Bryce Canyon", but from observation 2 the word "Canyonlands" still carries with it the word "Canyon" at the front. I tried using the split(), scan(), but it gives me weird answers. How can I remove the word Canyon in Canyonlands??


Accepted Solutions
Solution
‎02-26-2014 06:57 AM
Super User
Super User
Posts: 7,392

Re: Removing a certain part of a word

Tranwrd would be one option:

NEW_PARK = tranwrd(upcase(PARK),"CANYON",""));

View solution in original post


All Replies
Respected Advisor
Posts: 3,777

Re: Removing a certain part of a word

indexW searches for words perhaps you want to seach at the string level.  Look for a function that searches in that way.

Solution
‎02-26-2014 06:57 AM
Super User
Super User
Posts: 7,392

Re: Removing a certain part of a word

Tranwrd would be one option:

NEW_PARK = tranwrd(upcase(PARK),"CANYON",""));

Occasional Contributor
Posts: 7

Re: Removing a certain part of a word

Ohhh amazin'! Thanks

Frequent Contributor
Posts: 106

Re: Removing a certain part of a word

or u may try this

park=transtrn(park,'Canyon','');

Occasional Contributor
Posts: 12

Re: Removing a certain part of a word

a complex method

park=prxchange('s/(Canyon)//i',-1,park);

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 228 views
  • 7 likes
  • 5 in conversation