Announcement

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

  • couple id variable

    Dear statalist,
    In my cross-sectional data I have one id -variable, but I also want to create a couple id variable. The couples that belong to each other have id+10 000. This means that id: 1551 belongs to id: 11 551.
    I would really appreciate if someone could tell me how to write the command so that I can create a couple id variable.
    Many thanks.
    Sunniva

  • #2
    is "id" a string or a numeric variable? the code is different; please provide data examples using -dataex-

    Comment


    • #3
      Sunniva:
      do you mean something along the following lines?
      Code:
      . set obs 4
      number of observations (_N) was 0, now 4
      
      . g id=1 in 1/2
      
      
      . replace id=2 if id==.
      
      . bysort id: gen counter=sum(_n)
      
      . list
      
           +--------------+
           | id   counter |
           |--------------|
        1. |  1         1 |
        2. |  1         3 |
        3. |  2         1 |
        4. |  2         3 |
           +--------------+
      
      . list
      
           +-----------------+
           |    id   counter |
           |-----------------|
        1. |     1         1 |
        2. | 10001         3 |
        3. |     2         1 |
        4. | 10002         3 |
           +-----------------+
      
      .
      Rich's caveat obviously applies (I assumed that your id variable was numeric).
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Many thanks for the reply. The id variabl is numeric and look like this:


        id
        1551
        1001
        1050
        11001
        11 050
        11551

        I would like to create a couple id like this below., but I still do not understand how to do this.
        :
        id Couple id
        1551 1
        1001 2
        1050 3
        11001 2
        11 050 3
        11551 1
        Couple: 1551 and 11551( couple id: 1), 1001 and 11001( couple id:2 ), 1050 and 11050 (couple id:3).

        Comment


        • #5
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input int id
           1551
           1001
           1050
          11001
          11050
          11551
          end
          
          gen id2= cond(id>10000,id-10000, id)
          egen couple_id= group(id2)
          id2 above is sufficient as an identifier, but the group() function of egen gives you what you want.

          Res.:

          Code:
          . l, sep(0)
          
               +-------------------------+
               |    id    id2   couple~d |
               |-------------------------|
            1. |  1551   1551          3 |
            2. |  1001   1001          1 |
            3. |  1050   1050          2 |
            4. | 11001   1001          1 |
            5. | 11050   1050          2 |
            6. | 11551   1551          3 |
               +-------------------------+

          Comment


          • #6
            Many thanks for the help.

            Comment


            • #7
              Note also the mod() function -- the Tip at https://www.stata-journal.com/articl...article=pr0031 if anything understates its usefulness.

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input int id
               1551
               1001
               1050
              11001
              11050
              11551
              end
              
              gen share = mod(id, 10000)
              
              sort share id 
              
              list, sepby(share)
              
                   +---------------+
                   |    id   share |
                   |---------------|
                1. |  1001    1001 |
                2. | 11001    1001 |
                   |---------------|
                3. |  1050    1050 |
                4. | 11050    1050 |
                   |---------------|
                5. |  1551    1551 |
                6. | 11551    1551 |
                   +---------------+
              Code:
              
              


              Comment


              • #8
                Many thanks for the help.

                Comment

                Working...
                X