Announcement

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

  • Replace missing observations

    Dear Stata users

    Is there a simple way to replace mi(y) with x's corresponding y value? I have tried to do some research without any luck. Please see the attached screenshot. Looking forward to hear from you - hope you can help me!

    Best regards

    Harris

    Click image for larger version

Name:	123.JPG
Views:	1
Size:	19.7 KB
ID:	1706282

  • #2
    What you are asking to do is only meaningful if every value of x is always consistently associated with the same value of y if y is not missing. This assumption is verified in the code below. If the verification passes, the replacement is made:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(x y)
    232 1
    232 1
    232 1
    233 2
     11 4
     11 4
    233 2
    232 .
    232 .
    232 .
    232 .
    233 .
     11 .
     11 .
    233 .
    end
    
    by x (y), sort: assert inlist(y, y[1], .)
    by x (y): replace y = y[1] if missing(y)
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Also, it is the norm in this community to use our real given and surnames as our user name, to promote collegiality and professionalism. (I apologize if your surname truly is Econ.) You cannot edit your username in your profile, but you can click on Contact Us in the lower right corner of this page and request the system administrator to make the change for you. Thank you in advance.

    Comment

    Working...
    X