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   "Joseph Coveney" <jcoveney@bigplanet.com>
To   "Statalist" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: How to attach a string to a numeric variable as its value lable
Date   Wed, 26 Mar 2008 09:22:25 +0900

Jia Xiangping wrote:

There are two variables, one string and one numeric. The meaning of x
is supposed to be attached to y as its value label.

[redacted]

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?

--------------------------------------------------------------------------------

The approach shown below produces the following output, which is what you
want:

 +-------------------+
 |       x         y |
 |-------------------|
 |   RAKAI     RAKAI |
 |   RAKAI     RAKAI |
 |   RAKAI     RAKAI |
 |-------------------|
 |  MASAKA    MASAKA |
 |  MASAKA    MASAKA |
 |  MASAKA    MASAKA |
 |  MASAKA    MASAKA |
 |-------------------|
 |   MBALE     MBALE |
 |   MBALE     MBALE |
 |   MBALE     MBALE |
 |-------------------|
 | SIRONKO   SIRONKO |
 | SIRONKO   SIRONKO |
 |-------------------|
 |  TORORO    TORORO |
 |  TORORO    TORORO |
 |-------------------|
 |   BUSIA     BUSIA |
 +-------------------+

MyLabels:
         21 RAKAI
         22 MASAKA
         23 MBALE
         24 SIRONKO
         25 TORORO
         26 BUSIA

An alternative approach is to use -label save- and -run- in lieu of the
approach that is illustrated below, which uses -merge-.  See the help file
for how to use -label save-.

Joseph Coveney

clear *
set more off
input str10 x byte 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
end
*
tempfile tmpfil0
generate long row_nr = _n // Optional
sort y
quietly save `tmpfil0'
*
contract x y
isid x // Optional, but advised
isid y // Likewise
forvalues i = 1/`=_N' {
   label define MyLabels `=y[`i']' `=x[`i']', add
}
label values y MyLabels
sort y
merge y using `tmpfil0'
assert _merge == 3 // Optional, but advised
sort row_nr // Optional
drop row_nr _freq _merge // Optional
list, noobs sepby(x)
label list MyLabels
exit


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index