Announcement

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

  • Generate new variable by counting

    ID Best Friend 1 Best Friend 2 Best friend 3
    200102 200105 200110 200104
    200103 200102 200104 .
    200104 . . .
    200105 200104 . .
    200106 200110 200105 200104
    200107 200110 200105 200104
    200108 200102 200109 .
    200109 200110 200102 200109
    200110 . .
    My data looks similar to the table above. Individuals are identified by a unique ID. Eveyone was asked to nominate other people from the same dataset as their friends.
    I would like to generate a (or several) new variable(s) that show how many times each individual was nominated as best friend by the other individuals. For example ID 200102 was named two times under Best Friend 1.
    So the new variable "Number of nominations as BF1" would simply show the value 2 in the row of ID 200102.
    Does anyone have an idea how to solve this problem. Every help is highly appreciated. Thanks in advance.

  • #2
    A loop over observations is usually despised, and I will join in if there is a neater way to do it, which I did try to think up.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long(id bestfriend1 bestfriend2 bestfriend3)
    200102 200105 200110 200104
    200103 200102 200104      .
    200104      .      .      .
    200105 200104      .      .
    200106 200110 200105 200104
    200107 200110 200105 200104
    200108 200102 200109      .
    200109 200110 200102 200109
    200110      .      .      .
    end
    
    gen count = 0
    
    quietly forval i = 1/`=_N' {
        forval j = 1/3 {
            count if bestfriend`j' == id[`i']
            replace count = count + r(N) in `i'
        }
    }
    
    list 
    
         +-------------------------------------------------+
         |     id   bestfr~1   bestfr~2   bestfr~3   count |
         |-------------------------------------------------|
      1. | 200102     200105     200110     200104       3 |
      2. | 200103     200102     200104          .       0 |
      3. | 200104          .          .          .       5 |
      4. | 200105     200104          .          .       3 |
      5. | 200106     200110     200105     200104       0 |
         |-------------------------------------------------|
      6. | 200107     200110     200105     200104       0 |
      7. | 200108     200102     200109          .       0 |
      8. | 200109     200110     200102     200109       2 |
      9. | 200110          .          .          .       4 |
         +-------------------------------------------------+
    .

    Comment


    • #3
      I think post #2 isn't quite what post #1 asks for, which seems to me to be three separate counters. I think the following will do the job, as long as you are running Stata 16 or later.
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input long(id bestfriend1 bestfriend2 bestfriend3)
      200102 200105 200110 200104
      200103 200102 200104      .
      200104      .      .      .
      200105 200104      .      .
      200106 200110 200105 200104
      200107 200110 200105 200104
      200108 200102 200109      .
      200109 200110 200102 200109
      200110      .      .      .
      end
      frame copy default bff, replace
      
      frame bff {
          reshape long bestfriend, i(id) j(rank)
          drop if missing(bestfriend)
          sort bestfriend
          collapse (count) nbf=id, by(bestfriend rank)
          reshape wide nbf, i(bestfriend) j(rank)
      }
      
      frlink 1:1 id, frame(bff bestfriend) generate(bflink)
      frget nbf*, from(bflink)
      drop bflink
      mvencode nbf*, mv(0)
      list, clean noobs abbreviate(12)
      Code:
      . list, clean noobs abbreviate(12)
      
              id   bestfriend1   bestfriend2   bestfriend3   nbf1   nbf2   nbf3  
          200102        200105        200110        200104      2      1      0  
          200103        200102        200104             .      0      0      0  
          200104             .             .             .      1      1      3  
          200105        200104             .             .      1      2      0  
          200106        200110        200105        200104      0      0      0  
          200107        200110        200105        200104      0      0      0  
          200108        200102        200109             .      0      0      0  
          200109        200110        200102        200109      0      1      1  
          200110             .             .             .      3      1      0

      Comment


      • #4
        The question in #1 was

        a (or several) new variable(s) that show how many times each individual was nominated as best friend by the other individuals
        and I went for the first request, a variable with a total. It is easy enough to break down the total into separate variables.


        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input long(id bestfriend1 bestfriend2 bestfriend3)
        200102 200105 200110 200104
        200103 200102 200104      .
        200104      .      .      .
        200105 200104      .      .
        200106 200110 200105 200104
        200107 200110 200105 200104
        200108 200102 200109      .
        200109 200110 200102 200109
        200110      .      .      .
        end
        
        quietly forval j = 1/3 { 
            gen count`j' = 0 
            forval i = 1/`=_N' { 
            
                count if bestfriend`j' == id[`i']
                replace count`j' = count`j' + r(N) in `i'
            }
        }
        
        gen count = count1 + count2 + count3 
        
        list 
        
             +----------------------------------------------------------------------------+
             |     id   bestfr~1   bestfr~2   bestfr~3   count1   count2   count3   count |
             |----------------------------------------------------------------------------|
          1. | 200102     200105     200110     200104        2        1        0       3 |
          2. | 200103     200102     200104          .        0        0        0       0 |
          3. | 200104          .          .          .        1        1        3       5 |
          4. | 200105     200104          .          .        1        2        0       3 |
          5. | 200106     200110     200105     200104        0        0        0       0 |
             |----------------------------------------------------------------------------|
          6. | 200107     200110     200105     200104        0        0        0       0 |
          7. | 200108     200102     200109          .        0        0        0       0 |
          8. | 200109     200110     200102     200109        0        1        1       2 |
          9. | 200110          .          .          .        3        1        0       4 |
             +----------------------------------------------------------------------------+

        Comment


        • #5
          Thanks so much! it turned out to be exactly what I was looking for.

          Comment

          Working...
          X