## leading zeroes in character variable

Solved
Regular Contributor
Posts: 180

# leading zeroes in character variable

data want;

input ln_no \$ ;

datalines;

21555555

5544000

6654444

1145555555555

;

run;

data want2;

set want;

/*if ln_no lt 10 THEN LN_No = substr(left(LN_Nbr_R),1,10.);*/

if ln_no lt 10 THEN LN_No = put(ln_no, z10.);

run;

I am attempting to add leading zeroes sufficient to make the character variable ln_no have 10 characters. ie 21555555 becomes 0021555555   if the variable is 10 or more characters, I want to bring it in as is.  Above is the code I have tried but no success

Accepted Solutions
Solution
‎02-20-2018 09:26 AM
PROC Star
Posts: 277

## Re: leading zeroes in character variable

If your data is character, you may as well treat is as such, instead of messing round with numeric formats:

``````data have;
length ln_no \$20;
input ln_no \$ ;
datalines;
21555555
5544000
6654444
1145555555555
;run;

data want;
set have;
if lengthn(ln_no)<10 then
ln_no=repeat('0',9-lengthn(ln_no))!!ln_no;
run;

``````

I used the LENGTHN function because the LENGTH function will report a length of 1 for an empty string (LENGTHN returns 0).

All Replies
Super User
Posts: 8,289

## Re: leading zeroes in character variable

Why are you comparing your character variable to an integer constant?  Why are trying to apply a numeric format to your character variable?  One of your example values is longer than 10 characters?

If the values are valid integers then convert them to numbers and use the Z format to produce the value with the leading zeros.

``````data have
infile cards truncover ;
input ln_no \$20. ;
datalines;

21555555
5544000
6654444
1145555555555
;

data want;
set have ;
if 0 < lengthn(ln_no) <10 then ln_no=put(input(ln_no,32.),Z10.);
run;``````
```Obs        ln_no

1
2     0021555555
3     0005544000
4     0006654444
5     1145555555555```
PROC Star
Posts: 1,410

## Re: leading zeroes in character variable

Do like this

``````data want;
input ln_no \$;
datalines;
21555555
5544000
6654444
1145555555555
;
run;

data want2;
set want;
/*if ln_no lt 10 THEN LN_No = substr(left(LN_Nbr_R),1,10.);*/
ln_no=put(input(ln_no,best10.),z10.);
run;``````
SAS Employee
Posts: 30

## Re: leading zeroes in character variable

[ Edited ]

You can use the Zw.d format to add leading zeros, so I might suggest reading your data as numeric first, then converting over to character. There may be more elegant solutions, but here's an idea that seems to accomplish what you're asking for:

``````data want;
input ln_no;
ln_no2 = PUT(ln_no, Z16.);

DO i=1 TO 6;
IF CHAR(ln_no2, i) = 0 THEN
trim = i+1;
ELSE LEAVE;
END;

ln_no2 = SUBSTR(ln_no2, trim);

datalines;
21555555
5544000
6654444
1145555555555
;``````

This solution pads all values up to 16 characters with leading 0s (16 is the max length of a number on Windows SAS so this should be sufficient). Any extra zeros are then trimmed off, but not allowing the character string to go below a length of 10.

Super User
Posts: 8,289

## Re: leading zeroes in character variable

@GinaRepole  Zn.  is a FORMAT, which is how your code is using it.

SAS Employee
Posts: 30

## Re: leading zeroes in character variable

Whoops! Good catch. I updated the above post to be accurate.
Occasional Contributor
Posts: 17

## Re: leading zeroes in character variable

Here's what I do...

format NEW_NUM \$10. ;
SWAP = NUMBER;

Bread Crumbs and Circuses for All
Solution
‎02-20-2018 09:26 AM
PROC Star
Posts: 277

## Re: leading zeroes in character variable

If your data is character, you may as well treat is as such, instead of messing round with numeric formats:

``````data have;
length ln_no \$20;
input ln_no \$ ;
datalines;
21555555
5544000
6654444
1145555555555
;run;

data want;
set have;
if lengthn(ln_no)<10 then
ln_no=repeat('0',9-lengthn(ln_no))!!ln_no;
run;

``````

I used the LENGTHN function because the LENGTH function will report a length of 1 for an empty string (LENGTHN returns 0).

☑ This topic is solved.