Search code examples
statalocal-variablesstata-macros

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


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.


Solution

  • 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';