## Searching a character string for embedded numbers

Solved
Occasional Contributor
Posts: 12

# Searching a character string for embedded numbers

Hi,

I have a string variable (var) that contains a sequence of characters or numbers. I wish to assign to another to variable (seq) a sequence of numbers that are embedded in this number, usually between two non-numeric delimiters (e.g., '/' or '-'). For example,  var=14025/1258/669. I would like save to seq the charater 1258. The problem that I have is the first set of numbers vary in length as does the seq I want to save.

Here is a theoretical database;

var                                              seq (I want to save)

---                                               ------------------------

14025/1258/669                         1258

145890/0048/4458                     0048

1212/1-1515                               1

14589/49/1247                           49

124/12458/1                               12458

4545-00001-999                         00001

45*784CH21                               784

I think SAS string functions may help (e.g., find, index). If anyone can solve I'd appreciate as always.

Thanks.

Using 9.4, 64-bit under Windows 7

--Tim

Accepted Solutions
Solution
‎10-22-2016 03:12 AM
Posts: 3,167

## Re: Searching a character string for embedded numbers

``````data test;
length var \$ 50;
input var \$;
seq=scan(var,2,'0123456789','k');
cards;
14025/1258/669
145890/0048/4458
1212/1-1515
14589/49/1247
124/12458/1
4545-00001-999
45*784CH21
;``````

All Replies
Solution
‎10-22-2016 03:12 AM
Posts: 3,167

## Re: Searching a character string for embedded numbers

``````data test;
length var \$ 50;
input var \$;
seq=scan(var,2,'0123456789','k');
cards;
14025/1258/669
145890/0048/4458
1212/1-1515
14589/49/1247
124/12458/1
4545-00001-999
45*784CH21
;``````
Posts: 5,540

## Re: Searching a character string for embedded numbers

Regular expressions pattern matching is well suited for this kind of task:

``````data have;
input var :\$32. wantSeq :\$12.;
datalines;
14025/1258/669 1258
145890/0048/4458 0048
1212/1-1515 1
14589/49/1247 49
124/12458/1 12458
4545-00001-999 00001
45*784CH21 784
;

data want;
set have;
length seq \$12;
seq = prxchange("s#\D*\d+[^0-9]+(\d+)[^0-9]+.*#\1#o", 1, var);
run;

proc print; run;``````
PG
☑ This topic is solved.