Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[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
"Gosse, Michelle" <[email protected]>
To
"[email protected]" <[email protected]>
Subject
RE: st: egen and recode, from summary statistics, zero values [SEC: UNCLASSIFIED]
Date
Mon, 1 Aug 2011 07:52:41 +1000
Thanks to everyone for their fast and helpful responses.
I've used Austin's code below and it works perfectly. I'll keep a note of the other responses too, as a range of tools is always helpful. :)
Cheers
Michelle
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Austin Nichols
Sent: Monday, 1 August 2011 9:17 AM
To: [email protected]
Subject: Re: st: egen and recode, from summary statistics, zero values [SEC: UNCLASSIFIED]
Michelle <[email protected]>:
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 <[email protected]> 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_t
> o_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/
UNCLASSIFIED
**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.
This footnote also confirms that this email message has been swept by MIMEsweeper for the presence of computer viruses.
**********************************************************************
*
* 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/