Announcement

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

  • How can I copy values for a variable to other observations with the same id?

    I have various individuals with a unique identifier (id) and if they died, they have a death id (id_do). I want to copy the values for id_do to other missing observations for the same id. Below is a screenshot that should hopefully help explain what I mean using an example for individual (id) 199763. I use stata 15 for reference.
    Thanks in advance!
    Click image for larger version

Name:	Screenshot 2024-02-20 at 19.46.19.png
Views:	1
Size:	336.1 KB
ID:	1743982


  • #2
    OK, from the color, it appears that id_do is a string variable. It is an unusual one in that it has "." as a value--as if somebody made a string that looks like a Stata numeric variable with missing values. Be that as it may:
    Code:
    replace id_do = "" if id_do == "."
    
    by id (id_do), sort: replace id_do = id_do[_N]
    I appreciate your posting example data. But screenshots are not as helpful as you might imagine them to be, for a number of reasons, which you can read about in the Forum FAQ at #12. In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 18, 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.

    Comment


    • #3
      What looks like a version of this was cross-posted at https://www.reddit.com/r/stata/comme...ult_spreading/

      That raises comments on two quite different levels. First, the solution offered there works for numeric variables in which missing values are sorted last. But your identifier turns out to be string and missing values on string variables are sorted first. That's why Clyde Schechter sorted and then picked up the last value for each identifier.

      Second, it is a rule on Reddit and a request here to tell people about cross-posting. That rule or request is public in either case so that people interested in posting answers don't duplicate efforts made elsewhere, and so that people interested in reading answers get a signal that a good answer might be posted elsewhere.

      Our request is explicit at https://www.statalist.org/forums/help#crossposting

      Comment

      Working...
      X