obs | PARK | ST | YRESTAB | ACRES |
---|---|---|---|---|
1 | Arches | UT | 1971 | 73000 |
2 | Canyonlands | UT | 1964 | 338000 |
3 | Capitol Reef | UT | 1971 | 242000 |
4 | Zion | UT | 1919 | 147000 |
5 | 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 | PARK | ST | YRESTAB | ACRES | t |
---|---|---|---|---|---|
1 | Arches | UT | 1971 | 73000 | 0 |
2 | Canyonlands | UT | 1964 | 338000 | 0 |
3 | Capitol Reef | UT | 1971 | 242000 | 0 |
4 | Zion | UT | 1919 | 147000 | 0 |
5 | 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??
Tranwrd would be one option:
NEW_PARK = tranwrd(upcase(PARK),"CANYON",""));
indexW searches for words perhaps you want to seach at the string level. Look for a function that searches in that way.
Tranwrd would be one option:
NEW_PARK = tranwrd(upcase(PARK),"CANYON",""));
Ohhh amazin'! Thanks
or u may try this
park=transtrn(park,'Canyon','');
a complex method
park=prxchange('s/(Canyon)//i',-1,park);
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.