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 have;
length
  loan_number $10
  
;
input loan_number ;

datalines;
2323333
22345099
112009899
345566
;
run;

data have2;
set have;
ln_no=put(loan_number,z10.);
run;

I ran the code and received format not found. Note this is a character variable. I want to assign leading zeros as needed to get a max 10 digit number in character format
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @Q1983  Yet another easy and lazy alternative-

data have;
length
  loan_number $10
  
;
input loan_number ;

datalines;
2323333
22345099
112009899
345566
;
run;

data want;
 set have;
 length need $10;
 need=translate(right(loan_number),'0',' ');
run;

Logic-

Move the contents to the right. Fill the blanks with 0.

 

View solution in original post

5 REPLIES 5
Reeza
Super User
Your original variable needs to be numeric. Convert it to a number and then apply the Z format.

ln_no = put( input(Loan_number, best12.), z10.);
AMSAS
SAS Super FREQ

In SAS there's always another way / different approach:

data have;
	length
  		loan_number $10 ;
	input loan_number ;
	loan_number=substr("0000000000"||loan_number,length(loan_number)) ;

datalines;
2323333
22345099
112009899
345566
;
run;
Reeza
Super User
Most definitely....I think there's at least two other ways to solve this I can think about, one using REPEAT() and another using SUBSTR() as well.
Tom
Super User Tom
Super User

Close.

data have;
	input loan_number $10.;
  put loan_number $10. ' -> ' @;
	loan_number=substr("0000000000"||loan_number,1+lengthn(loan_number)) ;
  put loan_number;
datalines;

1234567890
2323333
22345099
112009899
345566
;
           -> 0000000000
1234567890 -> 1234567890
2323333    -> 0002323333
22345099   -> 0022345099
112009899  -> 0112009899
345566     -> 0000345566
novinosrin
Tourmaline | Level 20

Hi @Q1983  Yet another easy and lazy alternative-

data have;
length
  loan_number $10
  
;
input loan_number ;

datalines;
2323333
22345099
112009899
345566
;
run;

data want;
 set have;
 length need $10;
 need=translate(right(loan_number),'0',' ');
run;

Logic-

Move the contents to the right. Fill the blanks with 0.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1303 views
  • 12 likes
  • 5 in conversation