Announcement

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

  • Combining two variables to create a new one

    Dear all,
    I'm working with a large household survey (over a 40,000 observations) but I don't have an id number for each observation in my data set. What I have is an id for each household (idhh) and an number for each person (idmember) in the household.

    I want to create an unique id for each observation.

    I tried using the command "gen newid = idhh + idmember" (both idhh and idmember are in the string format).
    However I have different decimal places for the observations.


    I would like to know if there's a command to create this new variable (so that my new variable "newid" has the same decimal places for all observations) combining two other variables?

    Thank you all!
    Last edited by Ana Costa; 01 Feb 2016, 06:14.

  • #2
    I believe what you are looking for is concatenation, see page 17 of the egen manual: http://www.stata.com/manuals13/degen.pdf
    Using this youd end up with equal decimal places for your newid, provided the originals had equal decimal places to start with.

    Comment


    • #3
      Click image for larger version

Name:	statalist.jpg
Views:	1
Size:	28.7 KB
ID:	1325015


      Thank you Jorrit for your reply.
      ​The command you said didn't work because for the "idhh" variable, the observations have different decimal places.
      You can see above what I have (columns idhh and idmember) and what I want (column newid)
      Last edited by Ana Costa; 01 Feb 2016, 07:00.

      Comment


      • #4
        Presuming idhh is max 5 numbers, below code works. Add zeros to taste

        Code:
        gen zeros = "00000"
        egen idlong = concat(zeros idhh idmember)
        gen idnew=substr(idlong,-6,.)

        Comment


        • #5
          That worked perfectly! Thank you!!

          Comment

          Working...
          X