Help using Base SAS procedures

Question how do I add leading zero to numbers under 10?

Reply
Occasional Contributor
Posts: 18

Question how do I add leading zero to numbers under 10?

I need to add leading zero to any number under 10.

data lat;

set clovis_data;

  latdeg1=int(latitude);

  latdeg2=int((latitude-latdeg1)*60);

  latdeg3=round((((latitude-latdeg1)*60)-latdeg2)*60);

run;

I need to add leading zero to any number under 10 for Latdeg2 and Latdeg3.

 

Trusted Advisor
Posts: 1,933

Re: Question how do I add leading zero to numbers under 10?

Posted in reply to GregINGov

You can use the format Z2. to make the values appear with a leading zero (or some version of the Zw.d format)

Or you can turn them into character variables and concatenate a zero before the actual value if it is less than 10.

Of the two choices, I prefer the first in most cases, which leaves the number as a numeric variable

Occasional Contributor
Posts: 18

Re: Question how do I add leading zero to numbers under 10?

Posted in reply to PaigeMiller

so in format Z2 I  can make just the number under 10 with a leading zero?

Super User
Posts: 7,866

Re: Question how do I add leading zero to numbers under 10?

Posted in reply to GregINGov

That is the explicit purpose of the z2. format.

2 -> 02

9 -> 09

10 -> 10

100 and up will yield ** and generate a NOTE in the log that the format was too small.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,997

Re: Question how do I add leading zero to numbers under 10?

Posted in reply to GregINGov

Well, if you have a 0 preceding a number like: 02, its not really a number as such.  So the number there is 2.  If you want to display that with a preceding 0 then you have two options - apply a format Z2. to that variable - or put the number into Z2 and store it in a character variable.  There are plusses and minus to each approach.

Trusted Advisor
Posts: 3,215

Re: Question how do I add leading zero to numbers under 10?

Posted in reply to GregINGov
Ask a Question
Discussion stats
  • 5 replies
  • 329 views
  • 9 likes
  • 5 in conversation