DATA Step, Macro, Functions and more

Searching a character string for embedded numbers

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

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
Respected Advisor
Posts: 3,156

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         
;

View solution in original post


All Replies
Solution
‎10-22-2016 03:12 AM
Respected Advisor
Posts: 3,156

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         
;
Respected Advisor
Posts: 4,932

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.

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

Discussion stats
  • 2 replies
  • 225 views
  • 2 likes
  • 3 in conversation