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

st: destring, keeping leading zeros


From   n j cox <[email protected]>
To   [email protected]
Subject   st: destring, keeping leading zeros
Date   Tue, 05 Jul 2005 15:31:54 +0100

You have put your finger on the nub of the problem.

If you want to have it both ways, then you must
keep the original version of the string variable
and use -destring, generate()-, or some other
command, to generate a numeric version of the same.

In Stata Journal 2:314-329 (2002) and elsewhere
the facilities for string-numeric conversions
are presented as three pairs. That article is
still fairly comprehensive, but note that
-tostring- is now an official command.

(1) The functions -real()-, -string()- are
brute-force solutions, often adequate, but
sometimes too heavy-handed to handle
complications.

(2) The commands -encode-, -decode- are for
having it both ways, having a string
variable and also a numeric equivalent using value
labels.

(3) The commands -destring-, -tostring-
are for correcting mistakes. That is,
Stata thinks your variable is numeric (string),
but you think it should be the other flavour.
The mistake may be yours, or the product of
some small unappreciated quirks in the data,
or the side-effect of having ventured inside
a spreadsheet, etc.

Your situation appears between (2) and (3).

Nick
[email protected]

Nawar, Eric

I have a string variable which has the following distinct codes:

0123
123

I want to make the variable numeric, but I want to keep the leading zero
on the first code to keep them distinct.  A suggestion to a similar
posting was to format with leading zeros - but this would make both
codes "0123".
*
*   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