Dear Experts,
I am trying to calculate z-scores over a sum of variables sorted by month.
If only one of the variables is missing, the whole z-score for that row will be missing, so I am trying to include only those variables in the std-function that are nonmissing.
I managed to create a string which contains the nonmissing variables per row with the following code but I am unable to "insert" this string into the std-function.
Does anyone know how to "insert" this string as the sum of variables into the std-function? Other approaches to my problem are offcourse also highly welcomed.
Thanks in advance!
I am trying to calculate z-scores over a sum of variables sorted by month.
Code:
bysort month_id: egen z_profitability=std(var1+var2+var3+var4+var5)
I managed to create a string which contains the nonmissing variables per row with the following code but I am unable to "insert" this string into the std-function.
Code:
gen nonmissing_vars = "" foreach var of local vars { replace nonmissing_vars = nonmissing_vars + "`var'+" if !missing(`var') } replace nonmissing_vars = substr(nonmissing_vars, 1, length(nonmissing_vars) - 1) gen z_profitability = . forval i = 1/`=_N' { local curr_var_list = nonmissing_vars[`i'] egen z_profitability[`i'] = std(inlist("`curr_var_list'"), missing) }
Thanks in advance!
Comment