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

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag

View solution in original post

4 REPLIES 4
ballardw
Super User

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.

Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
Ksharp
Super User
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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4114 views
  • 2 likes
  • 4 in conversation