Desktop productivity for business analysts and programmers

Remove First 3 Words

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Remove First 3 Words

[ Edited ]

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


Accepted Solutions
Solution
‎10-02-2015 04:52 AM
Super User
Super User
Posts: 7,720

Re: Remove First 3 Words

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


All Replies
Solution
‎10-02-2015 04:52 AM
Super User
Super User
Posts: 7,720

Re: Remove First 3 Words

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;
Occasional Contributor
Posts: 15

Re: Remove First 3 Words

Thank you so much for the solution. It its working

Super User
Super User
Posts: 7,720

Re: Remove First 3 Words

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.

Occasional Contributor
Posts: 15

Re: Remove First 3 Words

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:

 

Super User
Super User
Posts: 7,720

Re: Remove First 3 Words

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.

Respected Advisor
Posts: 3,156

Re: Remove First 3 Words

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);
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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