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

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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
Reeza
Super User

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

 

  want = scan(answer, 2, ":-");
hhchenfx
Barite | Level 11

Thanks a lot!!!

HC

PaigeMiller
Diamond | Level 26

@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

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 9321 views
  • 2 likes
  • 3 in conversation