Announcement

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

  • How to create a child number indicator?

    Hi all,

    Everyone was really helpful on my last post. Currently, I have a dataset that has parents (hhidpn) and children (kidid). I've created a variable (seen below) named "kidcount" that tells me the number of unique children each hhidpn has. I now would like to create a variable that gives each child a child number. In the example, lines 10-16 would have kidnumber = 1 and lines 17-23 would have kidnumber = 2 and so on for each parent. Once a new parent (hhidpn) is reached in the dataset, kidnumer would reset. I am sure this is an easy question so I apologize in advance.

    Click image for larger version

Name:	data ex.PNG
Views:	1
Size:	42.8 KB
ID:	1697287

  • #2
    something along the lines of:

    Code:
    egen kidnumber = group(hhidn kidid)

    Comment


    • #3
      Hi, thanks! This gets me close. I will post the results below. This gives me a count of each unique hhidpn-kidid pair, but I need it to restart at 1 everytime hhidpn changes if that makes sense.
      Click image for larger version

Name:	example 2.PNG
Views:	1
Size:	38.4 KB
ID:	1697290

      Comment


      • #4
        Code:
        by hhidn (kidid), sort: gen kidnumber = sum(kidid != kidid[_n-1])
        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.



        Comment


        • #5
          I will be sure to use -dataex-, sorry about that!

          Your code appears to work! I think I am a bit confused on the logic though. With your code, if a kidid does not equal the last kidid, +1 is added within each hhidpn kidid pair?

          Comment


          • #6
            Almost. +1 is added within hhidpn when a new kidid is encountered.

            Comment

            Working...
            X