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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.