Announcement

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

  • Recoding a variable with data from another variable

    Hi,
    Can somebody please advise me on the most straightforward way to recode a variable with data from a different variable?

    My problem is that I have a height variable that has about 700 missing values. However, I also have an imputed height variable that I'd like to use to get rid of the missing information on the height variable.

    This is what I see when run the following command:
    Code:
    list height imputed_height if height>300
    
           +-------------------+
           | height   impute~t |
           |-------------------|
       16. |  999.9      172.9 |
       19. |  999.9      165.3 |
       23. |  999.9      173.6 |
       31. |  999.9      160.8 |
       39. |  999.9      168.5 |
           |-------------------|
       40. |  999.9      165.3 |
       77. |  999.9      171.5 |
       78. |  999.9        152 |
       79. |  999.9      163.4 |
       81. |  999.9      170.3 |
           |-------------------|
       82. |  999.9        167 |
       83. |  999.9      167.5 |
       84. |  999.9      152.4 |
       85. |  999.9      161.7 |
       86. |  999.9      169.4 |
           |-------------------|
       87. |  999.9      170.8 |
       88. |  999.9      174.2 |
       89. |  999.9      180.8 |
       90. |  999.9      171.1 |
       92. |  999.9      166.4 |
    So what I needed to do was to make height equal to imputed_height whenever height equals 999.9. I've tried to use recode but have been unable to find a working syntax hitherto.

    Thanks in advance.

  • #2
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 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.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    The answer to your question, however, is most likely this:

    Code:
    replace height = imputed_height if float(height) == float(999.9)
    Unsolicited advice: using magic number codes like 999.9 for missing values is, at best, suboptimal in Stata. It is especially problematic because the value chosen here is non-integer, so that simple constructions like height == 999.9 will generally not produce the expected results. Stata has a missing value (.) and extended missing values (.a through .z)--so learn to use those instead.

    Comment


    • #3
      Thank you, Clyde, for the quick reply. The code has worked flawlessly. I am sorry that I did not use dataex and I appreciate the extra piece of advice, I'll keep that in mind.

      Comment

      Working...
      X