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/