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

Re: st: gsort issue

From   "Brian P. Poi" <>
Subject   Re: st: gsort issue
Date   Thu, 5 Jul 2007 14:11:26 -0500 (CDT)

On Thu Jul  5 06:58:30 2007, Fred Wolfe wrote:

Is there a problem with gsort (Stata 10 and below) or am I misunderstanding something?

I have a variable called -phdif-. I want the greatest value of that variable to appear in the last observation. There are missing values, so I use -gsort- with the -mfirst- option.

. gsort phdif
. l phdif in 1,clean

1. 1

. l phdif in l,clean

169914. .

The problem appears to be that missings are still last even though I used the -mfirst- option.

Any suggestions? Is this a problem or am I thinking about this incorrectly?

The "mfirst" option of -gsort- applies only to variables sorted in descending order.

Stata stores missing values as extremely large numbers, so if a variable is sorted in descending order, missing values should appear first in the list since they are greater than all non-missing values.

-gsort-, however, tries to be helpful when sorting in descending order by putting the missing values at the end of the list, assuming that the user really cares about the large real values of the variable, not the missing values.

The "mfirst" option tells -gsort- to put the missing values first in the list instead of trying to be helpful by putting them at the end of the list.

If you want to get the missing values to appear first when doing an ascending sort, one way to proceed is to create a 0/1 variable equal to 0 if the variable of interest contains missing and 1 otherwise and then sort by the indicator variable and the variable of interest:

. sysuse auto
. generate missrep78 = cond(missing(rep78), 0, 1)
. gsort missrep78 rep78
. list rep78 in 1/7, sep(0)
| rep78 |
1. | . |
2. | . |
3. | . |
4. | . |
5. | . |
6. | 1 |
7. | 1 |

-- Brian Poi

* For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index