This is more out of my own curiosity than trying to solve a problem.
I will occasionally use "by x:" or "bysort x:" with a command. This does not work for all commands. For example, "bysort x: mean y" will return the error "mean may not be combined with by"
And when this happens, I grunt and sigh and write a simple for loop of the format
levelsof x, l(xvalues)
foreach i in `xvalues' {
mean y if x == `i'
}
I've always imagined that the "by" prefix is basically just a shorthand for that loop that I wrote (with some bells and whistles tacked on top). But it must not be, because if it were, there's no reason why any command that accepts "if" would be unable to handle "by." But there are several commands that can't handle "by".
So what is actually going on behind the scenes with "by"? How is it structured that it can't do this seemingly simple thing?
I will occasionally use "by x:" or "bysort x:" with a command. This does not work for all commands. For example, "bysort x: mean y" will return the error "mean may not be combined with by"
And when this happens, I grunt and sigh and write a simple for loop of the format
levelsof x, l(xvalues)
foreach i in `xvalues' {
mean y if x == `i'
}
I've always imagined that the "by" prefix is basically just a shorthand for that loop that I wrote (with some bells and whistles tacked on top). But it must not be, because if it were, there's no reason why any command that accepts "if" would be unable to handle "by." But there are several commands that can't handle "by".
So what is actually going on behind the scenes with "by"? How is it structured that it can't do this seemingly simple thing?
Comment