BookmarkSubscribeRSS Feed
CamRutherford
Fluorite | Level 6

Hello,

 

Looking for some assistance...

 

I have a dataset with a column containing various member_id's (see below example). I would like the first character, everything left of the dash, to be moved to the end of the string prefixed with two 0's and the dash removed. For example, using the below example, looking at the first member id, I'd like 5-12344 to become 12344005.

 

COLUMN A

5-12344

5-1234

5-123456

7-345

2-3456

 

Thanks

6 REPLIES 6
Reeza
Super User

Use the SCAN() function to separate the components. 

 

 

ballardw
Super User

cats(scan(a,2,'-'),'00',scan(a,1,'-'))

PGStats
Opal | Level 21

Another option - regular expression substitution :

 

data have;
input id :$12.;
datalines;
5-12344
5-1234
5-123456
7-345
2-3456
;

proc sql;
select id, prxchange("s/(\d+)\-(\d+)/\200\1/o", 1, id) as newId length=15
from have;
quit;
PG
CamRutherford
Fluorite | Level 6

Thanks @PGStats and @ballardw.

 

Both work great! However I now want to increase the length of the string to be length 10 and prefixed with 0's if it is less than this until it reaches the correct length. Any ideas?

Ksharp
Super User
data have;
input id :$12.;
datalines;
5-12344
5-1234
5-123456
7-345
2-3456
;
run;
data want;
 set have;
 want=repeat('0',9);
 x=scan(id,1,'-');
 y=scan(id,-1,'-');
 substr(want,1,length(y))=y;
 substr(want,10-length(x)+1)=x;
run;

PGStats
Opal | Level 21

Since I was already using heavy artillery, here is some more:

 

data have;
input id :$12.;
datalines;
5-12344
5-1234
5-123456
7-345
2-3456
;

proc sql;
select id, 
    put(input(prxchange("s/(\d+)\-(\d+)/\200\1/o", 1, id),10.),z10.0) as newId
from have;
quit;
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
  • 6 replies
  • 1157 views
  • 1 like
  • 5 in conversation