I am calculating a few portfolio performance metrics that I need to output in an Excel file. The Sharpe ratio (SR here) is one of them. I need to maintain the order (sorting done with "id" manually, mkt and BTC always appearing first) of the variables I have in order to output each metric to the corresponding variable in Excel. The SR is the deciding factor for me to keep a variable or not. I would like to automate this criteria in my Stata code in a way that I do not have to change the string name every time I have new input data (I have +100 variables and their name changes). If the SR is positive, then I keep the variable, otherwise I drop if from the table. Here is what I have manually programmed. Would you be able to help me automate it?
Code:
clear all
input SR_cpd SR_cpdpf SR_cpdpm
-.04414876 .08082671 .65427268
end
gen SR_BTC = runiform()
gen SR_mkt= runiform()
*order w_date $SR_strat
*collapse (mean) $SR_strat
*order $SR_strat
gen id = 1
reshape long SR_ , i(id) j(SR) string
sort SR_
* Drop cpd because SR_ is negative:
drop if (SR_<0 & SR!="BTC" & SR!="mkt")
replace id = 0 if SR=="BTC"
replace id = 0.5 if SR=="mkt"
sort id SR_
* I do not want to have to change the string name is every time I backtest variables with different names:
replace id = 1 if SR=="cpdpf"
replace id = 2 if SR=="cpdpm"
sort id
drop id SR
rename (SR_ ) (SR )
putexcel set 0.Results.xlsx, sheet("Table 1") modify
putexcel K2="SR"
format SR %9.3g
export excel using 0.Results.xlsx , sheetmodify sheet("Table 1") cell(K4) keepcellfmt

Comment