My guess is that you are confusing two separate things, how a number is
held in storage and how it is presented.
If you want display that includes a leading zero to pad 5 displayed
digits to 6, then check out display formats, say
. format x %06.0f
If you want identifiers that always include 0s as part of what is held,
then that requires a string version, and what you did sounds correct of
one way of doing it. See also below.
. set obs 5
obs was 0, now 5
. gen foo = cond(_n == 5, 99999, 999999)
. gen bar = string(foo, "%06.0f")
. l
+-----------------+
| foo bar |
|-----------------|
1. | 999999 999999 |
2. | 999999 999999 |
3. | 999999 999999 |
4. | 999999 999999 |
5. | 99999 099999 |
+-----------------+
Nick
[email protected]
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Nathan Hutto
Sent: 10 February 2010 18:37
To: [email protected]
Subject: st: How to lengthen a number
Hi all,
I have a numeric variable "x". Some of the values are 5 digits and
others are 6 digits. I would like all of the values to be 6 digits, so
I would like to add a zero to the beginning of the 5 digit values. I
converted "x" to a string variable, created another string
variable=="0" and added them together, but that didn't work. Anyone
know how I can do this?
thank you,
Nathan
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/