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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.