Thanks Friedrich. It helps a lot. A command -labmask- is another
option. But your solution helps explain the looping process. Thanks.
On 3/26/08, Friedrich Huebler <[email protected]> wrote:
> Xiangping Jia,
>
> You asked "Does Stata have a simple way for such a function?" Was my
> solution not simple enough?
>
> Friedrich
>
> On Tue, Mar 25, 2008 at 7:15 PM, Jia Xiangping <[email protected]> wrote:
> > Thanks Friedrich. As you said, the new numeric variable has the values
> > 1, 2, 3, and so on, but alphabetically with x. -replace- is a wrong
> > way; the numeric is the same, but the labels are changed. Does stata
> > have a simple way for such a function?
> >
> > Thanks for the caution, Margaret. All the variables have no some
> > extraneous blanks.
> >
> >
> >
> > encode x, gen(new)
> > list new y x, nolabel sep(0)
> > replace new=y
> > list new y x, sep(0)
> >
> > new y x new[replaced] y x
> > 1 27 BUGIRI SOROTI 27 BUGIRI
> > 1 27 BUGIRI SOROTI 27 BUGIRI
> > 4 28 IGANGA TORORO 28 IGANGA
> > 4 28 IGANGA TORORO 28 IGANGA
> > 4 28 IGANGA TORORO 28 IGANGA
> > 16 29 MAYUGE WAKISO 29 MAYUGE
> > 16 29 MAYUGE WAKISO 29 MAYUGE
> > 16 29 MAYUGE WAKISO 29 MAYUGE
> > 16 29 MAYUGE WAKISO 29 MAYUGE
> > 16 29 MAYUGE WAKISO 29 MAYUGE
> > 16 29 MAYUGE WAKISO 29 MAYUGE
> > 5 30 JINJA 30 30 JINJA
> > 5 30 JINJA 30 30 JINJA
> > 5 30 JINJA 30 30 JINJA
> > 8 31 KAMULI 31 31 KAMULI
> > 8 31 KAMULI 31 31 KAMULI
> > 8 31 KAMULI 31 31 KAMULI
> > 8 31 KAMULI 31 31 KAMULI
> > 23 32 PALLISA 32 32 PALLISA
> > 29 33 WAKISO 33 33 WAKISO
> > 29 33 WAKISO 33 33 WAKISO
> > 19 34 MPIGI 34 34 MPIGI
> > 19 34 MPIGI 34 34 MPIGI
> > 21 35 MUKONO 35 35 MUKONO
> > 21 35 MUKONO 35 35 MUKONO
> > 21 35 MUKONO 35 35 MUKONO
> > 11 36 KAYUNGA 36 36 KAYUNGA
> > 2 37 BUSHENYI 37 37 BUSHENYI
> > 25 38 RUKUNGIRI 38 38 RUKUNGIRI
> > 9 39 KAPCHORWA 39 39 KAPCHORWA
> > 9 39 KAPCHORWA 39 39 KAPCHORWA
> > 9 39 KAPCHORWA 39 39 KAPCHORWA
> > 27 40 SOROTI 40 40 SOROTI
> > 13 40 KUMI 40 40 KUMI
> >
> >
> >
> >
> >
> >
> > On 3/25/08, Friedrich Huebler <[email protected]> wrote:
> > > Xiangping Jia,
> > >
> > > When you -encode- a string variable, the new numeric variable has the
> > > values 1, 2, 3, and so on. These values are probably not the same as
> > > those in the variable y. In the example in your message, the label
> > > "RAKAI" is assigned to the value 1 and not the value 21 when you
> > > -replace newx=y-.
> > >
> > > Below is an inefficient solution that creates correct value labels.
> > > The solution is inefficient because it loops over all observations.
> > >
> > > . count
> > > . local n = r(N)
> > > . forvalues i = 1/`n' {
> > > local xi = x[`i']
> > > local yi = y[`i']
> > > lab def ylab `yi' "`xi'", modify
> > > }
> > > . lab val y ylab
> > > . lab list ylab
> > > ylab:
> > > 21 RAKAI
> > > 22 MASAKA
> > > 23 MBALE
> > > 24 SIRONKO
> > > 25 TORORO
> > > 26 BUSIA
> > >
> > > Friedrich
> > >
> > > On Tue, Mar 25, 2008 at 4:32 PM, Jia Xiangping <[email protected]> wrote:
> > > > Dear all,
> > > >
> > > > There are two variables, one string and one numeric. The meaning of x
> > > > is supposed to be attached to y as its value label.
> > > >
> > > > x y
> > > > RAKAI 21
> > > > RAKAI 21
> > > > RAKAI 21
> > > > MASAKA 22
> > > > MASAKA 22
> > > > MASAKA 22
> > > > MASAKA 22
> > > > MBALE 23
> > > > MBALE 23
> > > > MBALE 23
> > > > SIRONKO 24
> > > > SIRONKO 24
> > > > TORORO 25
> > > > TORORO 25
> > > > BUSIA 26
> > > >
> > > >
> > > > The simple way I did was:
> > > > . encode x, gen(newx)
> > > > . replace newx=y
> > > > . list newx y
> > > >
> > > > Then it is found some are not replaced. I must read a solution
> > > > somewhere, but I can not recall it. Could someone give a quick
> > > > solution? Thanks.
> > > >
> > > > newx x
> > > > 65. | TORORO IGANGA |
> > > > 66. | WAKISO MAYUGE |
> > > > 67. | WAKISO MAYUGE |
> > > > 68. | WAKISO MAYUGE |
> > > > 69. | WAKISO MAYUGE |
> > > > 70. | WAKISO MAYUGE |
> > > > 71. | WAKISO MAYUGE |
> > > > 72. | 30 JINJA |
> > > > 73. | 30 JINJA |
> > > > 74. | 30 JINJA |
> > > > 75. | 31 KAMULI |
> > > > 76. | 31 KAMULI |
> > > >
> > > >
> > > > ps. Stata 10. Windows XP.
> > > >
> > > >
> > > >
> > > > --
> > > > Xiangping Jia
> >
> >
> >
> > --
> > Xiangping Jia
> *
> * 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/
>
--
Xiangping Jia
*
* 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/