-
Notifications
You must be signed in to change notification settings - Fork 11
/
zscore_by_grp.do
47 lines (39 loc) · 1.61 KB
/
zscore_by_grp.do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Demonstrate creation of zscore by group.
// This example standardizes the variable fte "Full-time equivalent enrollment"
// and also variable cindon "Price for in-district students living on campus"
// by state, sector, and year.
// This file demonstrates using the output produced by:
// https://raw.githubusercontent.com/adamrossnelson/StataIPEDSAll/master/IPEDSDirInfo02to16.do
use IPEDSDirInfo02to16.dta, clear
// Make list of variables to work with.
local thevars fte cindon
// Declare variables for later use. In this case it is necessary to declare
// variables because the loop below which creates zscores relative to group
// members must iteratively reference grpz_`varname'. Thus, it is necssary to
// use replace instead of gen in the subsequent loop.
foreach varname in `thevars' {
gen grpz_`varname' = .
}
// Loop through varlist to apply standardized zscores for each variable.
// Store standardized score in grpz_`varname'.
levelsof isYr, local(yname)
levelsof fips, local(stname)
levelsof sector, local(secname)
foreach y of local yname {
foreach st of local stname {
foreach sec of local secname {
foreach varname in `thevars' {
local ifcond = "if isYr == `y' & fips == `st' & sector == `sec'"
sum `varname' `ifcond'
if r(mean) > 0 & r(mean) < . {
replace grpz_`varname' = (`varname' - `r(mean)')/`r(sd)' `ifcond'
}
}
}
}
}
// Check results. Mean of the standardized score within each should be zero.
bysort sector: sum grpz_fte if stabbr == "WI"
bysort sector: sum grpz_fte if stabbr == "FL"
bysort sector: sum grpz_fte if stabbr == "TX"
bysort sector: sum grpz_fte if stabbr == "CA"