Help using Base SAS procedures

How to Add 0 before number

Reply
Contributor
Posts: 39

How to Add 0 before number

I've N number of zip codes but I need zip code minimum should be 6 numbers if less then it should prefix with '0'. Please help me on this
Esteemed Advisor
Posts: 6,334

Re: How to Add 0 before number

If it is already stored as character, do this:

zipcode = put(input(zipcode,best.),z6.);

else, you need to convert to character:

zipcode_char = put(zipcode,z6.);

or you simply assign it the format for display:

format zipcode z6.;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 39

Re: How to Add 0 before number

Complete data in number format so I can use as format option right?

Regards
Ravikumar

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Grand Advisor
Posts: 16,930

Re: How to Add 0 before number

Depends. Try it and see if it meets your requirements. 

Esteemed Advisor
Posts: 6,334

Re: How to Add 0 before number

Converting to number and then using a format provides for the simplest code. But you might to have to solve other issues (blanks within zipcodes etc) first for this to work.

In this like in most other cases, testing beats speculating

(German: "Probieren geht über studieren", English: "The proof of the pudding is in the eating")

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Esteemed Advisor
Esteemed Advisor
Posts: 7,003

Re: How to Add 0 before number

Whilst I agree with the above, if number convert is easiest, if the data contains non numerics that isn't going to work, so here is a text version:

data want;
  input zip $;
  if lengthn(zip) < 6 then zip=cats(repeat("0",5-lengthn(zip)),zip);
datalines;
12345
123456
12AE4
1
;
run;
Ask a Question
Discussion stats
  • 5 replies
  • 252 views
  • 0 likes
  • 4 in conversation