Announcement

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

  • Counter

    Hi all,

    I have the following dataset:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double idfirm float id_molecule
     1  434
     1  434
     1 1223
     1  301
     1  832
     1  639
     1  800
     1  800
     2 1439
     2 1439
     2 1392
     2 1392
     2 1865
     2  395
     2 1439
     2  746
     2  301
     2  301
     2 1439
     2  746
     3  399
     3  474
     3  153
     3 1615
     3  477
     3 1792
     3   23
     3 1804
     3 1146
     3  961
     3 1804
     3  273
     4  281
     4 1383
     4 1992
     4  393
     4  827
     4  958
     4   80
     4  983
     4  783
     4 1626
     4  210
     4  104
     5  961
     6 1905
     6 1905
     6 1905
     6 1905
     6 1905
     6 1905
     7  963
     7  963
     7  963
     7  963
     7  963
     7  963
     7  963
     7  963
     7  963
     8 1821
     8 1402
     8 1022
     8  832
     8 1162
     8 1022
     8  885
     8  495
     8  898
     8 1821
     8 1440
     8  958
     9 1372
     9  441
     9 1482
     9 1482
     9  228
     9   38
     9 1727
     9 1787
     9  425
     9  959
     9  661
     9 1985
    10  495
    11  800
    11 1511
    11 1511
    11  800
    11  800
    11  800
    11  800
    12 1792
    12 1792
    12 1792
    12 1792
    13 1665
    13 1665
    13 1665
    13 1025
    end
    what I would like to do is:
    1) generate a counter taking on value 1 if an id_molecule is observed for the first time within a same firm (idfirm) and 0 otherwise so that for instance in case of firm with idfirm = 1 I will have:
    Counter
    1
    0
    1
    1
    1
    1
    1
    0

    2) Generate counter taking on value 1 if the id_molecule is observed for the first time in all the sample. So for instance, in the date I have displayed, 434 will have counter = 1 (just the first one) because it appears just for firm having idfirm =1, while for instance 800 will take on value 0 in that it appears also for other firms (e.g. firm with idfirm = 11).

    Can someone please help me?

    Thank you very much,

    Federico

  • #2
    Code:
    . gen long obs = _n 
    
    . 
    . bysort idfirm id_molecule (obs) : gen wanted1 = _n == 1 
    
    . 
    . bysort id_molecule (obs) : gen wanted2 = _n == 1 
    
    . 
    . sort obs 
    
    . 
    . list, sepby(idfirm) 
    
         +---------------------------------------------+
         | idfirm   id_mol~e   obs   wanted1   wanted2 |
         |---------------------------------------------|
      1. |      1        434     1         1         1 |
      2. |      1        434     2         0         0 |
      3. |      1       1223     3         1         1 |
      4. |      1        301     4         1         1 |
      5. |      1        832     5         1         1 |
      6. |      1        639     6         1         1 |
      7. |      1        800     7         1         1 |
      8. |      1        800     8         0         0 |
         |---------------------------------------------|
      9. |      2       1439     9         1         1 |
     10. |      2       1439    10         0         0 |
     11. |      2       1392    11         1         1 |
     12. |      2       1392    12         0         0 |
     13. |      2       1865    13         1         1 |
     14. |      2        395    14         1         1 |
     15. |      2       1439    15         0         0 |
     16. |      2        746    16         1         1 |
     17. |      2        301    17         1         0 |
     18. |      2        301    18         0         0 |
     19. |      2       1439    19         0         0 |
     20. |      2        746    20         0         0 |
         |---------------------------------------------|
     21. |      3        399    21         1         1 |
     22. |      3        474    22         1         1 |
     23. |      3        153    23         1         1 |
     24. |      3       1615    24         1         1 |
     25. |      3        477    25         1         1 |
     26. |      3       1792    26         1         1 |
     27. |      3         23    27         1         1 |
     28. |      3       1804    28         1         1 |
     29. |      3       1146    29         1         1 |
     30. |      3        961    30         1         1 |
     31. |      3       1804    31         0         0 |
     32. |      3        273    32         1         1 |
         |---------------------------------------------|
     33. |      4        281    33         1         1 |
     34. |      4       1383    34         1         1 |
     35. |      4       1992    35         1         1 |
     36. |      4        393    36         1         1 |
     37. |      4        827    37         1         1 |
     38. |      4        958    38         1         1 |
     39. |      4         80    39         1         1 |
     40. |      4        983    40         1         1 |
     41. |      4        783    41         1         1 |
     42. |      4       1626    42         1         1 |
     43. |      4        210    43         1         1 |
     44. |      4        104    44         1         1 |
         |---------------------------------------------|
     45. |      5        961    45         1         0 |
         |---------------------------------------------|
     46. |      6       1905    46         1         1 |
     47. |      6       1905    47         0         0 |
     48. |      6       1905    48         0         0 |
     49. |      6       1905    49         0         0 |
     50. |      6       1905    50         0         0 |
     51. |      6       1905    51         0         0 |
         |---------------------------------------------|
     52. |      7        963    52         1         1 |
     53. |      7        963    53         0         0 |
     54. |      7        963    54         0         0 |
     55. |      7        963    55         0         0 |
     56. |      7        963    56         0         0 |
     57. |      7        963    57         0         0 |
     58. |      7        963    58         0         0 |
     59. |      7        963    59         0         0 |
     60. |      7        963    60         0         0 |
         |---------------------------------------------|
     61. |      8       1821    61         1         1 |
     62. |      8       1402    62         1         1 |
     63. |      8       1022    63         1         1 |
     64. |      8        832    64         1         0 |
     65. |      8       1162    65         1         1 |
     66. |      8       1022    66         0         0 |
     67. |      8        885    67         1         1 |
     68. |      8        495    68         1         1 |
     69. |      8        898    69         1         1 |
     70. |      8       1821    70         0         0 |
     71. |      8       1440    71         1         1 |
     72. |      8        958    72         1         0 |
         |---------------------------------------------|
     73. |      9       1372    73         1         1 |
     74. |      9        441    74         1         1 |
     75. |      9       1482    75         1         1 |
     76. |      9       1482    76         0         0 |
     77. |      9        228    77         1         1 |
     78. |      9         38    78         1         1 |
     79. |      9       1727    79         1         1 |
     80. |      9       1787    80         1         1 |
     81. |      9        425    81         1         1 |
     82. |      9        959    82         1         1 |
     83. |      9        661    83         1         1 |
     84. |      9       1985    84         1         1 |
         |---------------------------------------------|
     85. |     10        495    85         1         0 |
         |---------------------------------------------|
     86. |     11        800    86         1         0 |
     87. |     11       1511    87         1         1 |
     88. |     11       1511    88         0         0 |
     89. |     11        800    89         0         0 |
     90. |     11        800    90         0         0 |
     91. |     11        800    91         0         0 |
     92. |     11        800    92         0         0 |
         |---------------------------------------------|
     93. |     12       1792    93         1         0 |
     94. |     12       1792    94         0         0 |
     95. |     12       1792    95         0         0 |
     96. |     12       1792    96         0         0 |
         |---------------------------------------------|
     97. |     13       1665    97         1         1 |
     98. |     13       1665    98         0         0 |
     99. |     13       1665    99         0         0 |
    100. |     13       1025   100         1         1 |
         +---------------------------------------------+

    Comment


    • #3
      Many thanks!

      Comment

      Working...
      X