Hi all!

I need some help with figuring out how to calculate the portfolio returns.
Below you can find an example of my data. From the start this dataset includes both monthly and annual observations, in order to sort the data correctly.
Basically what I've done so far, is to create Portfolio, which is done by astile that rank the stocks based on their carbon intensity by each year.
Then within these portfolios I've created the portfolio called Low, where the stocks are ranked by the market values.

The problem arises when I start with the rebalancing and calculations for the total market values for each month.
Because I have to, e.g. calculate the total market value in July 2006, based on the stocks that are in Low = 1 in 2005.

I've tried different codes:
bys Low Year Date Stock: gen Total_MV = sum(cond(Low == 1 & Year == _n-1, mMVU, .))
- This yields 0 for all observations in the whole sample.

bys Year Date: gen Total_MV = sum((cond(Year == 2005 & Low == 1, mMVU, .))) if Year == 2006 & Month == 7
- This one yields 0 for all observations in month 7 in 2006. So this is somehow better but still wrong.

What I need is to calculate the sum of the market values of each stock in July 2006 that are included in Low = 1 in 2005.

All help is appreciated!

Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input int Date float(Year Month) str7 Stock double mMVU float Return double(Portfolio Low)
16527 2005  4 "KIEL"     568.79   -.03121603 1 1
16557 2005  5 "KIEL"     585.52    .02799498 1 1
16468 2005  2 "KIEL"     543.97    .13304429 1 1
16771 2005 12 "KIEL"     743.04    .03405367 1 1
16710 2005 10 "KIEL"     693.75     .1369351 1 1
16437 2005  1 "KIEL"     476.02            . 1 1
16741 2005 11 "KIEL"     718.16   .034176216 1 1
16496 2005  3 "KIEL"     591.56     .0838562 1 1
16618 2005  7 "KIEL"     594.24   .024435306 1 1
16649 2005  8 "KIEL"     586.29  -.013471234 1 1
16680 2005  9 "KIEL"      613.6    .04549555 1 1
16588 2005  6 "KIEL"     579.86  -.010564864 1 1
16437 2005  1 "RCHL"    3588.77            . 1 1
16741 2005 11 "RCHL"    3011.15 -.0019533124 1 1
16496 2005  3 "RCHL"    3922.25   .032918602 1 1
16468 2005  2 "RCHL"    3795.24    .05593862 1 1
16710 2005 10 "RCHL"    3052.07   -.06623962 1 1
16588 2005  6 "RCHL"    3295.18   -.04476731 1 1
16649 2005  8 "RCHL"    3156.59  -.033595264 1 1
16771 2005 12 "RCHL"    3082.16    .02330668 1 1
16527 2005  4 "RCHL"    3910.71   .002255948 1 1
16680 2005  9 "RCHL"    3284.03    .04322027 1 1
16557 2005  5 "RCHL"    3544.63    -.0948633 1 1
16618 2005  7 "RCHL"    3264.44  -.007059526 1 1
16468 2005  2 "WKPL"     752.83    .07841957 1 1
16618 2005  7 "WKPL"     702.86  -.005423577 1 1
16741 2005 11 "WKPL"     799.25   .012359874 1 1
16588 2005  6 "WKPL"     713.48  -.072448164 1 1
16437 2005  1 "WKPL"     699.66            . 1 1
16649 2005  8 "WKPL"     780.14    .10431275 1 1
16527 2005  4 "WKPL"     760.54  -.018530298 1 1
16771 2005 12 "WKPL"      897.1     .1150798 1 1
16680 2005  9 "WKPL"     824.56    .05537794 1 1
16557 2005  5 "WKPL"     767.09   .008572414 1 1
16496 2005  3 "WKPL"     774.74    .02753247 1 1
16710 2005 10 "WKPL"     789.43   -.04354438 1 1
16680 2005  9 "BLNDL"   8461.21   .065679364 1 2
16710 2005 10 "BLNDL"    8593.3   .015489834 1 2
16618 2005  7 "BLNDL"   8047.62  -.035962593 1 2
16741 2005 11 "BLNDL"   8362.58   -.02721535 1 2
16496 2005  3 "BLNDL"   8640.97   .016071377 1 2
16649 2005  8 "BLNDL"   7923.34 -.0031265914 1 2
16527 2005  4 "BLNDL"   7919.51   -.08740418 1 2
16437 2005  1 "BLNDL"   8833.19            . 1 2
16771 2005 12 "BLNDL"   8944.29   .067248575 1 2
16468 2005  2 "BLNDL"    8503.2   -.03300808 1 2
16557 2005  5 "BLNDL"   8026.05   .013363804 1 2
16588 2005  6 "BLNDL"   8342.29    .03864542 1 2
16496 2005  3 "PSONL"   9896.12    .04490013 1 2
16468 2005  2 "PSONL"   9459.07  -.015612788 1 2
16741 2005 11 "PSONL"   9009.74    -.0383768 1 2
16710 2005 10 "PSONL"   9353.96   -.04618024 1 2
16649 2005  8 "PSONL"   9673.45    .02620629 1 2
16618 2005  7 "PSONL"   9423.24  -.029890925 1 2
16680 2005  9 "PSONL"   9796.06    .02733312 1 2
16771 2005 12 "PSONL"   9374.75    .04004392 1 2
16527 2005  4 "PSONL"   9833.43  -.006354702 1 2
16437 2005  1 "PSONL"    9607.9            . 1 2
16557 2005  5 "PSONL"   9659.05   .006477555 1 2
16588 2005  6 "PSONL"   9709.16    .00514026 1 2
16710 2005 10 "RSAL"    5004.22    .01083707 1 2
16557 2005  5 "RSAL"    4162.48   -.05246427 1 2
16437 2005  1 "RSAL"     4295.5            . 1 2
16649 2005  8 "RSAL"    4558.72    .06071266 1 2
16496 2005  3 "RSAL"    4738.97  .0008009337 1 2
16527 2005  4 "RSAL"    4386.68    -.0412447 1 2
16741 2005 11 "RSAL"    5165.38    .03169745 1 2
16771 2005 12 "RSAL"    5931.47    .13829322 1 2
16680 2005  9 "RSAL"    4950.28    .10015264 1 2
16588 2005  6 "RSAL"    4124.37  -.009197218 1 2
16468 2005  2 "RSAL"    4735.13    .09744137 1 2
16618 2005  7 "RSAL"    4290.18    .03751416 1 2
16557 2005  5 "ITVL"    9392.03   -.04136343 1 3
16496 2005  3 "ITVL"    9196.47   .023369005 1 3
16437 2005  1 "ITVL"     8165.4            . 1 3
16527 2005  4 "ITVL"     9908.1     .0672004 1 3
16618 2005  7 "ITVL"    9061.11    .06330452 1 3
16741 2005 11 "ITVL"    7688.36   -.04875071 1 3
16588 2005  6 "ITVL"    8505.28   -.09917368 1 3
16649 2005  8 "ITVL"    8728.85   -.03735987 1 3
16771 2005 12 "ITVL"    7728.98   .017473178 1 3
16710 2005 10 "ITVL"    8071.14   -.08320428 1 3
16468 2005  2 "ITVL"    8984.05    .09554502 1 3
16680 2005  9 "ITVL"    8756.04  .0031089485 1 3
16618 2005  7 "STANL"  23986.72   .004595602 1 3
16496 2005  3 "STANL"  23905.77   .018152678 1 3
16710 2005 10 "STANL"  28361.94  .0035508936 1 3
16468 2005  2 "STANL"  23988.46  .0034523855 1 3
16557 2005  5 "STANL"  23079.18   -.01510054 1 3
16649 2005  8 "STANL"  25835.28   .074240886 1 3
16437 2005  1 "STANL"   21732.6            . 1 3
16588 2005  6 "STANL"  23876.74   .032030724 1 3
16771 2005 12 "STANL"  28094.06   .012108482 1 3
16527 2005  4 "STANL"  23430.33   -.02008867 1 3
16741 2005 11 "STANL"  27755.94  -.022932366 1 3
16680 2005  9 "STANL"   28261.4    .09846766 1 3
16496 2005  3 "VODL"  172196.08   .032149825 1 3
16527 2005  4 "VODL"  171717.85  .0014842894 1 3
16618 2005  7 "VODL"  153488.87    -.0301091 1 3
16680 2005  9 "VODL"  175732.35    .07463264 1 3
end
format %tdnn/dd/CCYY Date