Bookmark and Share

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


From   "Impavido, Gregorio" <[email protected]>
To   "[email protected]" <[email protected]>
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: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: Friday, June 08, 2012 6:27 AM
To: '[email protected]'
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 
[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/

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index