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

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
Rhodochrosite | Level 12

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 10492 views
  • 2 likes
  • 3 in conversation