New SAS User

Completely new to SAS or trying something new with SAS? Post here for help getting started.
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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