Bookmark and Share

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: how to keep labels when using foreach


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: how to keep labels when using foreach
Date   Wed, 13 Mar 2013 13:11:31 +0000

Maarten's solution of combining -collapse-s looks more attractive than this!

The problem appears to be

         gen str30 group=

where there is no right-hand side.

In addition, picking off the last character works with
-smoking_status-, and it will work with -education-,
-- but it won't work with -BMI- which qua string  is either one or two
characters.

         if substr("`xx'",1,7)=="m_agegr" {
             local val=substr("`xx'", -1,1)
             local label:label(m_agegr)  `val'
         }
         else if substr("`xx'",1,3)=="BMI" {
             local val=substr("`xx'", -6,.)
             local label:label(BMI) `val'
         }
         else if substr("`xx'",1,9)=="education" {
             local val=substr("`xx'", -1,1)
             local label:label(education)  `val'
        }
      else if substr("`xx'",1,7)=="smoking" {
             local val=substr("`xx'", -1,1)
             local label:label(smoking_status)  `val'
       }

On Wed, Mar 13, 2013 at 12:59 PM, anne broe <[email protected]> wrote:
> I have a few more variables than the two first posted.
> When I apply Nicks suggestion to my do file the following happens:
>
> 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         ///
>             BMI==0 BMI==18 BMI==25 BMI==30 BMI==35 BMI==40 education==1    ///
>             education==2 education==3 education==0 smoking_status==0
> smoking_status==1 smoking_status==2 smoking_status==3    {
>         use Work\Temp4, clear
>         display "`xx'"
>         keep if `xx'
>         duplicates drop newpnr, force
>         gen atc_yes = 1 if ga_atc==1
>         gen atc_no = 1 if ga_atc==0
>         if substr("`xx'",1,7)=="m_agegr" {
>             local val=substr("`xx'", -1,1)
>             local label:label(m_agegr)
>         `val'
>         }
>         if substr("`xx'",1,3)=="BMI" {
>             local val=substr("`xx'", -1,1)
>             local label:label(BMI)
>         `val'
>         }
>         if substr("`xx'",1,9)=="education" {
>             local val=substr("`xx'", -1,1)
>             local label:label(education)
>         `val'
>         }
>         if substr("`xx'",1,7)=="smoking" {
>             local val=substr("`xx'", -1,1)
>             local label:label(smoking_status)
>         `val'
>         }
>         collapse (sum) atc_yes atc_no
>         gen str30 group=
>         label var group "`l`v''"
>         if "`xx'" != "m_agegr==0" append using Work\Q4
>     save Work\Q4, replace
>     }
>
> This is the error message i get:
>
> m_agegr==0
> (xxxx observations deleted)
>
> Duplicates in terms of newpnr
>
> (xx observations deleted)
> (xx missing values generated)
> (x missing values generated)
> invalid syntax
> r(198);
>
>
> Since I cant find the error in the do file, maybe someone else can help me :)
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index