Help using Base SAS procedures

how to pad character variable with leading zeroes?

Accepted Solution Solved
Reply
N/A
Posts: 0
Accepted Solution

how to pad character variable with leading zeroes?

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.

Accepted Solutions
Solution
‎05-27-2016 09:57 AM
Super User
Posts: 9,681

Re: how to pad character variable with leading zeroes?

[ Edited ]

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


All Replies
Solution
‎05-27-2016 09:57 AM
Super User
Posts: 9,681

Re: how to pad character variable with leading zeroes?

[ Edited ]

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

Contributor
Posts: 74

Re: how to pad character variable with leading zeroes?

Thanks Kshrap!
Contributor
Posts: 52

Re: how to pad character variable with leading zeroes?

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?

Contributor
Posts: 52

Re: how to pad character variable with leading zeroes?

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

Regular Contributor
Posts: 168

Re: how to pad character variable with leading zeroes?

Hi Namrata,

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

can anyone suggests?

Super User
Posts: 9,681

Re: how to pad character variable with leading zeroes?

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

Contributor
Posts: 52

Re: how to pad character variable with leading zeroes?

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

Thanks!

@Ram Kumar My records are all less than 6.

New Contributor
Posts: 3

Re: how to pad character variable with leading zeroes?

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!

 

 

 

New Contributor
Posts: 3

Re: how to pad character variable with leading zeroes?

Found the answer to my question:

 

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

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 63776 views
  • 7 likes
  • 6 in conversation