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

st: RE: I did it! sorting with a key.

From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: I did it! sorting with a key.
Date   Fri, 12 Sep 2008 15:43:51 +0100

Dear Ashim: 

"Guys" at least in some quarters connotes males only and thus might be
read as sexist. 

I have some comments on your program: 

0. The exact purpose of the program needs to be documented in a help
file. Without that readers have to guess at precisely what is intended. 

1. You make -key()- optional, but your program will I think fail if no
-key()- is provided. As the whole point of the program is to use a key,
that option should be compulsory. 

2. The use of -st_view()- within your Mata code implies that both your
main -varlist- and the argument of -key()- should be varlists specifying
only numeric variables. Your program will fail otherwise. It is best to
trap any string variables early. If you ever wish to sort string
variables, or with string variables, you would need to generalise the
program or to write another. 

3. That being so, the option -strok- is a distraction on -marksample-.
(It is irrelevant in any case given -novarlist-.) 

4. As it stands, your -marksample- always generates 1s for every
observation. It is thus redundant as it stands, although equally it does
no harm. The main point of using -marksample- would thus be if you
decided to support -if- and -in- as well. 

5. There is no check in your program that the number of variables in
-varlist- and -key()- is the same. 

Answering also another question of yours, your program could thus start 

program keysortrows
    version 9
    syntax varlist(min=2 numeric) ,  key(varlist numeric min=2)	

    marksample touse, novarlist

    local nv : word count `varlist' 
    local nk : word count `key' 
    if `nv' != `nk' {
         di as err "`nv' variables, but `nk' key variables"
	   exit 198 

    mata:_keysortrower("`varlist'", "`key'","`touse'")


program keysortrows
    version 9
    syntax varlist(min=2 numeric) [if] [in] ,  key(varlist numeric

    <same stuff> 

depending on the answer to 4. 

I have not checked this code. 

I have not looked in detail at your Mata code. I will if I get some time
later to do it. 

[email protected] 

Ashim Kapoor

I wrote a code which is the SAME as sortrows but it sorts with a KEY.

Here is the ado file :-

 program define keysortrows
    version 9
    syntax varlist(min=2) ,         ///
    [                               ///
        key(string)		    ///	

    marksample touse, strok novarlist

    mata:_keysortrower("`varlist'", "`key'","`touse'")

void _keysortrower(string scalar varlist,string scalar key,string scalar

	 real matrix x

	 st_view(Y,.,tokens(key), touse)



*   For searches and help try:

*   For searches and help try:

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