Solved
Contributor
Posts: 58

# Manipulate the number

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

Accepted Solutions
Solution
‎04-04-2017 02:14 PM
Super User
Posts: 13,521

## Re: Manipulate the number

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.

All Replies
Super User
Posts: 23,700

## Re: Manipulate the number

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?

Solution
‎04-04-2017 02:14 PM
Super User
Posts: 13,521

## Re: Manipulate the number

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.

Contributor
Posts: 58

## Re: Manipulate the number

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

Posts: 4,736

## Re: Manipulate the number

[ Edited ]

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.

Posts: 1,147

## Re: Manipulate the number

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;``````
Thanks,
Jag
Contributor
Posts: 58