Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • "too few variables" foreach

    Hi,

    I'd like to recode (reverse) a list of 24 dichotomous variables (0/1).
    The following should illustrate the problem:

    Code:
    webuse auto, clear
    gen foreign_copy1 = foreign
    gen foreign_copy2 = foreign
    gen foreign_copy3 = foreign
    gen foreign_copy4 = foreign
    gen foreign_copy5 = foreign
    
    foreach v of var(foreign   ///
    foreign_copy1 ///
    foreign_copy2 ///
    foreign_copy3 ///
    foreign_copy4 ///
    foreign_copy5) {
        recode `var' (0 = 1 "named") (1 = 0 "not named"), gen(`var'_e)
    }
    Stata responds:
    "too few variables specified", same using wildcards:
    Code:
    foreach v of var(foreign foreign_copy*) {
        recode `var' (0 = 1 "named") (1 = 0 "not named"), gen(`var'_e)
    }
    What's wrong with the code?
    (The real - confidential - data of course have different distributions in each of the 24 variables, but the code stays the same,)

    Bye
    Thank you for reading (and some reply)
    Using Stata 16.1
    Extractions (-dataex-) of the data I'm working with is impossible, sorry!

  • #2
    I see no reason whatsoever for the parentheses in

    Code:
     
     foreach v of var(foreign foreign_copy*) {
    which aren't part of the allowed syntax. So, I suggest removing them.

    Code:
     
     foreach v of var foreign foreign_copy* {

    Comment


    • #3
      Note also that the recode command should refer to `v' rather than `var', since "var" is merely an abbreviation for varlist, while "v" is the subject of the foreach command.

      Comment


      • #4
        still the same error message
        Code:
        foreach v of var foreign  ///
        foreign_copy1 ///
        foreign_copy2 ///
        foreign_copy3 ///
        foreign_copy4 ///
        foreign_copy5 {
            recode `var' (0 = 1 "named") (1 = 0 "not named"), gen(`var'_e)
        }
        Thank you for reading (and some reply)
        Using Stata 16.1
        Extractions (-dataex-) of the data I'm working with is impossible, sorry!

        Comment


        • #5
          aaaaaah!
          outch! damn it

          thank you both!
          Thank you for reading (and some reply)
          Using Stata 16.1
          Extractions (-dataex-) of the data I'm working with is impossible, sorry!

          Comment


          • #6
            Let's back up. Please follow the FAQ on asking questions - provide Stata code in code delimiters, readable Stata output, and sample data using dataex. If we can replicate your problem, we can help you better. Using artificial data:
            Code:
            clear
            set obs 100
            forvalues i=1/5  {
            g t`i'=rnormal()
            g x`i'=t`i'>0
            }
            
            foreach v of var x1 x2 {
            set trace on
             recode `var' (0 = 1 "named") (1 = 0 "not named"), gen(`var'_e)
             }
            The set trace on shows the problem - the recode refers to `var' but `var' is not defined so the recode is translated as

            - recode `var' (0 = 1 "named") (1 = 0 "not named"), gen(`var'_e)
            = recode (0 = 1 "named") (1 = 0 "not named"), gen(_e)

            Code:
            clear
            set obs 100
            forvalues i=1/5  {
            g t`i'=rnormal()
            g x`i'=t`i'>0
            }
            
            foreach v of var x1 x2 {
            set trace on
             recode `v' (0 = 1 "named") (1 = 0 "not named"), gen(`v'_e)
             }
            works.

            Comment


            • #7
              Hi Phil.

              First of all thank you for your response!

              Sorry - I really don't get your point about:
              Let's back up. Please follow the FAQ on asking questions - provide Stata code in code delimiters, readable Stata output, and sample data using dataex. If we can replicate your problem, we can help you better. Using artificial data:
              Since "my" data must not be shared - as supposed with dataex -, I used the auto.dta to illustrate the same problem using a .dta usable here in "public".
              (The real - confidential - data of course have different distributions in each of the 24 variables, but the code stays the same,)
              So, I just did, what FAQ had told me, I should do (in my case of confidential data).


              Have a nice day
              Thank you for reading (and some reply)
              Using Stata 16.1
              Extractions (-dataex-) of the data I'm working with is impossible, sorry!

              Comment

              Working...
              X