2

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.

Buras
  • 3,069
  • 28
  • 79
  • 126

2 Answers2

2

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.

Community
  • 1
  • 1
Nick Cox
  • 35,529
  • 6
  • 31
  • 47
2

One way to do this would be the following:

#delimit;
clear;
set mem 700m;

use file1.dta;
quietly: summarize income;
local average_income = r(mean); 

use New.dta;
gen avincome = `average_income';
  • Hi @stately_treasures thank you for reply. I tried and have the following error: . `gen avincome = average_income; average_income not found` – Buras May 27 '13 at 23:58
  • You need `\`average_income'` in the last line. That is a local macro and needs to be referred to as such. Otherwise Stata will look for a variable or scalar with that name, and will not find it. – Nick Cox May 28 '13 at 00:01
  • The bigger deal is the poster's other question http://stackoverflow.com/questions/16781571/how-to-retrieve-data-from-multiple-stata-files of how to do this for _several_ files. – Nick Cox May 28 '13 at 18:37