Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: egen and recode, from summary statistics, zero values [SEC: UNCLASSIFIED]

 From Eric Booth To "" Subject Re: st: egen and recode, from summary statistics, zero values [SEC: UNCLASSIFIED] Date Sun, 31 Jul 2011 21:32:34 +0000

```<>

On Jul 31, 2011, at 3:53 PM, Gosse, Michelle wrote:

>
> I can set the value of min_add_sugar correctly for people who have non-zero added sugar (i.e. where r_add_sug = 1) by using:
>
> however, that means that people with r_add_sug = 0 have a missing value for min_add_sugar.
>
> I tried to fix that by running the following, but I get a syntax error:
> recode min_add_sugar = mean(min_add_sugar) if r_add_sug != 0 =exp not allowed r(101);
>
> and this does not work either:
> replace min_add_sugar = mean(min_add_sugar) if missing(min_add_sugar) unknown function mean() r(133);
>
> So I thought that I would generate a local variable to store the single value I need, and then generate or recode off that, but I can't make it work either. I was going to use:
>
> but I get a syntax error on the first line:
> local minaddsugar = min(ADD_SUG) if r_add_sug != 0 invalid syntax r(198);

Hi Michelle:

The main issue here is that you're trying to use mean() in the syntax for -replace- , -local-, or -recode-.  mean() is a -egen- function and does not work in any of those scenarios.   So separate your -egen, mean()- step and your -replace- or -recode- steps (-replace- is probably better here).

could be:

egen x = mean(min_add_sugar) if r_add_sug != 0
replace min_add_sugar = x if mi(min_add_sugar) & !mi(x)    //last part probably isn't necessary, but I'm overly cautious with my conditions

you mentioned using -sum- and macros, so here's an alternative approach using the macro:

qui su min_add_sugar, d

A couple of underlying issues in your post:  1) You mention the values for 'people' in the dataset, so it might be the case that you should be using a -by- or -bysort- prefix to -egen- these values by person/group (if your data are arranged as such -- you make reference to a previous post that contained information about your dataset, but I haven't read that post and that information isn't included in this thread --  don't expect everyone to remember your description of your data in a prior thread); 2) you mention trying the syntax:

ignoring the erroneous mean() part, your statement reduces to:

replace var = var if missing(var)

which could never produce changes, even if you are manipulating var in some way like:

replace var = var^2 if mi(var)

Consider,

sysuse auto, clear
replace mpg = mpg^2 if missing(mpg)

which produces no changes.

Your issues resulted in an 'invalid syntax' warning, so when you trigger this error, it's best to consult the legal syntax in the help file for these commands.  Functions, like mean(), are not mentioned in the syntax description for local, replace, or recode.

- Eric
______
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```