Announcement

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

  • Creating Birth Ordered Dyads

    Hello, I am attempting to organize sibling pair data by birth order. As it stands I have variables id1 and id2 for each pair, however I want my data to be that id1 is the older sibling and id2 is the younger sibling. I have age_1 and age_2 for each pair as well, I am looking for guidance as to what syntax I need to be able to make id_1 the older sibling and id_2 the younger sibling for each of my pairs.
    Last edited by Huda Jabbar; 29 Nov 2022, 08:57. Reason: dyadic data

  • #2
    Code:
    clonevar swap = id_1 if age_1 < age_2
    replace id_1 = id_2 if !missing(swap)
    replace id_2 = swap if !missing(swap)
    Note: As no example data was provided, this code is untested and may fail due to details of the data set that had to be imagined. In the future, to avoid guesswork and wasted time (for both of us), always show example data when requesting assistance with code. And always use the -dataex- command to do that. 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.

    Comment


    • #3
      Huda:
      welcome to this forum.
      Do you mean something along the following lines?
      Code:
      . set obs 2
      Number of observations (_N) was 0, now 2.
      
      . g HH_id=1
      
      . g id=2 in 1
      
      . replace id=1 in 2
      
      . g age=10 in 1
      
      . replace age=15 in 2
      
      . sort id age
      
      . list
      
           +------------------+
           | HH_id   id   age |
           |------------------|
        1. |     1    1    15 |
        2. |     1    2    10 |
           +------------------+
      
      .
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Thank you! The clonevar syntax worked. I will make sure to add more detail in future posts to not waste anyone's time.

        Comment


        • #5
          See also https://www.stata-journal.com/articl...article=dm0043

          Comment

          Working...
          X