Dear Stata Experts,
Please help me to overcome the following issue. This concerns one of the questions that I have already asked sometime ago. “personid” is ID of a person; “cusip6” firm ID; “trandate” is a date of transaction when a person (“personid”) makes a transaction. What I need is to find individuals (“personid”) who make transactions in three consecutive years in the same month within a given firm. (Say if person A made transaction in 2000M1, 1999M1, 1998M1 with firm XXXX than he has “1” in all the observation for year 2000 (this variable is “consecutive”). I employ the code below, but what it gives me is that it does not necessarily takes into account consecutive years. I upload a sample of data. Please, advise me how can I improve it.
Please help me to overcome the following issue. This concerns one of the questions that I have already asked sometime ago. “personid” is ID of a person; “cusip6” firm ID; “trandate” is a date of transaction when a person (“personid”) makes a transaction. What I need is to find individuals (“personid”) who make transactions in three consecutive years in the same month within a given firm. (Say if person A made transaction in 2000M1, 1999M1, 1998M1 with firm XXXX than he has “1” in all the observation for year 2000 (this variable is “consecutive”). I employ the code below, but what it gives me is that it does not necessarily takes into account consecutive years. I upload a sample of data. Please, advise me how can I improve it.
Code:
sort personid gen exp_no = _n gen year = year(trandate) gen ym = mofd(trandate) format %tm ym * exports in the same month for 3 consecutive years gen trade_month = month(trandate) isid cusip6 personid trade_month year exp_no, sort by cusip6 personid trade_month year: gen cm_tag = _n == 1 bysort cusip6 personid cm_tag trade_month (year): gen has3m = (year[_n-3] == year - 3) & cm_tag bysort personid cusip6 year: egen consequtive=sum(has3m)
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input double personid str6 cusip6 long trandate float(exp_no year ym trade_month cm_tag has3m consequtive) 12099844 "909911" 11526 5 1991 378 7 1 0 0 12099844 "909911" 11548 1 1991 379 8 1 0 0 12099844 "909911" 11834 6 1992 388 5 1 0 0 12099844 "909911" 11848 3 1992 389 6 1 0 0 12099844 "909911" 13205 4 1996 433 2 0 0 0 12099844 "909911" 13205 2 1996 433 2 1 0 0 12099846 "909911" 13020 7 1995 427 8 1 0 0 12099846 "909911" 13179 8 1996 432 1 1 0 0 12099847 "909911" 12597 9 1994 413 6 1 0 0 12099849 "909911" 16029 10 2003 526 11 1 0 0 12099849 "909911" 16007 14 2003 525 10 1 0 0 12099849 "909911" 15740 24 2003 517 2 1 0 0 12099849 "909911" 15923 23 2003 523 8 1 0 0 12099849 "909911" 16103 21 2004 529 2 1 0 0 12099849 "909911" 16198 15 2004 532 5 1 0 0 12099849 "909911" 16310 16 2004 535 8 1 0 0 12099849 "909911" 16572 13 2005 544 5 1 0 0 12099849 "909911" 16496 18 2005 542 3 1 0 0 12099849 "909911" 16685 11 2005 548 9 1 0 0 12099849 "909911" 16762 20 2005 550 11 1 0 0 12099849 "909911" 16855 19 2006 553 2 1 0 0 12099849 "909911" 16937 22 2006 556 5 1 0 0 12099849 "909911" 17730 17 2008 582 7 1 0 0 12099849 "909911" 17933 12 2009 589 2 1 0 0 12099858 "909911" 16007 33 2003 525 10 1 0 0 12099858 "909911" 16198 29 2004 532 5 1 0 0 12099858 "909911" 16685 32 2005 548 9 1 0 0 12099858 "909911" 16762 27 2005 550 11 1 0 0 12099858 "909911" 16572 31 2005 544 5 1 0 0 12099858 "909911" 16490 30 2005 541 2 1 0 0 12099858 "909911" 16926 38 2006 556 5 1 0 0 12099858 "909911" 17135 28 2006 562 11 0 0 0 12099858 "909911" 16985 35 2006 558 7 1 0 0 12099858 "909911" 17115 25 2006 562 11 1 0 0 12099858 "909911" 17321 42 2007 569 6 1 0 0 12099858 "909911" 17231 41 2007 566 3 1 0 0 12099858 "909911" 17493 26 2007 574 11 1 0 0 12099858 "909911" 17563 36 2008 577 2 1 0 1 12099858 "909911" 17850 39 2008 586 11 1 1 1 12099858 "909911" 18697 34 2011 614 3 1 0 0 12099858 "909911" 19068 40 2012 626 3 1 0 0 12099858 "909911" 19492 37 2013 640 5 1 0 0 12099861 "909911" 17021 52 2006 559 8 1 0 0 12099861 "909911" 17218 47 2007 565 2 1 0 0 12099861 "909911" 17400 44 2007 571 8 1 0 0 12099861 "909911" 17399 53 2007 571 8 0 0 0 12099861 "909911" 18400 46 2010 604 5 1 0 0 12099861 "909911" 18498 43 2010 607 8 1 0 0 12099861 "909911" 18697 48 2011 614 3 1 0 0 12099861 "909911" 18949 54 2011 622 11 1 0 0 12099861 "909911" 19134 51 2012 628 5 1 0 0 12099861 "909911" 19683 49 2013 646 11 1 0 0 12099861 "909911" 20041 50 2014 658 11 1 0 0 12099861 "909911" 20235 45 2015 664 5 1 0 0 12305414 "007974" 13368 58 1996 439 8 1 0 0 12305414 "007974" 13440 63 1996 441 10 1 0 0 12305414 "007974" 13986 59 1998 459 4 1 0 0 12305414 "007974" 14637 69 2000 480 1 0 0 0 12305414 "007974" 14812 68 2000 486 7 0 0 0 12305414 "007974" 14815 70 2000 486 7 0 0 0 12305414 "007974" 14741 67 2000 484 5 1 0 0 12305414 "007974" 14640 64 2000 480 1 1 0 0 12305414 "007974" 14817 57 2000 486 7 1 0 0 12305414 "007974" 15299 56 2001 502 11 1 0 0 12305414 "007974" 15034 62 2001 493 2 1 0 0 12305414 "007974" 15110 65 2001 496 5 1 0 0 12305414 "007974" 15091 61 2001 495 4 1 0 0 12305414 "007974" 15299 60 2001 502 11 0 0 0 12305414 "007974" 15267 55 2001 501 10 1 0 0 12305414 "007974" 15020 66 2001 493 2 0 0 0 12901687 "007974" 15916 71 2003 522 7 1 0 0 12901687 "007974" 16188 72 2004 531 4 1 0 0 13088267 "007974" 18109 80 2009 594 7 1 0 0 13088267 "007974" 18112 90 2009 595 8 1 0 0 13088267 "007974" 18571 91 2010 610 11 0 0 0 13088267 "007974" 18507 81 2010 608 9 1 0 0 13088267 "007974" 18323 92 2010 602 3 0 0 0 13088267 "007974" 18574 76 2010 610 11 1 0 0 13088267 "007974" 18571 79 2010 610 11 0 0 0 13088267 "007974" 18323 73 2010 602 3 1 0 0 13088267 "007974" 18323 82 2010 602 3 0 0 0 13088267 "007974" 18571 89 2010 610 11 0 0 0 13088267 "007974" 18507 87 2010 608 9 0 0 0 13088267 "007974" 18931 77 2011 621 10 0 0 0 13088267 "007974" 18928 74 2011 621 10 1 0 0 13088267 "007974" 19117 83 2012 628 5 1 0 0 13088267 "007974" 19120 86 2012 628 5 0 0 0 13088267 "007974" 20026 88 2014 657 10 1 0 0 13088267 "007974" 19956 93 2014 655 8 0 0 0 13088267 "007974" 19955 85 2014 655 8 1 0 0 13088267 "007974" 20151 84 2015 662 3 0 0 0 13088267 "007974" 20150 75 2015 662 3 1 0 0 13088267 "007974" 20152 78 2015 662 3 0 0 0 end format %d trandate format %tm ym