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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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