Extract text between 2 text (":" and "-")

Accepted Solution Solved
Reply
Super Contributor
Posts: 503
Accepted Solution

Extract text between 2 text (":" and "-")

[ Edited ]

Hi Everyone,

 

I want to extract text in between 2 character ":" and "-" 

as below:

For "82 : B2310 - I&g9ST LOE, - Project /Task Lead-"  the new variable will be B2310 

The could be multiple "-" but only the first "-" matter.

 

The output file should be below:

B2310
B2310
B2330
B2350

 

Thank you,

 

HC

 

data want;
    set test;
    where_colon=find(answer,':');
    where_hyphen=find(answer,'-');

    desired_output1= strip(substr(answer,where_colon+1,where_colon+4));
    desired_output2= strip(substr(answer,where_colon+1,where_hyphen-where_colon-1));
run;

 

DATA test;
  INFILE DATALINES DELIMITER=',' DSD;
  INPUT answer & $50.;
  DATALINES;
"82 : B2310 - I&g9ST LOE, Project /Task Lead-"
"83 : B2310 - I&fgST LOE, Quality /IV&V Manager-"
"84 : B2330 - I&fgST LOE,  Expert 2-"
"85 : B2350 - I&ST LOE,  Expert 3-"
;
  RUN;

 


Accepted Solutions
Solution
‎11-21-2017 05:56 PM
Respected Advisor
Posts: 2,825

Re: Extract text between 2 text (":" and "-")

UNTESTED CODE

data want;
    set test;
    where_colon=find(answer,':');
    where_hyphen=find(answer,'-');
    desired_output= strip(substr(answer,where_colon+1,where_hypen-where_colon));
run;
--
Paige Miller

View solution in original post


All Replies
Solution
‎11-21-2017 05:56 PM
Respected Advisor
Posts: 2,825

Re: Extract text between 2 text (":" and "-")

UNTESTED CODE

data want;
    set test;
    where_colon=find(answer,':');
    where_hyphen=find(answer,'-');
    desired_output= strip(substr(answer,where_colon+1,where_hypen-where_colon));
run;
--
Paige Miller
Super User
Posts: 23,296

Re: Extract text between 2 text (":" and "-")

Use the SCAN() function and the third parameter allows you to set multiple custom delimiters.

 

  want = scan(answer, 2, ":-");
Super Contributor
Posts: 503

Re: Extract text between 2 text (":" and "-")

Thanks a lot!!!

HC

Respected Advisor
Posts: 2,825

Re: Extract text between 2 text (":" and "-")


Reeza wrote:

Use the SCAN() function and the third parameter allows you to set multiple custom delimiters.

 

  want = scan(answer, 2, ":-");

I like it. Easier than my solution. 

--
Paige Miller
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 342 views
  • 1 like
  • 3 in conversation