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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15
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

2 REPLIES 2
Haikuo
Onyx | Level 15
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         
;
PGStats
Opal | Level 21

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 1032 views
  • 2 likes
  • 3 in conversation