Dear all,
is there a way to add leading zeros?
I have the ‘Have’ row and expect to get the 'Want' row (add 'zero' to get Nine digit number, No need to convert to Num).
Have | Want |
123456789 | 123456789 |
12345678 | 012345678 |
1234567 | 001234567 |
123456 | 000123456 |
12345 | 000012345 |
1234 | 000001234 |
123 | 000000123 |
12 | 000000012 |
1 | 000000001 |
Could you please give me some advice about this?
Many thanks in advance.
data have ; input variable :$29.; cards; 123456789 12345678 1234567 123456 12345 1234 123 12 1 ;
Try this
data have ;
input variable :$29.;
cards;
123456789
12345678
1234567
123456
12345
1234
123
12
1
;
data want;
set have;
newvar = put(input(variable, 9.), z9.);
run;
9 digit or 29 digit?
Check out this old question: https://communities.sas.com/t5/SAS-Programming/char-function-add-left-zeroes/m-p/785107#M250540
data have ;
input variable :$9.;
cards;
123456789
12345678
1234567
123456
12345
1234
123
12
1
;
data want;
set have;
variable = translate(right(variable),'0',' ');
run;
If you want the 9 digit number that is stored in a variable that is actually longer than 9 bytes then change to:
variable = translate(right(substr(variable,1,9)),'0',' ');
Try this
data have ;
input variable :$29.;
cards;
123456789
12345678
1234567
123456
12345
1234
123
12
1
;
data want;
set have;
newvar = put(input(variable, 9.), z9.);
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.