☑ This topic is solved.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 09-30-2022 09:49 AM
(1118 views)
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 ;
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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',' ');
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;