Statalist


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

Re: st: Is there a command similar to "bys" if I want to gsort the variables?


From   Eric Booth <ebooth@ppri.tamu.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Is there a command similar to "bys" if I want to gsort the variables?
Date   Thu, 3 Dec 2009 10:36:33 -0600

>
>

On Dec 3, 2009, at 9:54 AM, gjhxmu@sina.com wrote:

> Dear statalists,
> 
> Is there a command similar to "bys" if I want to gsort the variables?
> 
> For example,
> 
> bys year (area): g num=_n
> 
> However, I want to gsort the variable of area.
> 
> I have tried as followings,
> 
> gsort year -area
> 
> by year: g num=_n
> 
> Is there an easier way to do it just like -bys-?
> 


You can use the -gen()- option of -gsort- to create the running N by each group:

gsort year - area, gen(num)


> 
> By the way, when I typed "d,short" after I typed "gsort year -area", the result was as followings,
> 
> Contains data from study.dta
>  obs:           155                          
> vars:             6                          5 Jun 2009 01:45
> size:         3,875 (99.9% of memory free)
> Sorted by:  year
> 
> Why the sorted variable did not contain area?
> 


-desc- displays only "year", not "area", after -gsort- because gsort creates a tempvar for the descending sorting variables like "area", so they go away after -gsort- runs its course.
By the way, if both variables were ascending-- that is if you were to type  "gsort year area"  --then -describe- would show both "year" and "area",not just "year".  

If you want to to see the tempvar that -gsort- creates when you have ascending and descending sorts in -gsort-, you can edit the ado-file by adding the a line with the command "desc"  just after the line:  "	sort `part1' `tvars'  "  in the ado-file.  It will show under -desc- that the dataset is sorted by "year" and the tempvar "_00000".    Of course, always take care when looking at or editing a system ado-file--you may want to make a copy of gsort.ado and name it something else before playing around with it too much.

> Thank you for any help!
> 
> Best regards,
> 
> Rose


~ Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu
Office: +979.845.6754

*
*   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   |   What's new   |   Site index