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!
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;
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;
@PeterClemmensen THANK YOU!
Using RegEx
data want(drop=rid);
set have;
rid=prxparse('s/(.{2})(.*)/$1-$2/');
call prxchange(rid, -1, ID);
run;
@PeterClemmensen thank you!
Anytime 🙂
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.