This code fragment describes how to write results from Stata to an external text file. The text file is tab separated and can be read by either a text editor or a spreadsheet program such as Excel. The image below shows what the output will look like this when opened in a spreadsheet. In this example, the table includes information on three schools. The values in the table are the number and percent of students who at each school who fall into the highest 10th percentile on various measures The four .
local rowvar school local colvars active support attend know capture file close myfile file open myfile using "results.tab", write replace * write the header file write myfile " " _tab "N*" _tab foreach var of varlist `colvars' { local `var'_lab: variable label `var' file write myfile "``var'_lab' N(%)" _tab } file write myfile _n local label : variable label `rowvar' file write myfile "`label'" _n levelsof `rowvar', local(a) foreach nl of local a { quietly count if `rowvar' ==`nl' local t = r(N) local varlab : label `rowvar' `nl' file write myfile "`varlab'" _tab (r(N)) _tab foreach var of varlist `colvars' { quietly count if `rowvar' ==`nl' & `var'==1 local mycount = r(N) local p=(`mycount'/`t')*100 file write myfile (`mycount') " (" %2.0f (`p') "%)" _tab } file write myfile _n } file close myfile