*! version 1.4.0 28july2010 program define scores version 9.0 gettoken newv 0 : 0, parse(" =") gettoken opt 0 : 0, parse("(") local opt = trim(subinstr("`opt'","=","",1)) gettoken vars 0 : 0, match(parns) syntax [if] [in] [, minvalid(integer 1) nv(integer 1) SCore(name) MINval(integer 0) MAXval(integer 0) Auto replace] marksample touse if `nv' != 1 { local minvalid = `nv' } if ("`score'"=="c") { local score = "centered" } if ("`score'"=="p") { local score = "pomp" } local 0="`vars'" syntax varlist(numeric) if subinword("`varlist'","`newv'"," ",.) != "`varlist'" { display as error "{hi:newvar (`newv')} may not be an element of {hi:varlist (`varlist')}" display "-scores- not executed" exit 498 } tempvar nvalid m_stat if inlist("`opt'","min","max","total","mean","sd")==0 { display "{hi:{it:=function(varlist)}} must specify one of {hi:min}, {hi:max}, {hi:total}, {hi:mean}, or {hi:sd}" display "-scores- not executed" exit 498 } if inlist("`score'","","z","centered","pomp")==0 { display "{hi:`score'} is an invalid argument of option {hi:score()}" display "-scores- not executed" exit 498 } if inlist("`score'","z","centered","pomp") & "`opt'"!="mean" { display "{hi:score(`score')} is only a valid option if {hi:{it:function(varlist)}} = {hi:mean}" display "-scores- not executed" exit 498 } if (`minvalid' < 1) { display "value specified in {hi:minvalid({it:#})} may not be less than 1" display "-scores- not executed" exit 498 } local k: word count `varlist' if (`minvalid' > `k') { display "value specified in {hi:minvalid({it:#})} may not be greater than" display "the number of variables specified in {it:{hi:function=(varlist)}} (=`k')" display "-scores- not executed" exit 498 } if ("`score'"=="pomp") { if "`auto'"=="auto" { foreach vname of varlist `varlist' { local vlab: value label `vname' if "`vlab'"=="" { di "Option {hi:auto} requires value labels of all variables of {hi:varlist}." di "{hi:`vname'} has no value labels." di "-scores- not executed" exit 498 } qui label list `vlab' local firstv: word 1 of `varlist' if "`firstv'"=="`vname'" { local minval = `r(min)' local maxval = `r(max)' } else { if `minval' != `r(min)' | `maxval' != `r(max)' { di "The range of labeled values of {hi:`vname'} differs from the range of labeled values of {hi:`firstv'}." di "-scores- not executed" exit 498 } } } } if (`minval'>=`maxval') { display "{hi:minval({it:#})} and {hi:maxval({it:#})} options should allow {hi:minval} to be less than {hi:maxval}" display "-scores- not executed" exit 498 } } if "`replace'"=="replace" { capture confirm new var `newv' if _rc!=0 { capture drop `newv' } } egen `nvalid'=rownonmiss(`varlist') if `touse' egen double `newv' = row`opt'(`varlist') if `touse' replace `newv' = cond(`nvalid'>=`minvalid',`newv', .) if "`score'" != "" { qui sum `newv' if `touse' if r(N) > 0 { if "`score'"=="z" { qui replace `newv' = (`newv'-r(mean))/r(sd) if r(sd)==0 { di "Warning: Standard deviation of {hi:`newv'} = 0," di "z-scores of {hi:`newv'} have been set to missing for all cases." } } else if "`score'"=="centered" { qui replace `newv' = `newv'-r(mean) } else if "`score'"=="pomp" { qui replace `newv' = 100*(`newv'-`minval')/(`maxval'-`minval') qui egen `m_stat' = rowmin(`varlist') if `touse' qui sum `m_stat' if `touse' drop `m_stat' if r(min)<`minval' { di "Warning: Value(s) of {hi:{it:varlist}} less than {hi:minval(`minval')}." } qui egen `m_stat' = rowmax(`varlist') if `touse' qui sum `m_stat' if `touse' if r(max)>`maxval' { di "Warning: Value(s) of {hi:{it:varlist}} greater than {hi:maxval(`maxval')}." } } } } qui sum `newv' if `touse' end