Bookmark and Share

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

From   Austin Nichols <>
Subject   Re: st: egen and recode, from summary statistics, zero values [SEC: UNCLASSIFIED]
Date   Sun, 31 Jul 2011 17:16:59 -0400

Michelle <>:
Have a look at [R] saved results, linked from
. help return
Also see
    Manual: [P] return, [R] saved results
and try:

g t_ADD_SUG=max(ADD_SUG,r(min)/2)

On Sun, Jul 31, 2011 at 4:53 PM, Gosse, Michelle
<> wrote:
> Greetings again,
> I have a new problem I have been trying to solve with my dataset. I am using Stata 11.2 SE in a networked environment.
> I'm still working with my added sugar variable (ADD_SUG). The end outcome is that I need a variable that contains either the actual added sugar intake, or half the non-zero minimum value if the added sugar intake is 0.
> Following the SAS code I am translating, I need to find the minimum value of ADD_SUG (excluding zero intakes), and then create a new variable that contains that minimum value for all people in my data (to copy the SAS code I am translating). I have called that new variable min_add_sugar, and the variable r_add_sug is a binary 0/1 variable (0 = person has no added sugar, 1 = person has added sugar).
> I ran this to find the minimum value:
> summarize ADD_SUG if r_add_sug != 0
> and that worked perfectly.
> 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:
> egen min_add_sugar = min(ADD_SUG) if r_add_sug != 0
> 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:
> local minaddsugar = min(ADD_SUG) if r_add_sug != 0 gen min_add_sugar = minaddsugar
> but I get a syntax error on the first line:
> local minaddsugar = min(ADD_SUG) if r_add_sug != 0 invalid syntax r(198);
> Basically if I could hold (half) the minimum non-zero value for added sugar in memory and then operate on that to create my new "corrected" added sugar variable, that would be the best outcome for me.
> How can I do this? In SAS, one can output the value of a function and then operate on that output - I can't seem to do that in Stata.
> In addition, if anyone could also suggest a book or website that would be helpful, I would also appreciate that advice. I'm finding this site helpful, but I need examples more similar to my situation
> Cheers
> Michelle

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index