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

HI,

 

Below is the input:

BOM_ZEB_MIC_1002214_API_1002214_K304330

BOM_EVR_MIC_11000000002328_API_11000000002328_243

 

And out put required is:

1002214_API_1002214_K304330

11000000002328_API_11000000002328_243

 

I want to remove the first 3 Words as you can see in output.What function to be used for this ?

 

Kindly help

KR

Sanchit Arora

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Use the substr() function with the findc() function and the "d" paramer:

data have;
  length text new_text $2000;
  text="BOM_ZEB_MIC_1002214_API_1002214_K304330"; 
  new_text=substr(text,findc(text,"1","d"));
  output;
  text="BOM_EVR_MIC_11000000002328_API_11000000002328_243"; 
  new_text=substr(text,findc(text,"1","d"));
  output;
run;

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Use the substr() function with the findc() function and the "d" paramer:

data have;
  length text new_text $2000;
  text="BOM_ZEB_MIC_1002214_API_1002214_K304330"; 
  new_text=substr(text,findc(text,"1","d"));
  output;
  text="BOM_EVR_MIC_11000000002328_API_11000000002328_243"; 
  new_text=substr(text,findc(text,"1","d"));
  output;
run;
SanchitArora
Calcite | Level 5

Thank you so much for the solution. It its working

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Do bear in mind that it looks for the first occurence of a number in the string, and then substrings from there.  If your data like the given examples you will be fine, just remember the above.

SanchitArora
Calcite | Level 5

HI,

 

I found  new problem . Sorry i have to mention earlier .

I have a Input like:

BOM_WAR_MIC_R740193 Output coming as :740193

BOM_EVR_MIC_CD5T: Output coming as :5T

 

I need output as:

R740193

 

CD5T:

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, thats why I mentioned it.  Basically the idea is to find a logical pattern in the data (as I don't have the data I can't tell you), then find that data and substring from that point.  Now say that your data is always separated by MIC, then you would do:

substr(text,find(text,"MIC")+4);

This will find the text MIC, then from the start position of MIC + four (so we don't get MIC_ in the output) substring out to the end of the string.  Maybe if its always from the third underscore you could do:

new_string=strip(tranwrd(string,cats(scan(string,1,"_"),"_",scan(string,2,"_"),"_",scan(string,3,"_"),"_"),""));

What the above does is scan out the first three parts as delimited by the underscore and screate a string with the underscores, that text then being removed from the string by use of tranwrd.

Its really up to how your data looks and what the logical delimiter is.

Haikuo
Onyx | Level 15

An alternative would be using Perl Regular Expression:

The following code will replace the first 3 words by nothing, meaning: removing them.

 

new_text=prxchange('s/^([^_]+_){3}//o', -1, text);

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 3693 views
  • 2 likes
  • 3 in conversation