I need to generate a new variable that is a standardized value of another variable but by a group (SAT scores by year). I calculated it using the following code:
egen mean_sat = mean(sat), by(year)
egen sd_sat = sd(sat), by(year)
gen std_dat = (sat - mean_sat) / sd_sat
Is there another more direct way to do that? I tried the following with no success...
. by year, sort : egen float std_SAT = std(sat)
egen ... std() may not be combined with by
r(190);
. egen std_SAT = std(sat), by(year)
egen ... std() may not be combined with by
At present, the officially written egen
function std()
does not support operations by
. I can't identify a statistical or computational reason for that, but it is well documented. (Why you need luck to get past a documented limitation I don't understand.)
In principle, any user could write their own egen
function to support what you want to be implemented in a one-line call. In practice, no one seems bothered enough to write it given the easy work-around that you have used. In practice, these things get written when someone gets irritated at the need for typing three lines of code repeatedly. A much more positive reason why the code you cite is useful is that statistically you should usually want to keep track of means and standard deviations any way.
EDIT 20 July 2020
Update to Stata 16.1
update 30jun2020
egen
has the following updates:c.
egen
functionstd()
now allowsby
varlist:
. When used withby
varlist:
, values are standardized within each group defined by varlist. The option specifying a value for the standard deviation has been renamedsd()
(the old option namestd()
continues to work as well).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With