Announcement

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

  • Looping egen mean weight

    Hi all,

    I am supposed to get a single mean_weight variable.

    Original egen code: egen weight_mean = rowmean(weight1 weight2 weight3)

    Code I used:
    foreach var of varlist weight1 weight2 weight3 {
    su `var', d
    egen `var'_mean = rowmean(`var')
    }

    I am not sure where I went wrong, but I ended up with three mean_weight variables (mean_weight1, mean_weight2, mean_weight3) after using the code above. I am supposed to get mean_weight alone.
    Last edited by Goh jedrek; 06 Apr 2021, 21:25.

  • #2
    You might want to try and explain better what is the problem. You seem to know how to accomplish the task:
    Code:
    egen weight_mean = rowmean(weight1 weight2 weight3)
    does what you want. So then what is the puzzle about?

    This command is equivalent to

    Code:
    gen myweight_mean = (weight1 + weight2 + weight3)/3
    except that if you have any missing values, egen will disregard them and calculate a mean based on the nonmissing values, and the gen will generate a missing whenever any of the three is missing.

    Comment


    • #3
      Originally posted by Joro Kolev View Post
      You might want to try and explain better what is the problem. You seem to know how to accomplish the task:
      Code:
      egen weight_mean = rowmean(weight1 weight2 weight3)
      does what you want. So then what is the puzzle about?

      This command is equivalent to

      Code:
      gen myweight_mean = (weight1 + weight2 + weight3)/3
      except that if you have any missing values, egen will disregard them and calculate a mean based on the nonmissing values, and the gen will generate a missing whenever any of the three is missing.
      Hi Joro, the problem is with my loop code and not the egen code.

      What I get from the loop code, is that I get weight_mean1 weight_mean2 weight_mean3, without any division of 3. I end up getting 3 new variables with the same initial weight values.

      So I think there is an issue with my loop code.
      Attached Files
      Last edited by Goh jedrek; 07 Apr 2021, 00:57.

      Comment


      • #4
        You wrote code that said that -- for each of the variables, calculate a row mean of that one variable. Stata did what you asked for. The mean of three variables requires only one statement. If for some reason you wanted to write it as a loop that could be

        Code:
        foreach v in "weight1 weight2 weight3"  {
            egen mean = rowmean(`v')
        }
        except that there is no obvjous reason to do that.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          You wrote code that said that -- for each of the variables, calculate a row mean of that one variable. Stata did what you asked for. The mean of three variables requires only one statement. If for some reason you wanted to write it as a loop that could be

          Code:
          foreach v in "weight1 weight2 weight3" {
          egen mean = rowmean(`v')
          }
          except that there is no obvjous reason to do that.
          Thanks for resolving my dilemma!!

          Comment

          Working...
          X