Hi guys,
This is my first time posting on Statalist. I am a doctor and I and use STATA to conduct medical research. I have recently been learning coding in STATA to help me perform statistics for different studies more efficiently, but I am not an expert by any means, and I apologise in advance if my terminology is incorrect throughout this post. Survival analysis and cox regression is key to medical research. I perform univariate cox regression over 50+ variables, before then using stepwise backwards multivariate cox regression to build a model.
When performing univariate cox regression, I used to run regress for each variable manually, and copy and paste each regression output into excel (which you can imagine is very tedious). I have since been trying to automate this process, and have since discovered foreach loops which has made this possible.I have been trying to get my results to automatically copy into an excel spreadsheet, to save me even more time. I have been using the outreg2 function to do this. However, this leads to data being displayed diagonally, rather than horizontally across a column as I would expect. I was wondering if anyone was able to help with formatting the output so that the data runs horizontally rather than diagonally, with variable names as the header for each column? I would prefer to have variable names listed down the first column with data running across a row, but I am unsure if this is possible with the outreg2 function.
I would really appreciate any help! If I haven’t formatted something correctly, please let me know. My code (scaled down for only 3 variables for simplicity) is enclosed below. I have attached images of my excel output which include my current excel output, the output I am trying to achieve and the output I would ideally want (but I am unsure if it is possible), respectively, from left to right.
Thanks in advance for your time and consideration.
Kind regards,
Duji

This is my first time posting on Statalist. I am a doctor and I and use STATA to conduct medical research. I have recently been learning coding in STATA to help me perform statistics for different studies more efficiently, but I am not an expert by any means, and I apologise in advance if my terminology is incorrect throughout this post. Survival analysis and cox regression is key to medical research. I perform univariate cox regression over 50+ variables, before then using stepwise backwards multivariate cox regression to build a model.
When performing univariate cox regression, I used to run regress for each variable manually, and copy and paste each regression output into excel (which you can imagine is very tedious). I have since been trying to automate this process, and have since discovered foreach loops which has made this possible.I have been trying to get my results to automatically copy into an excel spreadsheet, to save me even more time. I have been using the outreg2 function to do this. However, this leads to data being displayed diagonally, rather than horizontally across a column as I would expect. I was wondering if anyone was able to help with formatting the output so that the data runs horizontally rather than diagonally, with variable names as the header for each column? I would prefer to have variable names listed down the first column with data running across a row, but I am unsure if this is possible with the outreg2 function.
I would really appreciate any help! If I haven’t formatted something correctly, please let me know. My code (scaled down for only 3 variables for simplicity) is enclosed below. I have attached images of my excel output which include my current excel output, the output I am trying to achieve and the output I would ideally want (but I am unsure if it is possible), respectively, from left to right.
Thanks in advance for your time and consideration.
Kind regards,
Duji
Code:
foreach i in sex hb platelet inr { stcox `i' if final_dx == 1, robust outreg2 using booknew.xls, excel append stats(coef se pval ci) eform stnum(replace coef=exp(coef), replace se=coef*se) }
Comment