Announcement

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

  • Creating a loop that can calculate a mean of 21 variables, while ignoring variables with missing values

    I am trying to create a loop that can calculate a mean score for 21 variables, while ignoring missing values and values greater than 4.

  • #2
    Code:
    foreach  x of varlist whatever {
         su `x' if `x' < 4, meanonly
         di "`x'{col 33}" %5.0f  r(N)   %10.1f r(mean)
    }
    Notes:

    For whatever you need to supply your own varlist. We can't tell you what it is.

    For 33, 5.0, 10.1 you may well find other numbers more appropriate to your data.

    Missing values get ignored any way.

    Results after

    Code:
    sysuse auto, clear
    
    price                               0         .
    mpg                                 0         .
    rep78                              40       2.7
    headroom                           59       2.7
    trunk                               0         .
    weight                              0         .
    length                              0         .
    turn                                0         .
    displacement                        0         .
    gear_ratio                         74       3.0
    foreign                            74       0.3

    Comment


    • #3
      Welcome to Statalist.

      Your question has multiple interpretations. Nick's interpretation was that you want to calculate the means across all observations for 21 variables.

      Perhaps you want the mean, within each observation, of 21 variables. For that, something like the following might start you on your way.
      Code:
      generate avg = 0
      generate nvar = 0
      foreach x of varlist whatever {
          replace avg = avg + `x' if `x'<=4
          replace nvar = nvar+1 if `x'<=4
      }
      replace avg = avg/nvar

      Comment


      • #4
        Originally posted by William Lisowski View Post
        Welcome to Statalist.

        Your question has multiple interpretations. Nick's interpretation was that you want to calculate the means across all observations for 21 variables.

        Perhaps you want the mean, within each observation, of 21 variables. For that, something like the following might start you on your way.
        Code:
        generate avg = 0
        generate nvar = 0
        foreach x of varlist whatever {
        replace avg = avg + `x' if `x'<=4
        replace nvar = nvar+1 if `x'<=4
        }
        replace avg = avg/nvar
        Thank you very much William and you are absolutely right about the ambiguity of my question. I was actually asking about the exact same question that you have answered and the code has run perfectly! thank you very much

        Comment


        • #5
          Just one more question, assuming I have for instance questions 1-35, but I only need to run this command for questions 1-21, how do I specify this in the code?

          Comment


          • #6
            That is just a matter of specifying a different varlist and quite possibly new variable names.

            You've yet to give us any data example or even a hint at your variable names or the exact syntax you've been using, so please do that if this example is not enough. https://www.statalist.org/forums/help#stata remains expected reading.

            Code:
            generate newavg = 0
            generate newn = 0
            foreach x of varlist q1-q21 {
                replace newavg = newavg + `x' if `x'<=4
                replace newn = newn + 1 if `x'<=4
            }
            replace newavg = newavg/mewn

            Comment


            • #7
              Originally posted by Nick Cox View Post
              That is just a matter of specifying a different varlist and quite possibly new variable names.

              You've yet to give us any data example or even a hint at your variable names or the exact syntax you've been using, so please do that if this example is not enough. https://www.statalist.org/forums/help#stata remains expected reading.

              Code:
              generate newavg = 0
              generate newn = 0
              foreach x of varlist q1-q21 {
              replace newavg = newavg + `x' if `x'<=4
              replace newn = newn + 1 if `x'<=4
              }
              replace newavg = newavg/mewn
              This has worked perfectly! Many thanks Nick. My final syntax was

              generate Domain1 = 0
              generate nvar = 0
              foreach x of varlist QN1-QN21{
              replace Domain1 =Domain1 + `x' if `x'<=3
              replace nvar = nvar+1 if `x'<=3
              }
              replace Domain1 = Domain1/nvar

              Comment

              Working...
              X