Dear Stata users,
I have cross-sectional data of funds in one dataset and time series data of these funds in another. I need to generate a combined panel dataset of these both (to run regressions on the time series variables while keeping some cross-sectional variables constant). The funds can be uniquely identified.
I assume it requires a series of merge and reshape commands, but I wasn't able to figure out the solution myself.
Below are short examples of the two datasets and the intended result (created manually to illustrate the desired outcome). The real datasets have around 100 cross-sectional variables and contain time series data for around 1.000 funds .
1. Cross-sectional data
2. Time series data
3. Desired result as panel
Best regards,
Daniel
I have cross-sectional data of funds in one dataset and time series data of these funds in another. I need to generate a combined panel dataset of these both (to run regressions on the time series variables while keeping some cross-sectional variables constant). The funds can be uniquely identified.
I assume it requires a series of merge and reshape commands, but I wasn't able to figure out the solution myself.
Below are short examples of the two datasets and the intended result (created manually to illustrate the desired outcome). The real datasets have around 100 cross-sectional variables and contain time series data for around 1.000 funds .
1. Cross-sectional data
Code:
clear input byte(fund rating1 rating2) double(score1 score2) byte style 1 1 2 .56 2 0 2 1 2 .55 2.3 0 3 4 3 .55 1.95 1 4 5 3 .81 2.23 1 5 5 5 .8 2.1 0 end
Code:
clear input int Day byte t str4 period double fund1 int(fund2 fund3 fund4 fund5) double(return1 return2 return3 return4 return5) int index 21550 1 "pre" 87 2000 100 341 320 . . . . . 100 21551 2 "pre" 88 2080 98 341 335 .011494252873563315 .040000000000000036 -.020000000000000018 0 .046875 103 21552 3 "post" 88.5 2054 97 289 341 .005681818181818121 -.012499999999999956 -.010204081632653073 -.15249266862170086 .017910447761193993 104 21553 4 "post" 90 2180 103 295 329 .016949152542372836 .06134371957156759 .061855670103092786 .02076124567474058 -.035190615835777095 102 end format %tddd-Mon-YY Day
3. Desired result as panel
Code:
clear input int Day byte(t fund) double return int index str4 period byte(rating1 rating2) double(score1 score2) byte style 21550 1 1 . 100 "pre" 1 2 .56 2 0 21550 1 2 . 100 "pre" 1 2 .55 2.3 0 21550 1 3 . 100 "pre" 4 3 .55 1.95 1 21550 1 4 . 100 "pre" 5 3 .81 2.23 1 21550 1 5 . 100 "pre" 5 5 .8 2.1 0 21551 2 1 .011494252873563315 103 "pre" 1 2 .56 2 0 21551 2 2 .040000000000000036 103 "pre" 1 2 .55 2.3 0 21551 2 3 -.020000000000000018 103 "pre" 4 3 .55 1.95 1 21551 2 4 0 103 "pre" 5 3 .81 2.23 1 21551 2 5 .046875 103 "pre" 5 5 .8 2.1 0 21552 3 1 .005681818181818121 104 "post" 1 2 .56 2 0 21552 3 2 -.012499999999999956 104 "post" 1 2 .55 2.3 0 21552 3 3 -.010204081632653073 104 "post" 4 3 .55 1.95 1 21552 3 4 -.15249266862170086 104 "post" 5 3 .81 2.23 1 21552 3 5 .017910447761193993 104 "post" 5 5 .8 2.1 0 21553 4 1 .016949152542372836 102 "post" 1 2 .56 2 0 21553 4 2 .06134371957156759 102 "post" 1 2 .55 2.3 0 21553 4 3 .061855670103092786 102 "post" 4 3 .55 1.95 1 21553 4 4 .02076124567474058 102 "post" 5 3 .81 2.23 1 21553 4 5 -.035190615835777095 102 "post" 5 5 .8 2.1 0 end format %tdnn/dd/CCYY Day
Daniel
Comment