Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: consistency of sort?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: consistency of sort?
Date   Mon, 20 Dec 2004 19:04:34 -0000

Further thought shows that my test with 
the auto data is no test, given that 
the auto dataset is already -sort-ed 
on foreign. 

Here is an update of -swor-. In due course, 
meaning March, this will be updated in 
the Stata Journal. 

*! 1.1.0  NJC 20 Dec 2004                            
*! 1.0.0  NJC 3 Nov 2000          (STB-59: dm86)
program swor, sort 
	version 8 
	local 0 `"=`0'"'
	syntax =/exp [if] [in] [, BY(varlist) Keep Generate(str) ]

	tempvar nobs random 
	marksample touse 
	qui count if `touse'
	local N = r(N)
	if `N' == 0 { 
		di as err "no observations to sample"
		exit 2000 
	} 	

	local exp = trim("`exp'")
	confirm integer number `exp'
	if `exp' < 0 { 
		di as err "# in sample must be at least 1" 
		exit 198 
	}
	else if `exp' > `N' { 
		di as err "`exp' exceeds number of pertinent observations `N'" 
		exit 198 
	} 	

	if "`keep'" != "" & "`generate'" == "" { 
		di as err "generate() required with keep" 
		exit 198 
	} 
	
	if "`generate'" != "" confirm new variable `generate'
	else tempvar generate  

	if "`by'" != "" { 
		if "`in'" != "" {
			di as err "in may not be combined with by"
			exit 190
		}
		sort `touse' `by', stable 
		qui by `touse' `by' : gen long `nobs' = _N if `touse' 
		su `nobs', meanonly 
		if `exp' > `r(min)' { 
			di as err "`exp' exceeds smallest group size `r(min)'"
			exit 198 
		} 	
	}
	
	if _caller() < 4.0 {
		gen float `random' = uniform0()
	}
	else	gen float `random' = uniform()
	
	sort `touse' `by' `random', stable 
	qui by `touse' `by' : gen byte `generate' = (_n <= `exp') * `touse' 

	if "`keep'" == "" drop if `generate' == 0 & `touse' 
end

Nick 
[email protected] 

Gabriel Demombynes
 
> There it is right there in the -sort- help file where
> I should have been able to find it myself. Thanks!
> 
> The original problem I had with -swor-, I see, was due
> to the use of -by- with multiple applications of
> -swor- to the same data. If you wanted to use -swor-
> and avoid the problem, you could edit swor.ado to add
> the stable option to the sort performed within the ado
> file. 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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