Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: -encode- with predetermined sorting


From   "Impavido, Gregorio" <GImpavido@imf.org>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   st: -encode- with predetermined sorting
Date   Thu, 7 Jun 2012 16:45:15 -0400

Dear All,

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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index