Standardize a variable by group in Stata

OAM

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
Nick Cox

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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Stata estpost esttab: Generate table with mean of variable split by year and group

From Dev

Computing Unemployment rates by education group from an indicator variable (Stata)

From Dev

Standardize group names using a vector of possible matches

From Dev

Recoding Categorical Variable (Stata)

From Dev

Time aligned variable in stata

From Dev

In Stata, how can I group coefplot's means across categorical variable?

From Dev

Stata - Add Prefix to Variable Values

From Dev

Stata reference variable by column number

From Java

Loop for particular values of variable in Stata

From Dev

Replace $ in Stata variable labels with \textdollar

From Dev

Stata interaction variable previous year

From Dev

Stata interaction variable base year

From Dev

Labelling Subgroups in a String variable (Stata)

From Dev

Stata: Concatenate string variable on by condition

From Dev

Stata - Add Prefix to Variable Values

From Dev

Stata: Count a variable by another one?

From Dev

Stata reference variable by column number

From Dev

Stata does not replace variable value

From Dev

Create a variable by dividing a variable by IQR in Stata

From Dev

Stata function equivalent to group_concat

From Dev

Stata: using egen group() to create unique identifiers

From Dev

Stata -- predict after regression by group_id

From Dev

Stata putexcel summary statistics by group to MS Excel

From Dev

Creating a vector with unique observations from a variable in Stata

From Java

Inserting variable's values into a matrix - Stata

From Dev

Generate difference between observations as new variable in Stata

From Dev

Stata: Systematically replace characters in a string variable

From Dev

How to access and manipulate strings in Stata variable labels?

From Dev

Convert odd Stata string variable to date

Related Related

  1. 1

    Stata estpost esttab: Generate table with mean of variable split by year and group

  2. 2

    Computing Unemployment rates by education group from an indicator variable (Stata)

  3. 3

    Standardize group names using a vector of possible matches

  4. 4

    Recoding Categorical Variable (Stata)

  5. 5

    Time aligned variable in stata

  6. 6

    In Stata, how can I group coefplot's means across categorical variable?

  7. 7

    Stata - Add Prefix to Variable Values

  8. 8

    Stata reference variable by column number

  9. 9

    Loop for particular values of variable in Stata

  10. 10

    Replace $ in Stata variable labels with \textdollar

  11. 11

    Stata interaction variable previous year

  12. 12

    Stata interaction variable base year

  13. 13

    Labelling Subgroups in a String variable (Stata)

  14. 14

    Stata: Concatenate string variable on by condition

  15. 15

    Stata - Add Prefix to Variable Values

  16. 16

    Stata: Count a variable by another one?

  17. 17

    Stata reference variable by column number

  18. 18

    Stata does not replace variable value

  19. 19

    Create a variable by dividing a variable by IQR in Stata

  20. 20

    Stata function equivalent to group_concat

  21. 21

    Stata: using egen group() to create unique identifiers

  22. 22

    Stata -- predict after regression by group_id

  23. 23

    Stata putexcel summary statistics by group to MS Excel

  24. 24

    Creating a vector with unique observations from a variable in Stata

  25. 25

    Inserting variable's values into a matrix - Stata

  26. 26

    Generate difference between observations as new variable in Stata

  27. 27

    Stata: Systematically replace characters in a string variable

  28. 28

    How to access and manipulate strings in Stata variable labels?

  29. 29

    Convert odd Stata string variable to date

HotTag

Archive