## Removing a certain part of a word

Solved
Occasional Contributor
Posts: 7

# Removing a certain part of a word

obs 1 2 3 PARK ST YRESTAB ACRES Arches UT 1971 73000 Canyonlands UT 1964 338000 Capitol Reef UT 1971 242000 Zion UT 1919 147000 Bryce Canyon UT 1924 36000

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;

obs 1 2 3 4 PARK ST YRESTAB ACRES t Arches UT 1971 73000 0 Canyonlands UT 1964 338000 0 Capitol Reef UT 1971 242000 0 Zion UT 1919 147000 0 Bryce UT 1924 36000 7

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
Posts: 9,599

## Re: Removing a certain part of a word

Tranwrd would be one option:

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

All Replies
Respected Advisor
Posts: 3,852

## 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
Posts: 9,599

## 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 and locked.

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

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