Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: reordering observations


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: reordering observations
Date   Fri, 1 Aug 2008 17:44:04 +0100

Well, I didn't recognise -reorder- either. I looked at the URL you cited and discovered that it was something I wrote, which is on SSC. As you say, it is nothing to do with your problem. It's about ordering variables, i.e. columns, not sorting observations, i.e. rows. 

That just underlines the longstanding advice in the FAQ: 

"Say what command(s) you are using. If they are not part of official Stata, say where they come from: the STB/SJ, SSC, or other archives." 

To which we should perhaps add "Parents may not recognise their own children."

On your problem, I don't need to re-echo the general warning, as Maarten and Svend made the point so well and Cindy has established that she really does want this. 

On how you do it, no -merge-s or file choreography are necessary. Here's how to do it in place. 

First off, -fixsort- from SSC offers a canned solution: 

. ssc inst fixsort 

. fixsort var3 

Or (better style) 

. fixsort var3, gen(newvar3) 

-fixsort- codifies a trick I found in the official code for -qqplot-. 

. l

     +--------------------+
     | var1   var2   var3 |
     |--------------------|
  1. |    3      7      9 |
  2. |    2     10      3 |
  3. |    7      8      5 |
     +--------------------+

. gen long orig = _n

. sort var3

. gen newvar3 = var3[orig]

. sort orig

. l

     +-------------------------------------+
     | var1   var2   var3   orig   newvar3 |
     |-------------------------------------|
  1. |    3      7      9      1         3 |
  2. |    2     10      3      2         5 |
  3. |    7      8      5      3         9 |
     +-------------------------------------+


The above is perhaps especially for programmers to borrow. -fixsort- adds safety features and a few bells and whistles. I found I needed the basic trick once about every two years, but looking it up and re-implementing it was enough of a pain to make me want to program and document it properly. 

Nick
[email protected] 

Cindy Gao

Thank you to all those who responded. I realise that what I want to do is bit unusual, but there is reason for that - I want to compare the original variable and the reordered one, with respect to another variable. Thank you for the cautions though, and for the suggestions which have solve my problem.

PS -reorder- was mentioned in a previous Statalist posting, http://www.stata.com/statalist/archive/2004-08/msg00873.html

David Airey

If merge is not your thing, export to Microsoft Excel or any  
spreadsheet. Sort just that column. Import to Stata.

On Aug 1, 2008, at 8:43 AM, Cindy Gao wrote:

> Hello,
>
> I need to reorder one of my variables (from lowest to highest)  
> without changing the order of other variables in my dataset. Is  
> there any simply way to do this?
>
> Like, I have now:
> var1    var2       var3 ...
> 3        7             9
> 2        10           3
> 7        8             5
> ..........
>
> And I like to reorder var3 to get:
> var1    var2       var3 ...
> 3        7             3
> 2        10           5
> 7        8             9
> ..........
>
> Please, this is not the -reorder- command which is already there in  
> Stata, it is something different.

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index