## How to exract a word between delimiters?

Solved
Frequent Contributor
Posts: 82

# How to exract a word between delimiters?

Hello,

Can someone show me how to extract a word beween delimiters, in this case ()? I have listed my code and desired output below. Thanks in advance for your help!

Data a;

stra = 'no of records found in cv100112 file is (678) records';

strb = 'no of missing records in ab101012 file is  (789) records';

Run;

desired output

Stra_no = 678;

strb_no = 789

Accepted Solutions
Solution
‎10-25-2012 10:57 PM
Super User
Posts: 8,115

## Re: How to exract a word between delimiters?

Use the SCAN() function.

data a;

stra = 'no of records found in cv100112 file is (678) records';

strb = 'no of missing records in ab101012 file is  (789) records';

stra_no_txt = scan(stra,2,'()');

strb_no_txt = scan(strb,2,'()');

stra_no = input(stra_no_txt,10.);

strb_no = input(strb_no_txt,10.);

put (stra_: strb_ (=/);

run;

stra_no_txt=678

stra_no=678

strb_no_txt=789

strb_no=789

All Replies
Solution
‎10-25-2012 10:57 PM
Super User
Posts: 8,115

## Re: How to exract a word between delimiters?

Use the SCAN() function.

data a;

stra = 'no of records found in cv100112 file is (678) records';

strb = 'no of missing records in ab101012 file is  (789) records';

stra_no_txt = scan(stra,2,'()');

strb_no_txt = scan(strb,2,'()');

stra_no = input(stra_no_txt,10.);

strb_no = input(strb_no_txt,10.);

put (stra_: strb_ (=/);

run;

stra_no_txt=678

stra_no=678

strb_no_txt=789

strb_no=789

Frequent Contributor
Posts: 82

It worked. TY!

Posts: 3,167

## Re: How to exract a word between delimiters?

if anyone can make the best out of SAS base, that would be Tom. Here is a standard regular expression approach, not as nearly fancy:

Data a;

input str \$char80.;

if _n_=1 then pattern=prxparse("/\(\d+\)/");

retain pattern;

call prxsubstr(pattern,str,start,len);

digits=substr(str,start+1,len-2);

put digits=;

cards;

'no of records found in cv100112 file is (678) records'

'no of missing records in ab101012 file is (789) records'

;

Haikuo

🔒 This topic is solved and locked.