Hello SAS world,
I need help with the following problem.
I've a column with a 8 digit number, this should be padded with zeros in the appropriate places until the 9 digit, 5-4 format is established.
Example: original data Convert to
(4-4) XXXX-XXXX (5-4) 0XXXX-XXXX
(5-3) XXXXX-XXX (5-4) XXXXX-0XXX
Thanks
--Sujith
Is there a - in the middle of the digits? If not how do you know which part may need the padding? And if the is a - it is not a "number" but a character value.
Something like this may get you started.
data junk; length x $ 10; input x $ 1-9; x= catx('-',put(input(scan(x,1),best.),z5.),put(input(scan(x,2),best.),z4.)); datalines; 1234-1234 12345-123 ; run;
Note if your orginal variable is specified as a 9 character field the result will not fit and you'll need the length set before any SET statement to allow the code to work.
Is this a character variable?
Can you post some more sample data we can work with?
Have you looked at the Z5/Z4 formats with a PUT statement?
Is there a - in the middle of the digits? If not how do you know which part may need the padding? And if the is a - it is not a "number" but a character value.
Something like this may get you started.
data junk; length x $ 10; input x $ 1-9; x= catx('-',put(input(scan(x,1),best.),z5.),put(input(scan(x,2),best.),z4.)); datalines; 1234-1234 12345-123 ; run;
Note if your orginal variable is specified as a 9 character field the result will not fit and you'll need the length set before any SET statement to allow the code to work.
Hello,
Yes, there is a - in the middle of the digits and it's character (my mistake).
If you don't mind, can you please your 3rd line in your code is doing? It would be a great learning.
Thanks
--Sujith
Sample data would help. Here a code sample making assumptions on how your data looks like.
data sample;
have=' 4444-333 ';
/* bit of spaghetti code but should work and doesn't need intermediary variables */
want=put(input(scan(have,1,'-'),f5.),z5.)||'-'||put(input(scan(have,2,'-'),f4.),z4.);
output;
stop;
run;
For your real implementation: You probably should also have a check that your source string looks as expected so that you don't unvoluntarily remove digits/characters when converting to your want string.
you could also try the perl regular expression
data junk;
length x $ 10;
input x $ 1-9;
if prxmatch('m/\d{5,}\-\d{3,}/',x) then new=prxchange('s/(\d{5,})\-(\d{3,})/\1-0\2/',-1,x);
else if prxmatch('m/\d{4,}\-\d{4,}/',x) then new=prxchange('s/(\d{4,})\-(\d{3,})/0\1-\2/',-1,x);
else if prxmatch('m/\d{5,}\-\d{4,}/',x) then new=x;
datalines;
1234-1234
12345-123
;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.