Question

How can I outsheet a local value in stata I am trying using the below commands but it gives an error variable local not found in Stata 12

sysuse auto
summ price,de
outsheet local p1 local p99   using "range\range.csv", replace c

I have also tried

 outsheet r(p1) r(p99)   using "range\range.csv", replace c 

but error factor variables and time-series operators not allowed

Was it helpful?

Solution

A simple way to export a few local macro values into small csv files is through writing a text file with commas and renaming it to csv at the end. With your example you could try:

sysuse auto
file open TABLES using "ranges.txt", write replace //create temporary text file
file write TABLES "p1, p99" _n // columns headers
summ price,de
file write TABLES (r(p1)) "," (r(p99))  _n // write locals separated by commas into the text file
file close TABLES

copy ranges.txt ranges.csv, replace //change extension to csv
rm ranges.txt //remove text file

If you want to repeat the process for every level of rep78:

sysuse auto
levelsof rep78, local(levels) 
file open TABLES using "ranges.txt", write replace //create temporary text file
file write TABLES "level of rep78, p1, p99" _n // columns headers

foreach i of local levels {
summ price if rep78==`i',de 
file write TABLES "`i', `r(p1)' , `r(p99)'"_n 
}
file close TABLES
copy ranges.txt ranges.csv, replace //change extension to csv
rm ranges.txt //remove text file

OTHER TIPS

outsheet is for exporting data as variables. The only way to use outsheet to export anything not associated with a variable is put that thing into a variable. Thus your syntax is absolutely not going to work as none of the elements you referenced are variable names.

After summarize, detail results like the 1st and 99th percentiles are accessible in saved results like r(p1) and r(p99), and they can be referred to using a syntax resembling that for local macros, but nevertheless such saved results are not local macros.

If you back up and tell us your real question, you will be likely to get more positive advice. If you want to export summarize results to a spreadsheet, methods start with copy and paste.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top