BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hi,

Could you please let me know any trick to pad character variable with leading zeroes?

I have a 4 byte variable,if the data in that field is less than 4 bytes,then I would like to pad the value with leading zeroes.

Please advice.
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Your character variable value is all digit? Use INPUT to read as a number, then Z4. format to add leading zeroes.

 

data test;
    length charvar $ 4 padded $ 4;
    infile datalines;
    input charvar;
    /* read charvar as number, then format with leading zeroes */
    /* then put result into padded */
    padded = put(input(charvar,best4.),z4.);
datalines;
28
1034
4
8759
1234
234
34
;
run;


Output:

 

charvar  padded

28       0028 
1034     1034 
4        0004 
8759     8759 
1234     1234 
234      0234 
34       0034 


Ksharp

View solution in original post

10 REPLIES 10
Ksharp
Super User

Your character variable value is all digit? Use INPUT to read as a number, then Z4. format to add leading zeroes.

 

data test;
    length charvar $ 4 padded $ 4;
    infile datalines;
    input charvar;
    /* read charvar as number, then format with leading zeroes */
    /* then put result into padded */
    padded = put(input(charvar,best4.),z4.);
datalines;
28
1034
4
8759
1234
234
34
;
run;


Output:

 

charvar  padded

28       0028 
1034     1034 
4        0004 
8759     8759 
1234     1234 
234      0234 
34       0034 


Ksharp

ren2010
Obsidian | Level 7
Thanks Kshrap!
namrata
Fluorite | Level 6

Ksharp

In case I have a variable(character format) with both digits and characters, how will the code change?

For instance,

Raw data      Desired output

007851           007851

05614            005614

   2447            002447

Could you please help?

namrata
Fluorite | Level 6

Hi

I just got the solution to my problem here:

https://groups.google.com/forum/#!topic/comp.soft-sys.sas/uBuLYXMRqhw

if length(raw data) = 6 then new=raw data;

else new=repeat('0',6-length(raw data)-1)||raw data;

Just thought would share the code here if anyone needs Smiley Happy

RamKumar
Fluorite | Level 6

Hi Namrata,

I believe your code will not produce  a desired output if the input records with length more than 6.

can anyone suggests?

Ksharp
Super User

OK. Here are two solutions . Pick one up you like :

data have;
input Raw $char6.;
cards;
007851 
05614  
  2447
;
run;

/*** the first way ***/
data want1;
 set have;
 want=translate(right(raw),'0',' ');
run;

/*** the second way ***/
data want2;
 set have;
 want=put(input(raw,best32.),z6.);
run;

Xia Keshan

namrata
Fluorite | Level 6

The first way seems magic Smiley Happy! So simple & yet effective!

Thanks!

@Ram Kumar My records are all less than 6.

pmpradhan
Quartz | Level 8

I have a similar problem. My zip variable data has following values:

zip

123456789

123456789

1234

1234

12345

123456789

12345

12345

1234

123456789

 

I want to get a single variable with:

  • first 5 digit from 9 digit zip var;
  • add leading zero where there is 4 digit zip and
  • keep five digit zip value as it is.

Thanking you in advance!

 

 

 

pmpradhan
Quartz | Level 8

Found the answer to my question:

 

data test2; set ptcz2;
zip5 = substr(compress(zip),1,5);
zip5v2 = substr(("0000"||zip5),length(zip5),5);

run;

Aidaan_10
Calcite | Level 5

I have a similar problem. My data has following values:

zip

123456789

-123456789

1234

-.234

12345

-.23456789

12345

12345

1234

123456789

 

I want to get a single variable with:

  • first 6 digit from 9 digit zip var;
  • add leading zero where there is 1 less  digit zip and
  • keep 6 digit zip value as it is.
  • I tried increasing the zw.d format to 7.3 and its working for values with negative sign but the other values are not correct. Expected format is 6.3.

Thank you 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 10 replies
  • 232724 views
  • 21 likes
  • 7 in conversation