Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: How can I add leading zeros conditionally?


From   "German Rodriguez" <[email protected]>
To   <[email protected]>
Subject   st: RE: How can I add leading zeros conditionally?
Date   Thu, 16 Feb 2006 23:02:32 -0500

Assuming reg and ed are already strings with up to 2 and 5 digits, try

gen zpid = substr("00"+ref,-2,2) + substr("00000"+ed,-5,5)

Here + concatenates strings, we padd each part of the id with enough zeroes
and then take the last 2 or 5 characters. The minus sign in substr tells it
to count from the end. 

You probably don't need this for ref if it is always >= 10. I assume you
have no missing values (blanks) but if you do the relevant part will be
coded as all zeroes.  

Cheers,
Germ�n

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of ainsley charles
Sent: Thursday, February 16, 2006 10:23 PM
To: [email protected]
Subject: st: How can I add leading zeros conditionally?

Hello Statalisters,




© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index