BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Yughaber
Quartz | Level 8

I have a sentence "Sacramento is the capital of California" and am told to delete the words before the word "capital" and keep the rest. Now, the code has to be written so that if the word "capital" moves around then everything before it will be deleted. For example, "The capital of California is Sacramento" then only "The" in this sentence will be removed, whereas in the previous sentence "Sacramento is the" would have been removed. 

 

Help please! Thanks 🙂 

1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ

Hello,

 

I think you want the variable 'c' in the dataset 'want' :

data have;
a="Sacramento is the capital of California"; output;
a="The capital of California is Sacramento"; output;
run;

data want;
 set have;
 b=indexw(a,'capital');
 c=substr(a,b);
 d=substr(a,b+length('capital'));
run;
/* end of program */

Kind regards,

Koen

View solution in original post

4 REPLIES 4
sbxkoenk
SAS Super FREQ

Hello,

 

I think you want the variable 'c' in the dataset 'want' :

data have;
a="Sacramento is the capital of California"; output;
a="The capital of California is Sacramento"; output;
run;

data want;
 set have;
 b=indexw(a,'capital');
 c=substr(a,b);
 d=substr(a,b+length('capital'));
run;
/* end of program */

Kind regards,

Koen

Yughaber
Quartz | Level 8
Thank you very much!!
Yughaber
Quartz | Level 8
another question regarding this, if I were to not use the index function would I be able to code it any differently?
sbxkoenk
SAS Super FREQ

You can also use the FINDW function.

I would not use the SCAN function (you cannot scan for a whole word).

Koen

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1278 views
  • 1 like
  • 2 in conversation