Announcement

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

  • problem with foreach only looping once

    Any help would be very much appreciated. I have panel data - ids of each individual in one column, years in the next column, and variables in each following column. The variables are how many books they read and how many hours of TV they watched, by year.
    I would like to generate the mean, and standard deviations for these variables, between 2005 and 2010.
    foreach var of varlist booksread tvwatched {
    bysort id: egen `var'_mean2005to2010=mean(`var') if (year>=2005 & year<=2010)
    drop `var'_mean2005to2010 if !(year>=2005 & year<=2010)
    }

    This loops once, generates the booksread and then does not work for tvwatched. Instead I get 'invalid syntax r(198)', after the booksread has been generated. I can't figure out why it works only for the first pass. Help would be hugely appreciated

    p.s. this is a very much simplified example, I have 50 variables (hence why I'm not doing it manually), but it still doesn't work even when reduced to 2.

  • #2
    At first sight, I cannot figure out the source of your problem. Can you try
    Code:
    foreach var in booksread tvwatched {
    instead? It also helps if you could provide a replication code using a simple data set such as sysuse auto.

    Comment


    • #3
      Originally posted by shem shen View Post
      At first sight, I cannot figure out the source of your problem. Can you try
      Code:
      foreach var in booksread tvwatched {
      instead? It also helps if you could provide a replication code using a simple data set such as sysuse auto.
      Thank you very much for the quick response - I tried your suggestion and it still doesn't work. The dataset looks like
      id year books tv
      1 2005 4 3
      1 2006 5 2
      1 2007 6 1
      1 2008 7 1
      2 2005 1 2
      2 2006 2 3
      2 2007 3 4
      2 2008 4 5
      3 2005 1 1
      3 2006 2 2
      3 2007 4 3
      3 2008 4 4
      4 2006 5 8
      4 2008 5 8
      Could it be related to the "bysort" part

      Comment


      • #4
        The problem is you're specifying that a variable be dropped, but the if condition is based on observations. I'm not entirely sure what you're trying to achieve with the drop command, but you can't combine -drop var- with observations in the if condition. You can do either -drop if !(year>=2005 & year<=2010)- or -drop `var'_mean2005to2010-, but not both together. I can't imagine you want the latter command, because that just drops the variable you just created.
        Last edited by Ali Atia; 18 Apr 2021, 17:43.

        Comment


        • #5
          You can identify the reason by "set trace on".

          Code:
          sysuse auto,clear
          set trace on
          foreach var of varlist price mpg {
          bysort make: egen `var'_mean2005to2010=mean(`var') if (turn>=30 & turn<=40)
          drop `var'_mean2005to2010 if !(turn>=30 & turn<=40)
          }
          You cannot use variable and if condition simultaneously after drop. So "drop `var'_mean2005to2010 if !(year>=2005 & year<=2010)" is the invalid syntax. Here, I guess you want to set the values of `var'_mean2005to2010 to missing among observations outside of the range (year>=2005 & year<=2010). If this is the case, you don't need to do anything (because you already applied the if condition after egen). So you can just remove the drop command.

          Comment


          • #6
            Originally posted by Ali Atia View Post
            The problem is you're specifying that a variable be dropped, but the if condition is based on observations. I'm not entirely sure what you're trying to achieve with the drop command, but you can't combine -drop var- with observations in the if condition. You can do either -drop if !(year>=2005 & year<=2010)- or -drop `var'_mean2005to2010-, but not both together. I can't imagine you want the latter command, because that just drops the variable you just created.
            Ali - thank you very very much indeed! what an irritating mistake...

            Comment

            Working...
            X