How to store a mean value in a local macro and then save it in another file?

Buras picture Buras · May 28, 2013 · Viewed 7.2k times · Source

I have a Stata file file1.dta and one of the variables is income. I need to calculate average_income, assign it to a local macro, and store in a different Stata file, New.dta.

I have tried the following in a do file:

#delimit;
clear;
set mem 700m;

use file1.dta;
local average_income = mean income; 

use New.dta;
gen avincome = average_income;

However, it does not work.

Answer

Nick Cox picture Nick Cox · May 28, 2013

This overlaps with your other post, namely How to retrieve data from multiple Stata files?. You don't say why you think

use file1.dta;
local average_income = mean income; 

will work, but the second line is just fantasy syntax. There are various ways to calculate the mean of a variable, the most common being to use summarize and pick up the mean from r(mean).

You should probably delete this question: it serves no long-term purpose.