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
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).
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
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;
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.
@GinaRepole Zn. is a FORMAT, which is how your code is using it.
Here's what I do...
/*REFORMAT NUMBER WITH LEADING ZEROS*/
format NEW_NUM $10. ;
SWAP = NUMBER;
NEW_NUM = put(SWAP, z10.);/*ADD LEADING ZEROS TO NEW_NUM*/
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).
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.