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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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