data have;
input ln_no ;
datalines;
21555555
5544000
6654444
;
run;
data want;
set have;
if ln_no lt 17 THEN LN_No = (input(ln_no, best4.),z17.);
/*if ln_no lt 17 THEN LN_No = substr(left(LN_Nbr_R),1,10.);*/
/*if ln_no lt 17 THEN LN_No = substr(left(LN_Nbr_R),1,10.);*/
/*if ln_no lt 17 THEN LN_No = put(ln_no, z10.);/*Ln_no = put(Account, z10.);*/
run;
ln_no started out as a numeric.
Desired output is to have a 17 digit number with leading zeros. The format should retain its numeric status
Something like this
0000000021555555
As you can see I have tried different pieces of code here.
If your variable is actually a loan number with as many as 17
digits, then SAS can't hold that number accurately in a numeric variable. You need to store it as a character variable:
data have;
input @1 ln_no $12.;
ln_no_new = repeat('0', 16 - length(ln_no) - 1) !! ln_no;
put _all_;
datalines;
21555555
5544000
6654444
;
run;
A bit of a problem with this offer is that REPEAT returns $200 by default, and so LN_NO_NEW will be $212. To avoid this, either length $17 has to be assigned to LN_NO_NEW beforehand or the $17. format should be applied to the result, like so:
ln_no_new = put (repeat('0', 16 - length(ln_no) - 1) !! ln_no, $17.) ;
But perhaps it's simpler (at least to me) to use:
ln_no_new = translate (put (ln_no, $17.-R), "0", "") ;
Kind regards
Paul D.
@hashman - Good point. A LENGTH statement would probably be the most obvious solution for others to follow.
@hashman - Or assign back to original variable having adjusted the INFORMAT to the required length:
data have;
input @1 ln_no $16.;
ln_no = repeat('0', 16 - length(ln_no) - 1) !! ln_no;
put _all_;
datalines;
21555555
5544000
6654444
;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.