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: RE: RE: -encode- with predetermined sorting


From   "Impavido, Gregorio" <GImpavido@imf.org>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: -encode- with predetermined sorting
Date   Tue, 12 Jun 2012 16:08:47 -0400

Nick,  I found it! (after more web searching):

* begin
sort a
gen c = _n
forvalues i=1/4 {
	label define lblname `i' "`: di b[`i']'", add
	}
label values c lblname
* end

Can be simply done by:

-egen c = axis(a), label(b)-

Congratulations! The missing -sort()- option for -encode-.

Gregorio

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: Friday, June 08, 2012 6:27 AM
To: 'statalist@hsphsun2.harvard.edu'
Subject: st: RE: -encode- with predetermined sorting

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 
n.j.cox@durham.ac.uk 

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/

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