- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I have a string as below
4527488//ahdjs}hdu:34ahdgj:60A:283748 8q79979A B B C Dajhed}{-5 jkqedhkkh36929 82939 A D.
I want to remove everything after " }{-5" , including "}{-5" i.e. desired output is as below:
4527488//ahdjs}hdu:34ahdgj:60A:283748 8q79979A B B C Dajhed
there can be a } or { or - or 5 before or after the above combination but never in the combined "}{-5" combination format. I want to remove everything after this "}{-5" combination, including the combination.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Please try the below code
data have;
input string &:$200.;
datalines4;
4527488//ahdjs}hdu:34ahdgj:60A:283748 8q79979A B B C Dajhed}{-5 jkqedhkkh36929 82939 A D.
;;;;
data want;
set have;
pos=index(string,'}{');
string2=substr(string,1,pos-1);
run;
Jag
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
One way
data example; x="4527488//ahdjs}hdu:34ahdgj:60A:283748 8q79979A B B C Dajhed}{-5 jkqedhkkh36929 82939 A D."; newx = substr(x,1,index(x,"}{-5") -1); run;
The Index function returns the character position where the target string starts. So you want to end one position prior to that, so there is a -1. Substr selects characters in positions between the two position number including the ends.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Please try the below code
data have;
input string &:$200.;
datalines4;
4527488//ahdjs}hdu:34ahdgj:60A:283748 8q79979A B B C Dajhed}{-5 jkqedhkkh36929 82939 A D.
;;;;
data want;
set have;
pos=index(string,'}{');
string2=substr(string,1,pos-1);
run;
Jag
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
please try the perl regular expression
data want;
string="4527488//ahdjs}hdu:34ahdgj:60A:283748 8q79979A B B C Dajhed}{-5 jkqedhkkh36929 82939 A D.";
string2=prxchange('s/(.*)(\}{.*)/$1/oi',-1,string);
run;
Jag
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data want; string="4527488//ahdjs}hdu:34ahdgj:60A:283748 8q79979A B B C Dajhed}{-5 jkqedhkkh36929 82939 A D."; string2=prxchange('s/(}{.*)//oi',-1,strip(string)); run;