Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[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 <austinnichols@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: egen and recode, from summary statistics, zero values [SEC: UNCLASSIFIED]
Date   Sun, 31 Jul 2011 17:16:59 -0400

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

su ADD_SUG if ADD_SUG>0
g t_ADD_SUG=max(ADD_SUG,r(min)/2)

On Sun, Jul 31, 2011 at 4:53 PM, Gosse, Michelle
<Michelle.Gosse@foodstandards.gov.au> 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 http://www.cpc.unc.edu/research/tools/data_analysis/sas_to_stata/sas_to_stata.html
>
> Cheers
> Michelle

*
*   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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index