Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: Adding leading zeros to numeric variable and converting to string


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: Adding leading zeros to numeric variable and converting to string
Date   Sat, 17 May 2003 17:41:26 +0100

Nick Cox
> Ankur Sarin
> > 
> > I need to convert a numeric variable to string but add
> > leading zeros. 
> > So a 4 needs to be converted to 004, a 40 to 040 and
> > 400 to 400 (only  upto 3 digits). I need to combine
> > this new string variable with another to make ids. 
> > I tried
> > tostring hhpsu, f(%03s)
> >  but that returns hhpsu as str1 and with all missing
> > values. 
> 
> The reference is to -tostring- (STB-56/STB-57).
>  
> I don't know which version you are using but the 
> -format()- specified must be a numeric format 
> indicating how the number is to be rendered. That 
> is, it is applied to the input numeric variable, 
> not the output string variable.
> 
> Your error is trapped in the version of STB-57, 
> which is also that on SSC. 
> 
> As all your numbers appear to be integers, 
> you need only one line: 
> 
> either 
> 
> tostring hhpsu, format(%012.0f) 
> 
> or 
> 
> gen str3 hhpsu_s = string(hhpsu, "%012.0f")
> 

Sorry, I meant "%03.0f". I don't know where that "%012.0f" 
came from. 

Nick 
[email protected] 

*
*   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/



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