Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: How to attach a string to a numeric variable as its value lable


From   "Friedrich Huebler" <[email protected]>
To   [email protected]
Subject   Re: st: How to attach a string to a numeric variable as its value lable
Date   Tue, 25 Mar 2008 18:05:10 -0400

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