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

 

I have a ID variable of text type as below

 

020011
020028
020036
020029
020013

 

I want to convert it into the following by adding a "-" in between

02-0011
02-0028
02-0036
02-0029
02-0013

 

Thank you so much!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

One way

 

data have;
input ID $;
datalines;
020011
020028
020036
020029
020013
;

data want;
    set have;
    ID=catx('-', substr(ID, 1, 2), substr(ID, 3));
run;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

One way

 

data have;
input ID $;
datalines;
020011
020028
020036
020029
020013
;

data want;
    set have;
    ID=catx('-', substr(ID, 1, 2), substr(ID, 3));
run;
zimcom
Pyrite | Level 9

@PeterClemmensen THANK YOU!

PeterClemmensen
Tourmaline | Level 20

Using RegEx

 

data want(drop=rid);
    set have;
    rid=prxparse('s/(.{2})(.*)/$1-$2/');
    call prxchange(rid, -1, ID);
run;
zimcom
Pyrite | Level 9

@PeterClemmensen thank you!

PeterClemmensen
Tourmaline | Level 20
novinosrin
Tourmaline | Level 20

Fun and abuse 🙂

 


data have;
input ID $;
datalines;
020011
020028
020036
020029
020013
;

data want;
set have;
array t(3) $2 _temporary_;
length want $7;
call pokelong(id,addrlong(t(1)),length(id));
want=catx('-',t(1),cats(t(2),t(3)));
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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