Announcement

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

  • Replace missing data with mean or average for summative scale purposes

    Hi, I fully understand the issues with mean imputation, but I still need to use this as a quick solution to build proxy scales (not for publishable work). I have 65 variables and ~10k observations. Observations are named rf1, rf2, rf3...rf65. I've tried this:

    foreach var of varlist rf* {
    2. quietly sum 'var'
    3. replace 'var' = r(mean) if missing('var')
    4. }

    but I get the "invalid name r(198);" error message. How else could I approach this?

  • #2
    Hi Lis, I just have a quick sanity-check question before we really dig in - you use the back tick character below the tilde symbol (`) at the start of a local, right? From the code you provide here it looks like you surround your locals in single quotes - it should however be `var' not 'var', correct? Or does the full error look like this:

    Code:
    ' invalid name
    r(198);
    Last edited by Daniel Schaefer; 12 Jul 2022, 15:27.

    Comment


    • #3
      Otherwise, your solution seems to work with a very basic test case:

      Code:
      clear
      input float(rf1 rf2 rf3 rf4 rf5)
      1 1 5 1 3
      2 2 . . .
      3 3 3 . .
      4 . 2 . .
      5 5 1 1 .
      end
      
      foreach var of varlist rf* {
          quietly sum `var'
          replace `var' = r(mean) if missing(`var')
      }
      
      list, clean noob
      Code:
      . list, clean noob
      
          rf1    rf2    rf3   rf4   rf5  
            1      1      5     1     3  
            2      2   2.75     1     3  
            3      3      3     1     3  
            4   2.75      2     1     3  
            5      5      1     1     3

      Comment


      • #4
        You were right Daniel, I was the black tick mistake that kept my code from running correctly. Thank you so much, problem solved!

        Comment

        Working...
        X