Hello Statalist-Community, I have the following problem that I have been unable to solve for quite some time. For my thesis, I have Compustat Annual Data (funda.dta) and CRSP Daily Stock Data (CRSP.dta) , and I want to merge them using the linking table (CRSP_Compstat_Link.dta). I have already successfully performed a 1:1 merge with the Compustat Annual dataset and the Linking Table using the following command:
use "funda.dta", replace
merge 1:1 gvkey fyear using "CRSP_Compustat_Link.dta"
drop if _merge==1
drop if _merge==2
drop _merge
save "funda_link.dta", replace
However, I have been unable to find adequate help online for correctly merging the CRSP Daily dataset with the Annual dataset. Perhaps it's so simple with a provided Linking Table, that I couldn't find Stata instructions online. Everything I found was in a different programming language, or I couldn't relate the presented problem to my situation. This is just a small excerpt of the variables from the datasets. But from what I've read so far over the last days, I believe these are necessary ones for the merge. If anything is missing, I'd be happy to provide it.
What I have understood so far is that I need an M:1 merge and that the Compustat data represents fiscal years, while the CRSP (stock price data) is on a calendar basis, and they need to be aligned.
Therefore, I hope to receive your solution here.
Compustat Annual Data (funda.dta):
Linking Table (CRSP_Compustat_Link.dta):
CRSP Daily Stock Data (CRSP.dta):
use "funda.dta", replace
merge 1:1 gvkey fyear using "CRSP_Compustat_Link.dta"
drop if _merge==1
drop if _merge==2
drop _merge
save "funda_link.dta", replace
However, I have been unable to find adequate help online for correctly merging the CRSP Daily dataset with the Annual dataset. Perhaps it's so simple with a provided Linking Table, that I couldn't find Stata instructions online. Everything I found was in a different programming language, or I couldn't relate the presented problem to my situation. This is just a small excerpt of the variables from the datasets. But from what I've read so far over the last days, I believe these are necessary ones for the merge. If anything is missing, I'd be happy to provide it.
What I have understood so far is that I need an M:1 merge and that the Compustat data represents fiscal years, while the CRSP (stock price data) is on a calendar basis, and they need to be aligned.
Therefore, I hope to receive your solution here.
Compustat Annual Data (funda.dta):
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str6 gvkey long datadate double fyear "001004" 15126 2000 "001004" 15491 2001 "001004" 16222 2003 "001004" 16587 2004 "001004" 16952 2005 "001004" 18778 2010 "001004" 19144 2011 "001004" 19509 2012 "001004" 19874 2013 "001004" 20239 2014 "001004" 20605 2015 "001004" 20970 2016 "001013" 14914 2000 "001013" 15279 2001 "001013" 15644 2002 "001013" 16009 2003 "001013" 16375 2004 "001013" 16740 2005 "001013" 17105 2006 "001013" 17470 2007 "001013" 17836 2008 "001013" 18170 2009 "001013" 18535 2010 "001038" 15065 2000 "001038" 15430 2001 "001038" 15795 2002 "001038" 16161 2003 "001045" 19723 2013 "001045" 20088 2014 "001045" 20453 2015 "001045" 20819 2016 "001045" 21184 2017 "001050" 14975 2000 "001050" 15340 2001 "001050" 15705 2002 "001050" 16070 2003 "001050" 16436 2004 "001050" 16801 2005 "001050" 17166 2006 "001050" 17531 2007 "001050" 17897 2008 "001050" 18262 2009 "001050" 18627 2010 "001050" 18992 2011 "001050" 19358 2012 "001050" 19723 2013 "001050" 20088 2014 "001050" 20453 2015 "001050" 20819 2016 "001050" 21184 2017 "001056" 14791 2000 "001056" 15156 2001 "001056" 15521 2002 "001056" 15886 2003 "001056" 16252 2004 "001056" 16617 2005 "001056" 16982 2006 "001072" 15065 2000 "001072" 15430 2001 "001072" 15795 2002 "001072" 16161 2003 "001072" 16526 2004 "001072" 16891 2005 "001072" 17256 2006 "001072" 17622 2007 "001072" 17987 2008 "001072" 18352 2009 "001072" 18717 2010 "001072" 19083 2011 "001072" 19448 2012 "001072" 19813 2013 "001072" 20178 2014 "001072" 20544 2015 "001072" 20909 2016 "001078" 14975 2000 "001078" 15340 2001 "001078" 15705 2002 "001078" 16070 2003 "001078" 16436 2004 "001078" 16801 2005 "001078" 17166 2006 "001078" 17531 2007 "001078" 17897 2008 "001078" 18262 2009 "001078" 18627 2010 "001078" 18992 2011 "001078" 19358 2012 "001078" 19723 2013 "001078" 20088 2014 "001078" 20453 2015 "001078" 20819 2016 "001078" 21184 2017 "001082" 15095 2000 "001082" 15460 2001 "001082" 15825 2002 "001082" 16191 2003 "001082" 16556 2004 "001082" 16921 2005 "001082" 17286 2006 "001082" 17652 2007 end format %td datadate
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str6 gvkey int(datadate fyear) long GVKEY1 int(endfyr begfyr) str1 linkprim str3 liid str2 linktype long(lpermno lpermco) byte USEDFLAG int(linkdt linkenddt) "001004" 13300 1995 1004 13300 12935 "P" "01" "LU" 54594 20000 1 4497 . "001004" 13665 1996 1004 13665 13301 "P" "01" "LU" 54594 20000 1 4497 . "001004" 14030 1997 1004 14030 13666 "P" "01" "LU" 54594 20000 1 4497 . "001004" 14395 1998 1004 14395 14031 "P" "01" "LU" 54594 20000 1 4497 . "001004" 14761 1999 1004 14761 14396 "P" "01" "LU" 54594 20000 1 4497 . "001004" 15126 2000 1004 15126 14762 "P" "01" "LU" 54594 20000 1 4497 . "001004" 15491 2001 1004 15491 15127 "P" "01" "LU" 54594 20000 1 4497 . "001004" 15856 2002 1004 15856 15492 "P" "01" "LU" 54594 20000 1 4497 . "001004" 16222 2003 1004 16222 15857 "P" "01" "LU" 54594 20000 1 4497 . "001004" 16587 2004 1004 16587 16223 "P" "01" "LU" 54594 20000 1 4497 . "001004" 16952 2005 1004 16952 16588 "P" "01" "LU" 54594 20000 1 4497 . "001004" 17317 2006 1004 17317 16953 "P" "01" "LU" 54594 20000 1 4497 . "001004" 17683 2007 1004 17683 17318 "P" "01" "LU" 54594 20000 1 4497 . "001004" 18048 2008 1004 18048 17684 "P" "01" "LU" 54594 20000 1 4497 . "001004" 18413 2009 1004 18413 18049 "P" "01" "LU" 54594 20000 1 4497 . "001004" 18778 2010 1004 18778 18414 "P" "01" "LU" 54594 20000 1 4497 . "001004" 19144 2011 1004 19144 18779 "P" "01" "LU" 54594 20000 1 4497 . "001004" 19509 2012 1004 19509 19145 "P" "01" "LU" 54594 20000 1 4497 . "001004" 19874 2013 1004 19874 19510 "P" "01" "LU" 54594 20000 1 4497 . "001004" 20239 2014 1004 20239 19875 "P" "01" "LU" 54594 20000 1 4497 . "001004" 20605 2015 1004 20605 20240 "P" "01" "LU" 54594 20000 1 4497 . "001004" 20970 2016 1004 20970 20606 "P" "01" "LU" 54594 20000 1 4497 . "001013" 13087 1995 1013 13087 12723 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 13453 1996 1013 13453 13088 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 13818 1997 1013 13818 13454 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 14183 1998 1013 14183 13819 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 14548 1999 1013 14548 14184 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 14914 2000 1013 14914 14549 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 15279 2001 1013 15279 14915 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 15644 2002 1013 15644 15280 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 16009 2003 1013 16009 15645 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 16375 2004 1013 16375 16010 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 16740 2005 1013 16740 16376 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 17105 2006 1013 17105 16741 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 17470 2007 1013 17470 17106 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 17836 2008 1013 17836 17471 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 18170 2009 1013 18170 17806 "P" "01" "LU" 50906 2902 1 7014 18627 "001013" 18535 2010 1013 18535 18171 "P" "01" "LU" 50906 2902 1 7014 18627 "001021" 12964 1995 1021 12964 12600 "P" "01" "LU" 10218 109 1 7592 14322 "001021" 13330 1996 1021 13330 12965 "P" "01" "LU" 10218 109 1 7592 14322 "001021" 13695 1997 1021 13695 13331 "P" "01" "LU" 10218 109 1 7592 14322 "001021" 14060 1998 1021 14060 13696 "P" "01" "LU" 10218 109 1 7592 14322 "001034" 13148 1995 1034 13148 12784 "P" "01" "LC" 65832 20007 1 8811 17895 "001034" 13514 1996 1034 13514 13149 "P" "01" "LC" 65832 20007 1 8811 17895 "001034" 13879 1997 1034 13879 13515 "P" "01" "LC" 65832 20007 1 8811 17895 "001034" 14244 1998 1034 14244 13880 "P" "01" "LC" 65832 20007 1 8811 17895 "001034" 14609 1999 1034 14609 14245 "P" "01" "LC" 65832 20007 1 8811 17895 "001034" 14975 2000 1034 14975 14610 "P" "01" "LC" 65832 20007 1 8811 17895 "001034" 15340 2001 1034 15340 14976 "P" "01" "LC" 65832 20007 1 8811 17895 "001034" 15705 2002 1034 15705 15341 "P" "01" "LC" 65832 20007 1 8811 17895 "001034" 16070 2003 1034 16070 15706 "P" "01" "LC" 65832 20007 1 8811 17895 "001034" 16436 2004 1034 16436 16071 "P" "01" "LC" 65832 20007 1 8811 17895 "001034" 16801 2005 1034 16801 16437 "P" "01" "LC" 65832 20007 1 8811 17895 "001034" 17166 2006 1034 17166 16802 "P" "01" "LC" 65832 20007 1 8811 17895 "001034" 17531 2007 1034 17531 17167 "P" "01" "LC" 65832 20007 1 8811 17895 "001036" 13148 1995 1036 13148 12784 "C" "01" "LC" 65453 20008 1 8740 15126 "001036" 13514 1996 1036 13514 13149 "C" "01" "LC" 65453 20008 1 8740 15126 "001036" 13879 1997 1036 13879 13515 "C" "01" "LC" 65453 20008 1 8740 15126 "001036" 14244 1998 1036 14244 13880 "C" "01" "LC" 65453 20008 1 8740 15126 "001036" 14609 1999 1036 14609 14245 "C" "01" "LC" 65453 20008 1 8740 15126 "001036" 14975 2000 1036 14975 14610 "C" "01" "LC" 65453 20008 1 8740 15126 "001038" 13239 1995 1038 13239 12874 "P" "01" "LU" 66413 6301 1 8630 16436 "001038" 13604 1996 1038 13604 13240 "P" "01" "LU" 66413 6301 1 8630 16436 "001038" 13969 1997 1038 13969 13605 "P" "01" "LU" 66413 6301 1 8630 16436 "001038" 14334 1998 1038 14334 13970 "P" "01" "LU" 66413 6301 1 8630 16436 "001038" 14700 1999 1038 14700 14335 "P" "01" "LU" 66413 6301 1 8630 16436 "001038" 15065 2000 1038 15065 14701 "P" "01" "LU" 66413 6301 1 8630 16436 "001038" 15430 2001 1038 15430 15066 "P" "01" "LU" 66413 6301 1 8630 16436 "001038" 15795 2002 1038 15795 15431 "P" "01" "LU" 66413 6301 1 8630 16436 "001038" 16161 2003 1038 16161 15796 "P" "01" "LU" 66413 6301 1 8630 16436 "001043" 12995 1995 1043 12995 12631 "C" "01" "LC" 80071 30061 1 12393 14636 "001043" 13361 1996 1043 13361 12996 "C" "01" "LC" 80071 30061 1 12393 14636 "001043" 13726 1997 1043 13726 13362 "C" "01" "LC" 80071 30061 1 12393 14636 "001043" 14091 1998 1043 14091 13727 "C" "01" "LC" 80071 30061 1 12393 14636 "001043" 14456 1999 1043 14456 14092 "C" "01" "LC" 80071 30061 1 12393 14636 "001045" 19723 2013 1045 19723 19359 "P" "04" "LC" 21020 20010 1 19701 . "001045" 20088 2014 1045 20088 19724 "P" "04" "LC" 21020 20010 1 19701 . "001045" 20453 2015 1045 20453 20089 "P" "04" "LC" 21020 20010 1 19701 . "001045" 20819 2016 1045 20819 20454 "P" "04" "LC" 21020 20010 1 19701 . "001045" 21184 2017 1045 21184 20820 "P" "04" "LC" 21020 20010 1 19701 . "001045" 13148 1995 1045 13148 12784 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 13514 1996 1045 13514 13149 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 13879 1997 1045 13879 13515 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 14244 1998 1045 14244 13880 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 14609 1999 1045 14609 14245 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 14975 2000 1045 14975 14610 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 15340 2001 1045 15340 14976 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 15705 2002 1045 15705 15341 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 16070 2003 1045 16070 15706 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 16436 2004 1045 16436 16071 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 16801 2005 1045 16801 16437 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 17166 2006 1045 17166 16802 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 17531 2007 1045 17531 17167 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 17897 2008 1045 17897 17532 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 18262 2009 1045 18262 17898 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 18627 2010 1045 18627 18263 "P" "01" "LC" 21020 20010 1 761 18996 "001045" 18992 2011 1045 18992 18628 "P" "01" "LC" 21020 20010 1 761 18996 "001050" 13148 1995 1050 13148 12784 "P" "01" "LC" 11499 176 1 7637 . "001050" 13514 1996 1050 13514 13149 "P" "01" "LC" 11499 176 1 7637 . "001050" 13879 1997 1050 13879 13515 "P" "01" "LC" 11499 176 1 7637 . end format %td endfyr format %td begfyr
Code:
* Example generated by -dataex-. For more info, type help dataex clear input long PERMNO int date long PERMCO 10012 14612 7969 10012 14613 7969 10012 14614 7969 10012 14615 7969 10012 14616 7969 10012 14619 7969 10012 14620 7969 10012 14621 7969 10012 14622 7969 10012 14623 7969 10012 14627 7969 10012 14628 7969 10012 14629 7969 10012 14630 7969 10012 14633 7969 10012 14634 7969 10012 14635 7969 10012 14636 7969 10012 14637 7969 10012 14640 7969 10012 14641 7969 10012 14642 7969 10012 14643 7969 10012 14644 7969 10012 14647 7969 10012 14648 7969 10012 14649 7969 10012 14650 7969 10012 14651 7969 10012 14654 7969 10012 14655 7969 10012 14656 7969 10012 14657 7969 10012 14658 7969 10012 14662 7969 10012 14663 7969 10012 14664 7969 10012 14665 7969 10012 14668 7969 10012 14669 7969 10012 14670 7969 10012 14671 7969 10012 14672 7969 10012 14675 7969 10012 14676 7969 10012 14677 7969 10012 14678 7969 10012 14679 7969 10012 14682 7969 10012 14683 7969 10012 14684 7969 10012 14685 7969 10012 14686 7969 10012 14689 7969 10012 14690 7969 10012 14691 7969 10012 14692 7969 10012 14693 7969 10012 14696 7969 10012 14697 7969 10012 14698 7969 10012 14699 7969 10012 14700 7969 10012 14703 7969 10012 14704 7969 10012 14705 7969 10012 14706 7969 10012 14707 7969 10012 14710 7969 10012 14711 7969 10012 14712 7969 10012 14713 7969 10012 14714 7969 10012 14717 7969 10012 14718 7969 10012 14719 7969 10012 14720 7969 10012 14724 7969 10012 14725 7969 10012 14726 7969 10012 14727 7969 10012 14728 7969 10012 14731 7969 10012 14732 7969 10012 14733 7969 10012 14734 7969 10012 14735 7969 10012 14738 7969 10012 14739 7969 10012 14740 7969 10012 14741 7969 10012 14742 7969 10012 14745 7969 10012 14746 7969 10012 14747 7969 10012 14748 7969 10012 14749 7969 10012 14752 7969 10012 14753 7969 10012 14754 7969 end format %td date
Comment