Thursday, February 16, 2006

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

Hi Ainsley,

Welcome to statalist. The solution to your problem is -tostring ed, format(%05.0f) replace- The key is in the somewhat cryptic format part: the first 0 in %05.0f means "add leading zeros", the 5 means "number is shown with 5 digits" the second 0 means "zero digits behind the decimal point" and the f means "fixed format". See -whelp format- for more information.

*-----begin example------ input x 1 12 123 1234 12345 end

tostring x, format(%05.0f) replace list *------end example--------

HTH, Maarten

--- ainsley charles <realcharlo@yahoo.com> wrote: > How do I add leading zeros to a string variable > conditional on its length? My objective is to create a > unique identifier <snip> > > reg ed > 10 100 > 11 3200 > 12 16400 > > Reg is consistently a str2 but ed is not (at most, > though, it's a str5). I wish my new variable to be a > str7 of the form "1000100", "1103200", and "1216400"

----------------------------------------- between 1/2/2006 and 31/3/2006 I will be visiting the UCLA, during this time the best way to reach me is by email

Maarten L. Buis Department of Social Research Methodology Vrije Universiteit Amsterdam Boelelaan 1081 1081 HV Amsterdam The Netherlands

visiting adress: Buitenveldertselaan 3 (Metropolitan), room Z214

+31 20 5986715

http://home.fsw.vu.nl/m.buis/ -----------------------------------------

___________________________________________________________ To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre. http://uk.security.yahoo.com * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/


Tag:


Links to this post:

Create a Link



<< Home

This page is powered by Blogger. Isn't yours?