BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Q1983
Lapis Lazuli | Level 10

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

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

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).

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

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
PeterClemmensen
Tourmaline | Level 20

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;
GinaRepole
SAS Employee

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.

 

Tom
Super User Tom
Super User

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

GinaRepole
SAS Employee
Whoops! Good catch. I updated the above post to be accurate.
mich1
Obsidian | Level 7

Robot wink

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*/

Bread Crumbs and Circuses for All
s_lassen
Meteorite | Level 14

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).

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 4151 views
  • 4 likes
  • 6 in conversation