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'")
end
or
program keysortrows
version 9
syntax varlist(min=2 numeric) [if] [in] , key(varlist numeric
min=2)
<same stuff>
end
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.
Nick
[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'")
end
mata:
void _keysortrower(string scalar varlist,string scalar key,string scalar
touse)
{
real matrix x
st_view(X,.,tokens(varlist),touse)
st_view(Y,.,tokens(key), touse)
for(i=1;i<=rows(X);i++){
x=(X[i,.]\Y[i,.])
x=sort(x',2)
x=x'
X[i,.]=x[1,.]
Y[i,.]=x[2,.]
}
}
end
*
* 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/
*
* 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/