How to exract a word between delimiters?

Accepted Solution Solved
Reply
Contributor
Posts: 71
Accepted Solution

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
Super User
Posts: 6,498

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_Smiley Happy (=/);

run;


stra_no_txt=678

stra_no=678

strb_no_txt=789

strb_no=789

View solution in original post


All Replies
Solution
‎10-25-2012 10:57 PM
Super User
Super User
Posts: 6,498

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_Smiley Happy (=/);

run;


stra_no_txt=678

stra_no=678

strb_no_txt=789

strb_no=789

Contributor
Posts: 71

Re: How to exract a word between delimiters?

It worked. TY!


Respected Advisor
Posts: 3,124

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.

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

Discussion stats
  • 3 replies
  • 260 views
  • 1 like
  • 3 in conversation