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 on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: how to keep labels when using foreach


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: how to keep labels when using foreach
Date   Wed, 13 Mar 2013 12:11:03 +0000

Don't blame -foreach- here. You lose value labels here because they
are not needed and not asked for as far as your new variables are
concerned.

As I understand it, your new dataset includes only these variables

group
atc_yes
atc_no

Your predictors are some male age variable, I guess, and
-smoking_status-, which seems eminently scrutable as "smoking status".
Assuming you have value labels for the latter only, your code might be
extended in this way:

foreach xx in m_agegr==0 m_agegr==15 m_agegr==20 m_agegr==25
m_agegr==30 m_agegr==35 m_agegr==40 m_agegr==45 smoking_status==0
smoking_status==1 smoking_status==2 smoking_status==3    {
         use Work\Temp4, clear
         display "`xx'"
         keep if `xx'
         gen atc_yes = 1 if ga_atc==1
         gen atc_no = 1 if ga_atc==0

         // new
         if substr("`xx'", 1, 7) == "smoking" {
                  local val = substr("`xx'", -1, 1)
                  local label : label (smoking_status) `val'
         }
         else local label ""

         collapse (sum) atc_yes atc_no
         gen str30 group="`xx'"

         // new
         gen str30 label = "`label'"

        if "`xx'" != "m_agegr==0" append using Work\Q4
        save Work\Q4, replace
 }

I see that I would be excluded from your dataset on ageist grounds.

Nick

On Wed, Mar 13, 2013 at 11:38 AM, anne broe <annebroe@gmail.com> wrote:

> Im using -foreach- but seem to loose all my value labels when doing so.
> How can I keep the labels in the output from -foreach-?
>
> this is my do file
>
> foreach xx in m_agegr==0 m_agegr==15 m_agegr==20 m_agegr==25
> m_agegr==30 m_agegr==35 m_agegr==40 m_agegr==45 smoking_status==0
> smoking_status==1 smoking_status==2 smoking_status==3    {
>         use Work\Temp4, clear
>         display "`xx'"
>         keep if `xx'
>         gen atc_yes = 1 if ga_atc==1
>         gen atc_no = 1 if ga_atc==0
>         collapse (sum) atc_yes atc_no
>         gen str30 group="`xx'"
>         if "`xx'" != "m_agegr==0" append using Work\Q4
>     save Work\Q4, replace
>     }
>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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