BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Alexxxxxxx
Pyrite | Level 9

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
PeterClemmensen
Tourmaline | Level 20

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;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

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',' ');
PeterClemmensen
Tourmaline | Level 20

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 433 views
  • 7 likes
  • 3 in conversation