BookmarkSubscribeRSS Feed
SAS_New_User1
Obsidian | Level 7

I have a string

:20:catclimbedthetree:STOP:dogranaftercat:STOP:catwasinthehouse:-

 

I want to delete everything after the 2nd :START: and also delete :START: i.e. the desired output is as below, what should I do? I used index function and substr but it is truncating all the characters after the first :START:. Please help. 

 

:20:catclimbedthetree:START:dogranaftercat

 

Thank you

 

4 REPLIES 4
Reeza
Super User

You only have one START in your example not two.
Do you mean STOP?
Your description of the problem and example do not align.

 


@SAS_New_User1 wrote:

I have a string

:20:catclimbedthetree:STOP:dogranaftercat:STOP:catwasinthehouse:-

 

I want to delete everything after the 2nd :START: and also delete :START: i.e. the desired output is as below, what should I do? I used index function and substr but it is truncating all the characters after the first :START:. Please help. 

 

:20:catclimbedthetree:START:dogranaftercat

 

Thank you

 


 

SAS_New_User1
Obsidian | Level 7
yes, I meant :STOP:

sorry about that
Reeza
Super User
1. Use FINDW() to find the first STOP.
2. Use FINDW() again to find the second STOP - use the starting position from the first answer as a parameter to the second call to find the start of the second STOP
3. Use SUBSTR() to substring the string to the desired length - from 1 to the answer from step 2.

Documentation for FINDW.
https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lefunctionsref&docsetTarg...

If you're having issues post your code something as follows:

data have;
string = ":20:catclimbedthetree:STOP:dogranaftercat:STOP:catwasinthehouse:-";
firstSTOP = findw( .... ); * you need to fill in the appropriate parameters;
secondStop = findw(string, ...., firstStop); * you need to fill in the appropriate parameters;
want = substr(1, secondSTop);
run;


ballardw
Super User

 

The pattern searches and selections with Substr for the specified positions are very similar to some of your prior questions.

 

What have you tried so far?

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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