SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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