Announcement

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

  • Matching observations across years based on a variable

    Hi,
    I have a panel for factories with unique IDs that stop being unique in the year 2007 (the last data point is 2013). My goal is to use the end of year inventory for 2007 (MPMVAP & year==2007) and match it to the start of year inventory of the following year (MPMVAF & year==2008), and continue to do that until 2013. The problem is I don't know for a fact that the values will be the same, or that there won't be more than one factory that has the same value. So, for my first try, I'd like to see how many matches there are for final year inventory and subsequent year start inventory and to be able to identify them through an id variable that I generated which is unique (id).
    I found on an old Statalist message (https://www.stata.com/statalist/arch.../msg01035.html) a sort of similar situation and modified the code without success.
    Below is the modification I wrote. The loop only makes one replacement and keeps the rest as missing values.

    gen match_count = .
    gen match_id = .

    forval i = 2007/2013 {

    su id if (MPMVAF == MPMVAP[`i'] & (ANIO == ANIO[`i'] - 1)), meanonly
    replace match_id = r(min) in `i'
    replace match_count = r(N) in `i'
    }

    thanks for your help!

  • #2
    Well, your code inside the loop doesn't make sense because on the one hand the -forval- at the top declares i to iterate from 2007 to 2013, but then within the loop you treat `i' as if it were an observation number in your data set. So inside the data set it's trying to compare MPMVAF with the value of MPMVAP in the 2007th observation in your data set on the first time through the loop, and with the value of MPMVAP in the 2008th observation in your data set on the second time, etc. That doesn't make any sense.

    But I have to say that reading your explanation of the problem I have no idea at all what you are trying to do, so I have no advice to offer about fixing it. Perhaps somebody else will understand what you want and be able to suggest a path forward. If not, try reposting this using -dataex- (-ssc install dataex- if you don't have the command, -help dataex- for instructions) to post an example of your data, and then for your example show hand-calculated results that you want. That might make it clearer.

    Comment


    • #3
      Thanks Clyde for your response. I see that I wasn't very clear.
      I want to be able to do two things with my panel:
      1) Generate a variable that is "1" when there is a factory i in year x with value for end of year inventory that is equal to a factory j's start of year inventory in year x+1.
      2) Have the variable in 1) change value for each match (the first match equal "1", the second match equal "2") so that I have a list of values that tell me if there is a match and also how many matches (the variable will have the same value for factory i and j).
      Below is the example of my data.
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input long NUI float id_n int ANIO str1 DVN byte REGION int CIIU2 long CIIU3 byte TAMANO int(EMPTOTH EMPTOTM) float EMPTOT long(TEXVAP TEXVAF MPMVAP MPMVAF ACAVAP ACAVAF) float(match_count match_id)
      10223  194 1995 "7"  5 3111 1511 5  88   5  93  110446   78305 . .       0       0 . .
      14050 3074 1995 "3"  1 3111 1511 3  30  10  40   64228   64985 . .   64228   64985 . .
      19665 4338 1995 "7" 13 3111 1511 5 106  16 122       0       0 . .       0       0 . .
      13428 2790 1995 "7" 13 3122 1511 5 174   0 174 6168037 8042719 . . 3612688 4689111 . .
      11183  950 1995 "K" 10 3111 1511 4  76   9  85   98275   85738 . .   58214   55683 . .
      10678  561 1995 "K"  8 3111 1511 5 169  12 181  333277  283377 . .  250074  181239 . .
      11207  967 1995 "0" 10 3111 1511 6 259  32 291  153162  274988 . .   32377   56249 . .
      13787 3042 1995 "1" 13 3111 1511 7 611 155 766 1148583 1632404 . .  113121   91506 . .
      11136  913 1995 "8" 10 3111 1511 6 188  13 201  205633  214513 . .   40129   60894 . .
      10804  665 1995 "9"  8 3111 1511 3  22   1  23    3997    5691 . .     400     874 . .
      13088 2497 1995 "5" 13 3111 1511 6 397  74 471  759433 1003657 . .  158236  183740 . .
      20969 4867 1995 "4" 13 3111 1511 4  79   0  79  133092  144005 . .   46583   50402 . .
      10460  379 1995 "4"  6 3111 1511 2  13   2  15    1208    1809 . .       0     757 . .
      17763 3663 1995 "6"  5 3111 1511 5 118  13 131       0       0 . .       0       0 . .
      14082 3080 1995 "1"  5 3111 1511 2   7   7  14   10851    1678 . .       0       0 . .
      14666 3203 1995 "8"  4 3111 1511 3  32   0  32       0       0 . .       0       0 . .
      10261  219 1995 "K"  5 3111 1511 4  50   0  50   70677   42525 . .       0       0 . .
      10800  661 1995 "6"  8 3111 1511 2  15   2  17   22250   25756 . .       0       0 . .
      16383 3352 1995 "K" 13 3111 1511 2  16   3  19   15346   11959 . .       0       0 . .
      10679  562 1995 "8"  8 3111 1511 2  13   1  14    9728    3695 . .    2836    1742 . .
      10756  631 1995 "5"  8 3111 1511 3  47   1  48   22072   21766 . .    3217    5478 . .
      11999 1592 1995 "7" 13 3111 1511 6 203   2 205  622302  669108 . .   47390   87948 . .
      21324 5065 1995 "1" 13 3111 1511 3   9  25  34    6901   15286 . .       0       0 . .
      10569  455 1995 "4"  7 3111 1511 3  29  10  39   18533   24568 . .    2916    3871 . .
      10299  250 1995 "7"  5 3111 1511 3  26   4  30    5336    4972 . .       0       0 . .
      11112  901 1995 "0" 10 3111 1511 4  82   6  88   35731  112533 . .   16474   86486 . .
      11285 1025 1995 "2" 12 3111 1511 5  81  14  95  142284  167333 . .   43252   78057 . .
      10682  564 1995 "8"  8 3111 1511 3  24   6  30    2650     850 . .       0     680 . .
      10615  500 1995 "1"  7 3111 1511 7 687  30 717 1207992 1240052 . .  337026  376130 . .
      14213 3108 1995 "1" 10 3111 1511 5 121   8 129   52756  206150 . .       0       0 . .
      11030  840 1995 "2"  9 3111 1511 3  21   9  30    2930    5500 . .    1940    3030 . .
      10462  380 1995 "0"  6 3111 1511 2  13   2  15    6530    1240 . .       0       0 . .
      10707  588 1995 "7"  8 3111 1511 3  14   7  21    8506   10284 . .    1374     675 . .
      17741 3647 1995 "5" 13 3111 1511 2   9   3  12    4485    4256 . .       0       0 . .
      12411 1952 1995 "7" 13 3111 1511 3  23  12  35    1400     480 . .       0       0 . .
      13400 2767 1995 "7" 13 3111 1511 5 156   4 160  479634  486282 . .   85633   83062 . .
      20131 4437 1995 "6" 10 3111 1511 2  11   6  17    1490    1731 . .       0    1418 . .
      15265 3266 1995 "K" 11 3111 1511 1   2   3   5       0       0 . .       0       0 . .
      10551  438 1995 "1"  7 3111 1511 2   7   4  11    2536    5279 . .     969       0 . .
      12180 1739 1995 "0" 13 3111 1511 3  19   3  22   12749   11963 . .    7481    4804 . .
      12364 1906 1995 "1" 13 3111 1511 4  59   5  64   78843   95636 . .   40399   52426 . .
      11109  898 1995 "0" 10 3111 1511 3  18   1  19   13511   12903 . .    3910       0 . .
      21072 4942 1995 "2" 13 3111 1511 3  26   4  30       0       0 . .       0       0 . .
      20202 4476 1995 "9" 12 3111 1511 4  57   6  63    9008    1751 . .       0       0 . .
      13006 2428 1995 "0" 13 3111 1511 5  99  99 198   13539   21612 . .    2305    2241 . .
      18470 4027 1995 "5" 13 3111 1511 3  31   6  37   67564   79686 . .   19896   19091 . .
      11062  865 1995 "0"  9 3111 1511 3  34   9  43       0       0 . .       0       0 . .
      22221 5078 1995 "6" 10 3111 1511 3  29   2  31       0       0 . .       0       0 . .
      10228  196 1995 "8"  5 3111 1511 3  13   1  14    5830    4144 . .       0       0 . .
      18698 4199 1995 "8" 13 3111 1511 3   5  16  21       0     860 . .       0     475 . .
      11028  838 1995 "0"  9 3111 1511 6 215   5 220    9845   71359 . .       0       0 . .
      18262 3919 1995 "1"  9 3111 1511 3  28   4  32   32943   47642 . .   14495   20486 . .
      11263 1006 1995 "1" 12 3111 1511 5 114  14 128  198118  248509 . .       0       0 . .
      17262 3517 1995 "6" 13 3111 1511 3  20  19  39    1307    4247 . .       0       0 . .
      20204 4477 1995 "5" 12 3111 1511 4  58   8  66       0       0 . .       0       0 . .
      12979 2405 1995 "8" 13 3111 1511 7 554  48 602  816840 1217237 . .   61861   74496 . .
      22853 5115 1995 "2"  6 3111 1511 7 742   9 751  390852  289676 . .       0       0 . .
      10736  612 1995 "0"  8 3113 1511 4  20  36  56  294296  356889 . .  212627  283289 . .
      10265  223 1995 "2"  5 3111 1511 3  48   0  48   10117   10051 . .     794    1548 . .
      15267 3267 1995 "6" 13 3111 1511 3  30   4  34   18308   10888 . .    1641    1936 . .
      12368 1910 1995 "4" 13 3111 1511 5 138   8 146     386       0 . .       0       0 . .
      28177 5315 1995 "8" 11 3111 1511 1   2   3   5    9806    9922 . .    9806    9922 . .
      10792  654 1995 "1"  8 3111 1511 5 122  12 134  124548  129199 . .       0       0 . .
      15200 3260 1995 "5"  5 3111 1511 4  48   8  56  229585  159640 . .   12336   20450 . .
      11143  919 1995 "0" 10 3111 1511 6 317  16 333  374874  353962 . .       0       0 . .
      11243  992 1995 "7" 11 3111 1511 3  44   2  46   54981   68991 . .   43791   55455 . .
      12749 2208 1995 "3" 13 3111 1511 6 315  18 333  369731  422916 . .       0       0 . .
      12369 1911 1995 "2" 13 3111 1511 5 195   4 199       0   23081 . .       0   23081 . .
      13291 2675 1995 "8" 13 3111 1511 5 103   0 103  111052  141057 . .       0       0 . .
      17680 3598 1995 "K"  2 3111 1511 5 109   2 111  230730  246076 . .       0       0 . .
      20136 4440 1995 "7" 10 3111 1511 3  34   6  40   13742    2474 . .    2050     369 . .
      12381 1923 1995 "1" 13 3111 1511 4  45  23  68   65652  137571 . .       0       0 . .
      13621 2933 1995 "2"  5 3111 1511 6 311  33 344 3387546 2774105 . .  482301  511225 . .
      10799  660 1995 "9"  8 3111 1511 3  39   2  41   27598   31478 . .       0       0 . .
      16616 3367 1995 "2"  5 3111 1511 3   7  15  22    2056    5094 . .       0       0 . .
      12339 1885 1995 "0" 13 3111 1511 4  47   6  53   35247   42354 . .   35247   36200 . .
      13087 2496 1995 "7" 13 3111 1511 5 103   4 107       0  184689 . .       0   35160 . .
      11094  884 1995 "9" 10 3111 1511 3  23   3  26    7875    5385 . .       0       0 . .
      17290 3539 1995 "1" 13 3111 1511 4  84  13  97   15342   14900 . .    5162    5593 . .
      17062 3386 1995 "3" 13 3111 1511 7 695   9 704  871157  728608 . .  871157  728608 . .
      10576  461 1995 "7"  7 3111 1511 4  42  42  84   54143  112882 . .   11393   16799 . .
      17263 3518 1995 "4" 13 3111 1511 3  13   3  16    8316    3846 . .    3904    2123 . .
      12890 2325 1995 "2" 13 3121 1511 2  13   1  14     133     244 . .     133     244 . .
      10072   67 1995 "2"  2 3111 1511 4  56   4  60   71316   81695 . .       0       0 . .
      13056 2472 1995 "7" 13 3111 1511 5  73  71 144  221965  219195 . .       0   99938 . .
      11079  876 1995 "5"  9 3111 1511 3  22   2  24   10794   18536 . .       0       0 . .
      13289 2673 1995 "6" 13 3111 1511 3  19   2  21   29938   31678 . .       0       0 . .
      19946 4363 1995 "K"  2 3111 1511 3  28   5  33   32811   40893 . .   27754   25203 . .
      14357 3152 1995 "K"  8 3111 1511 3  19   3  22   22611    9157 . .    3000       0 . .
      12557 2064 1995 "1" 13 3111 1511 2  14   2  16    9583   16632 . .       0       0 . .
      13375 2747 1995 "2" 13 3111 1511 5 110  20 130  306116  392364 . .  111086  107044 . .
      12863 2299 1995 "5" 13 3111 1511 7 613  68 681  749531 1094136 . .       0       0 . .
      10000    1 1995 "5"  1 3111 1511 5 157   1 158   64306   66862 . .   42596   45881 . .
      14048 3073 1995 "1"  1 3111 1511 2   8   2  10       0       0 . .       0       0 . .
      21015 4894 1995 "3" 13 3111 1511 4  44   8  52   19069   17051 . .       0       0 . .
      20018 4388 1995 "2"  5 3111 1511 3  16   4  20    9754   12271 . .       0       0 . .
      12719 2183 1995 "1" 13 3111 1511 6 388  24 412  413869  632498 . .  292070  479745 . .
      10294  245 1995 "6"  5 3111 1511 3  23   2  25    9551   11233 . .    2725    2110 . .
      12737 2198 1995 "K" 13 3111 1511 3  22   2  24    8560   12430 . .       0       0 . .
      20830 4831 1995 "2" 13 3111 1511 3  14   2  16    9111    1191 . .       0       0 . .
      end
      Last edited by Maria Baez; 10 Sep 2017, 18:01.

      Comment


      • #4
        OK, I think I understand it now. Try this:

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input long NUI float id_n int ANIO str1 DVN byte REGION int CIIU2 long CIIU3 byte TAMANO int(EMPTOTH EMPTOTM) float EMPTOT long(TEXVAP TEXVAF MPMVAP MPMVAF ACAVAP ACAVAF)
        10223  194 1995 "7"  5 3111 1511 5  88   5  93  110446   78305 . .       0       0
        14050 3074 1995 "3"  1 3111 1511 3  30  10  40   64228   64985 . .   64228   64985
        19665 4338 1995 "7" 13 3111 1511 5 106  16 122       0       0 . .       0       0
        13428 2790 1995 "7" 13 3122 1511 5 174   0 174 6168037 8042719 . . 3612688 4689111
        11183  950 1995 "K" 10 3111 1511 4  76   9  85   98275   85738 . .   58214   55683
        10678  561 1995 "K"  8 3111 1511 5 169  12 181  333277  283377 . .  250074  181239
        11207  967 1995 "0" 10 3111 1511 6 259  32 291  153162  274988 . .   32377   56249
        13787 3042 1995 "1" 13 3111 1511 7 611 155 766 1148583 1632404 . .  113121   91506
        11136  913 1995 "8" 10 3111 1511 6 188  13 201  205633  214513 . .   40129   60894
        10804  665 1995 "9"  8 3111 1511 3  22   1  23    3997    5691 . .     400     874
        13088 2497 1995 "5" 13 3111 1511 6 397  74 471  759433 1003657 . .  158236  183740
        20969 4867 1995 "4" 13 3111 1511 4  79   0  79  133092  144005 . .   46583   50402
        10460  379 1995 "4"  6 3111 1511 2  13   2  15    1208    1809 . .       0     757
        17763 3663 1995 "6"  5 3111 1511 5 118  13 131       0       0 . .       0       0
        14082 3080 1995 "1"  5 3111 1511 2   7   7  14   10851    1678 . .       0       0
        14666 3203 1995 "8"  4 3111 1511 3  32   0  32       0       0 . .       0       0
        10261  219 1995 "K"  5 3111 1511 4  50   0  50   70677   42525 . .       0       0
        10800  661 1995 "6"  8 3111 1511 2  15   2  17   22250   25756 . .       0       0
        16383 3352 1995 "K" 13 3111 1511 2  16   3  19   15346   11959 . .       0       0
        10679  562 1995 "8"  8 3111 1511 2  13   1  14    9728    3695 . .    2836    1742
        10756  631 1995 "5"  8 3111 1511 3  47   1  48   22072   21766 . .    3217    5478
        11999 1592 1995 "7" 13 3111 1511 6 203   2 205  622302  669108 . .   47390   87948
        21324 5065 1995 "1" 13 3111 1511 3   9  25  34    6901   15286 . .       0       0
        10569  455 1995 "4"  7 3111 1511 3  29  10  39   18533   24568 . .    2916    3871
        10299  250 1995 "7"  5 3111 1511 3  26   4  30    5336    4972 . .       0       0
        11112  901 1995 "0" 10 3111 1511 4  82   6  88   35731  112533 . .   16474   86486
        11285 1025 1995 "2" 12 3111 1511 5  81  14  95  142284  167333 . .   43252   78057
        10682  564 1995 "8"  8 3111 1511 3  24   6  30    2650     850 . .       0     680
        10615  500 1995 "1"  7 3111 1511 7 687  30 717 1207992 1240052 . .  337026  376130
        14213 3108 1995 "1" 10 3111 1511 5 121   8 129   52756  206150 . .       0       0
        11030  840 1995 "2"  9 3111 1511 3  21   9  30    2930    5500 . .    1940    3030
        10462  380 1995 "0"  6 3111 1511 2  13   2  15    6530    1240 . .       0       0
        10707  588 1995 "7"  8 3111 1511 3  14   7  21    8506   10284 . .    1374     675
        17741 3647 1995 "5" 13 3111 1511 2   9   3  12    4485    4256 . .       0       0
        12411 1952 1995 "7" 13 3111 1511 3  23  12  35    1400     480 . .       0       0
        13400 2767 1995 "7" 13 3111 1511 5 156   4 160  479634  486282 . .   85633   83062
        20131 4437 1995 "6" 10 3111 1511 2  11   6  17    1490    1731 . .       0    1418
        15265 3266 1995 "K" 11 3111 1511 1   2   3   5       0       0 . .       0       0
        10551  438 1995 "1"  7 3111 1511 2   7   4  11    2536    5279 . .     969       0
        12180 1739 1995 "0" 13 3111 1511 3  19   3  22   12749   11963 . .    7481    4804
        12364 1906 1995 "1" 13 3111 1511 4  59   5  64   78843   95636 . .   40399   52426
        11109  898 1995 "0" 10 3111 1511 3  18   1  19   13511   12903 . .    3910       0
        21072 4942 1995 "2" 13 3111 1511 3  26   4  30       0       0 . .       0       0
        20202 4476 1995 "9" 12 3111 1511 4  57   6  63    9008    1751 . .       0       0
        13006 2428 1995 "0" 13 3111 1511 5  99  99 198   13539   21612 . .    2305    2241
        18470 4027 1995 "5" 13 3111 1511 3  31   6  37   67564   79686 . .   19896   19091
        11062  865 1995 "0"  9 3111 1511 3  34   9  43       0       0 . .       0       0
        22221 5078 1995 "6" 10 3111 1511 3  29   2  31       0       0 . .       0       0
        10228  196 1995 "8"  5 3111 1511 3  13   1  14    5830    4144 . .       0       0
        18698 4199 1995 "8" 13 3111 1511 3   5  16  21       0     860 . .       0     475
        11028  838 1995 "0"  9 3111 1511 6 215   5 220    9845   71359 . .       0       0
        18262 3919 1995 "1"  9 3111 1511 3  28   4  32   32943   47642 . .   14495   20486
        11263 1006 1995 "1" 12 3111 1511 5 114  14 128  198118  248509 . .       0       0
        17262 3517 1995 "6" 13 3111 1511 3  20  19  39    1307    4247 . .       0       0
        20204 4477 1995 "5" 12 3111 1511 4  58   8  66       0       0 . .       0       0
        12979 2405 1995 "8" 13 3111 1511 7 554  48 602  816840 1217237 . .   61861   74496
        22853 5115 1995 "2"  6 3111 1511 7 742   9 751  390852  289676 . .       0       0
        10736  612 1995 "0"  8 3113 1511 4  20  36  56  294296  356889 . .  212627  283289
        10265  223 1995 "2"  5 3111 1511 3  48   0  48   10117   10051 . .     794    1548
        15267 3267 1995 "6" 13 3111 1511 3  30   4  34   18308   10888 . .    1641    1936
        12368 1910 1995 "4" 13 3111 1511 5 138   8 146     386       0 . .       0       0
        28177 5315 1995 "8" 11 3111 1511 1   2   3   5    9806    9922 . .    9806    9922
        10792  654 1995 "1"  8 3111 1511 5 122  12 134  124548  129199 . .       0       0
        15200 3260 1995 "5"  5 3111 1511 4  48   8  56  229585  159640 . .   12336   20450
        11143  919 1995 "0" 10 3111 1511 6 317  16 333  374874  353962 . .       0       0
        11243  992 1995 "7" 11 3111 1511 3  44   2  46   54981   68991 . .   43791   55455
        12749 2208 1995 "3" 13 3111 1511 6 315  18 333  369731  422916 . .       0       0
        12369 1911 1995 "2" 13 3111 1511 5 195   4 199       0   23081 . .       0   23081
        13291 2675 1995 "8" 13 3111 1511 5 103   0 103  111052  141057 . .       0       0
        17680 3598 1995 "K"  2 3111 1511 5 109   2 111  230730  246076 . .       0       0
        20136 4440 1995 "7" 10 3111 1511 3  34   6  40   13742    2474 . .    2050     369
        12381 1923 1995 "1" 13 3111 1511 4  45  23  68   65652  137571 . .       0       0
        13621 2933 1995 "2"  5 3111 1511 6 311  33 344 3387546 2774105 . .  482301  511225
        10799  660 1995 "9"  8 3111 1511 3  39   2  41   27598   31478 . .       0       0
        16616 3367 1995 "2"  5 3111 1511 3   7  15  22    2056    5094 . .       0       0
        12339 1885 1995 "0" 13 3111 1511 4  47   6  53   35247   42354 . .   35247   36200
        13087 2496 1995 "7" 13 3111 1511 5 103   4 107       0  184689 . .       0   35160
        11094  884 1995 "9" 10 3111 1511 3  23   3  26    7875    5385 . .       0       0
        17290 3539 1995 "1" 13 3111 1511 4  84  13  97   15342   14900 . .    5162    5593
        17062 3386 1995 "3" 13 3111 1511 7 695   9 704  871157  728608 . .  871157  728608
        10576  461 1995 "7"  7 3111 1511 4  42  42  84   54143  112882 . .   11393   16799
        17263 3518 1995 "4" 13 3111 1511 3  13   3  16    8316    3846 . .    3904    2123
        12890 2325 1995 "2" 13 3121 1511 2  13   1  14     133     244 . .     133     244
        10072   67 1995 "2"  2 3111 1511 4  56   4  60   71316   81695 . .       0       0
        13056 2472 1995 "7" 13 3111 1511 5  73  71 144  221965  219195 . .       0   99938
        11079  876 1995 "5"  9 3111 1511 3  22   2  24   10794   18536 . .       0       0
        13289 2673 1995 "6" 13 3111 1511 3  19   2  21   29938   31678 . .       0       0
        19946 4363 1995 "K"  2 3111 1511 3  28   5  33   32811   40893 . .   27754   25203
        14357 3152 1995 "K"  8 3111 1511 3  19   3  22   22611    9157 . .    3000       0
        12557 2064 1995 "1" 13 3111 1511 2  14   2  16    9583   16632 . .       0       0
        13375 2747 1995 "2" 13 3111 1511 5 110  20 130  306116  392364 . .  111086  107044
        12863 2299 1995 "5" 13 3111 1511 7 613  68 681  749531 1094136 . .       0       0
        10000    1 1995 "5"  1 3111 1511 5 157   1 158   64306   66862 . .   42596   45881
        14048 3073 1995 "1"  1 3111 1511 2   8   2  10       0       0 . .       0       0
        21015 4894 1995 "3" 13 3111 1511 4  44   8  52   19069   17051 . .       0       0
        20018 4388 1995 "2"  5 3111 1511 3  16   4  20    9754   12271 . .       0       0
        12719 2183 1995 "1" 13 3111 1511 6 388  24 412  413869  632498 . .  292070  479745
        10294  245 1995 "6"  5 3111 1511 3  23   2  25    9551   11233 . .    2725    2110
        12737 2198 1995 "K" 13 3111 1511 3  22   2  24    8560   12430 . .       0       0
        20830 4831 1995 "2" 13 3111 1511 3  14   2  16    9111    1191 . .       0       0
        end
        
        
        tempfile copy
        clonevar to_match = MPMVAF
        save `copy'
        keep id_n ANIO MPMVAP
        clonevar to_match = MPMVAP
        
        rangejoin ANIO 1 1 using `copy', by(to_match)
        
        gen byte matched = !missing(id_n_U)
        by id_n ANIO, sort: egen match_count = count(id_n_U)
        Now, I could not test it on your data example, because you data example has all missing values for both MPMVAP and MPMVAF, and all of the observations have ANIO = 1995. Consequently, there is no possibility of finding any matches in this. I think that I have the logic right. (I tested it enough to know that it has no syntax errors.) If this isn't what you want, post back with a data example where there will be the possibility of some matches. (That is, don't just send the first 100 observations in your data set. Select a subset of your data that illustrates the problem well, and then apply -dataex- to only that.)

        One thing this code does not attempt to do is count up the matches 1, 2, 3,... The reason I omitted this part of your request is because nothing in your request, and nothing I could perceive in the data, tells me which one to call first, which to call second, etc.: the ordering is, as far as I can tell arbitrary since all of the matches will have the same ANIO and the same MPMVAF values. If you want the numbered 1, 2, 3, etc. in order of the values of some other variable, you need to say what that variable is. Instead, I gave you a variable match_count which tells you how many matches there were for the particular firm in that year.

        In order to run this code you will need Robert Picard's -rangejoin- command, which is available from SSC. In addition, for -rangejoin- to run, you also need Robert Picard, Nick Cox & Roberto Ferrer's -rangestat- command, also available from SSC.

        Comment

        Working...
        X