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 10:52:11 -0400

Thank you Nick.  Your suggestions prompted me to do a little more homework: hence, my belated response. 

My original question is about both the points you raise (I think).

Regarding your point 2., my loop below produces such labels in advance following the sorting of a. But in 2. below you suggest that this could be done with a one line command.  Are you thinking of some use of -levelsof-? Because if I have (say) many values in b, a one line of the type -label define lblname 1 "a" 4 "b" ... - is rather tedious (although it forces the idiosyncratic sorting I want).  But most importantly it is not readily changeable if I want to change the sort order (BTW: The whole use of this is to produce -graph twoyay bar- charts where the xlabels are sorted in any arbitrary fashion; contrary to -graph bar-, there is no -sort()- option in -graph twoway bar- (that I could find...))
 
Regarding your point 1., I managed to force -enocode- to use the arbitrary sorting produced in advance (by the loop below) by coding -encode a, gen(c2) label(lblname)-.  This produces the same variable c produced by -label values c lblname- below. Then, I am more confused: is there a concrete advantage then in using -encode- afterall if I have to specify the label name in advance?  Wouldn't be more useful if encode had a -sort(varlist)- option?

Grateful for any further suggestions

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