Statalist


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

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


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: How to attach a string to a numeric variable as its value lable
Date   Wed, 26 Mar 2008 10:29:05 -0000

I agree with Friedrich Huebler and Joseph Coveney. Jia wants the values of a string variable x to be the labels of a numeric variable y. It follows directly that any solution must pivot on assignments of the form 

. label define y <value_of_y> "<value_of_x>", modify 

(Joseph sets up his code so that he can use -add- not -modify-, but that's not a contradiction of the main story here.) 

Jia's solution does not meet that requirement. 

As Xiao Chen pointed out, this tactic is already codified as a program -labmask- available in the -labutil- package from SSC. -labmask- takes Friedrich's approach further by looping not over the observations but over the distinct values concerned. 
(Joseph does that in an equivalent way.) 
There are also checks that the numeric variable contains integers only -- in the observations specified -- and that values of x are constant within groups of y. 

Thanks to Xiao Chen for the web page advertising -labmask-. I was not aware of that. 

Jia says "I must [have] read a solution somewhere, but I can not recall it." 

That is completely correct. Jia asked a question on 28 February to which an answer was also to use -labmask-. 

============================== extract from Statalist archives 28 February 
Jia Xiangping

Thanks a lot, Nick.

I just went through the commands of -labutil-, and found they are
quite useful. I should have known this earlier.

Nick Cox 

> I understand this as follows:
>
> Suppose variable -x- has values 11, 12, 13, ... and value labels "A", "B", "C", ....
>
> You want -y- to have values 111, 112, 113, ... and the corresponding value labels.
>
> Try
>
> gen y = 100 + x
> labmask y, values(x) decode
>
> Here -labmask- is in -labutil- on SSC.

Jia Xiangping

> A variable in int storage type, reflecting different regions.
>
> label                             region code
> value
> 11                      A
> 12                      B
> 13                      C
> 21                      D
> 22                      E
> 23                      F
> 31                      G
> 32                      H
>
> Now I want to generate a new variable with a prefix "1" denoting the
> country code. But the variable still carry the codes.
> 111                     A
> 112                     B
> 113                     C
> 121                     D
> 122                     E
> 123                     F
> 131                     G
>
> How shall I do that with -recode-? Or there are some other commands for that?
==========================

Jia Xiangping

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.

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