Announcement

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

  • Create a new variable indicating if all variables in the dataset have the missing values

    Hello,

    I have been trying to create a new variable called truemiss to see whether or not ALL the variables in the dataset have missing values. There are 45 variables in the dataset. I first assigned zero to truemiss. Then I used loop to go through each variable, and assign zero to truemiss if the variable is missing (indicated by "") ; otherwise, assign 1 to the truemiss. Last, I attempted to add up the values of truemiss. Below are my codes. For an unknown reason, it is not working. I wondered how to solve this problem? I cannot figure it out.

    Code:
    gen truemiss=0
    
    foreach bimiss of varlist var1-var45 {
        
        recode truemiss=0 if (`bimiss'=="")
        recode truemiss=1 if (`bimiss'!="")
        sum(truemiss)
    }

    Many thanks,
    David

  • #2
    Your description of what you want is unclear.

    But perhaps the output of
    Code:
    misstable summarize var1-var45, all
    would provide the information you seek about missing values in your data.

    Comment


    • #3
      I believe you just need to use - egen - with the - rowmiss (or - rownomiss) option.
      Best regards,

      Marcos

      Comment


      • #4
        There could be many reasons why the code in #1 is not working.

        One is that it assumes that all the variables being looped over are strings.

        What the code tries to do, I think, is count the number of missing values in each variable.

        But what I think you want to do is orthogonal, to count the number of missing values in each observation. In the latter case Marcos Almeida has a solution.

        In the former case, missings (Stata Journal) supplies some functionality and has not yet been mentioned in this thread. See https://www.statalist.org/forums/for...aging-missings for an overview.

        Comment


        • #5
          Thank you, Marcos! I followed your advice and it worked perfectly! Below is the code.

          Code:
          egen truemiss =rownonmiss(var1-var45), strok
          Thank you, William!
          Thank you, Nick!

          Best,
          David

          Comment

          Working...
          X