Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: RE: -encode- with predetermined sorting
From 
 
Nick Cox <[email protected]> 
To 
 
"'[email protected]'" <[email protected]> 
Subject 
 
st: RE: -encode- with predetermined sorting 
Date 
 
Fri, 8 Jun 2012 11:26:52 +0100 
1. The value labels for an idiosyncratic -encode- need to exist in advance. Idiosyncratic just means here an encoding that -encode- would not produce by default. 
2. In your case you could define them with a single command line, which would be quicker to type and to execute than your code. 
I am guessing that your question is more about the first than the second. 
Nick 
[email protected] 
Impavido, Gregorio
I have the following dataset:
. list
 
      +--------+
      | a    b |
      |--------|
   1. | 10   a |
   2. | 40   b |
   3. | 30   c |
   4. | 20   d |
      +--------+
If I encode b and generate c, I get:
. list *, nolabel
 
      +-------------+
      | a    b   c  |
      |-------------|
   1. | 10   a   1  |
   2. | 40   b   2  |
   3. | 30   c   3  |
   4. | 20   d   4  |
      +-------------+
Now, I want to have a different sort order for c, say as a function of a:
. list *, nolabel
 
      +-------------+
      | a    b   c  |
      |-------------|
   1. | 10   a   1  |
   2. | 40   b   4  |
   3. | 30   c   3  |
   4. | 20   d   2  |
      +-------------+
Is there a more rapid way than the following to obtain what I want?
 
sort a
gen c = _n
forvalues i=1/4 {
	label define lblname `i' "`: di b[`i']'", add
	}
label values c lblname
*
*   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/