Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standardize a variable by group in Stata

Tags:

stata

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
like image 536
OAM Avatar asked Sep 30 '22 00:09

OAM


1 Answers

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

  1. egen has the following updates:

    c. egen function std() now allows by varlist:. When used with by varlist:, values are standardized within each group defined by varlist. The option specifying a value for the standard deviation has been renamed sd() (the old option name std() continues to work as well).

like image 185
Nick Cox Avatar answered Oct 04 '22 03:10

Nick Cox