How would I export results of the code:
forvalues i = 1/100 {
table HP`i'_quintile, by(HP`i'_quintile) c(mean KG`i')
}
Thanks
clear // Simulate some data in wide format. This may or may // not correspond to the structure the OP has. set obs 1400 gen int id = _n forval i = 1/140 { gen HP`i' = 100 * runiform() gen KG`i' = rnormal(0,1) } // // Change to long format makes this easier. reshape long HP KG, i(id) j(month) // // Categorize HP into quintiles within month. // Note that the -by- prefix doesn't work with -xtile-, which makes this harder. gen price_quintile = . forval i = 1/140 { if (mod(`i',10) == 0) { // entertainment display "Finished month `i'" } xtile temp = HP if month == `i', nq(5) quiet replace price_quintile = temp if month == `i' drop temp } // // -collapse- gives a dataset of means within month and price_quintile, // which can then be exported to Excel collapse (mean) mean_month = KG , by(month price_quintile) describe // just to show what has happened // // OP wants wide format for results. reshape wide mean_month, i(price_quintile) j(month) // export excel using "myfile.xls", sheetreplace firstrow(variables)
Comment