Greetings,
I have a specific index for 365 pairs of countries between 1 January 2020 and 30 September 2022 (daily data). Follows an example of my data:
What I would like to do is to compute the difference between a) the average index during the last week of a month and b) the average index during the first week of a month.
The following code gives me almost what I want:
The problem is that the word “almost” makes all the difference here, and with this code YYY and ZZZ are missing when there are observations missing in index. I would like to compute the average index, even if there are days with missing data in the first and last weeks. Therefore, for a certain month, there would be only a missing value in my final variable if data for the entire first/last week is missing.
Could someone help me with this?
Best regards.
I have a specific index for 365 pairs of countries between 1 January 2020 and 30 September 2022 (daily data). Follows an example of my data:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int date float day byte month int year str3(iso3_o iso3_d) float country_pair double index 21915 1 1 2020 "AUS" "ARE" 1 12.979732513427734 21916 2 1 2020 "AUS" "ARE" 1 13.065977096557617 21917 3 1 2020 "AUS" "ARE" 1 11.556705474853516 21918 4 1 2020 "AUS" "ARE" 1 11.211729049682617 21919 5 1 2020 "AUS" "ARE" 1 12.462267875671387 21920 6 1 2020 "AUS" "ARE" 1 12.850366592407227 21921 7 1 2020 "AUS" "ARE" 1 14.833980560302734 21922 8 1 2020 "AUS" "ARE" 1 15.825786590576172 21923 9 1 2020 "AUS" "ARE" 1 15.955153465270996 21924 10 1 2020 "AUS" "ARE" 1 13.4109525680542 21925 11 1 2020 "AUS" "ARE" 1 16.041397094726563 21926 12 1 2020 "AUS" "ARE" 1 16.429494857788086 21927 13 1 2020 "AUS" "ARE" 1 15.135834693908691 21928 14 1 2020 "AUS" "ARE" 1 15.437688827514648 21929 15 1 2020 "AUS" "ARE" 1 14.489004135131836 21930 16 1 2020 "AUS" "ARE" 1 13.324708938598633 21931 17 1 2020 "AUS" "ARE" 1 12.332901954650879 21932 18 1 2020 "AUS" "ARE" 1 12.462267875671387 21933 19 1 2020 "AUS" "ARE" 1 13.540319442749023 21934 20 1 2020 "AUS" "ARE" 1 12.807244300842285 21935 21 1 2020 "AUS" "ARE" 1 11.427339553833008 21936 22 1 2020 "AUS" "ARE" 1 10.737386703491211 21937 23 1 2020 "AUS" "ARE" 1 10.651143074035645 21938 24 1 2020 "AUS" "ARE" 1 10.176800727844238 21939 25 1 2020 "AUS" "ARE" 1 9.702458381652832 21940 26 1 2020 "AUS" "ARE" 1 10.69426441192627 21941 27 1 2020 "AUS" "ARE" 1 10.69426441192627 21942 28 1 2020 "AUS" "ARE" 1 10.478654861450195 21943 29 1 2020 "AUS" "ARE" 1 9.745579719543457 21944 30 1 2020 "AUS" "ARE" 1 10.004312515258789 21945 31 1 2020 "AUS" "ARE" 1 10.090556144714355 21946 1 2 2020 "AUS" "ARE" 1 10.263044357299805 21947 2 2 2020 "AUS" "ARE" 1 9.874945640563965 21948 3 2 2020 "AUS" "ARE" 1 11.12548542022705 end format %tdnn/dd/CCYY date
The following code gives me almost what I want:
Code:
bysort country_pair month year (date) : gen YYY = (index[1]+index[2]+index[3]+index[4]+index[5]+index[6]+index[7])/7 bysort country_pair month year (date) : gen ZZZ = (index[_N]+index[_N-1]+index[_N-2]+index[_N-3]+index[_N-4]+index[_N-5]+index[_N-6])/7 gen XXX= ZZZ-YYY
Could someone help me with this?
Best regards.

Comment