It seems that there are only 2 "groups" of brands (one group is A + C, the other is B + D)
You could take advantage of this fact as follows:
The first line generates a binary indicator, group, which is equal to 1 for brands A + C and 0 for the other brands.Code:gen group=brand=="A" | brand=="C" if !missing(brand) bysort date group: gen advcomp=adv[2] if _n==1 & _N==2 bysort date group: replace advcomp=adv[1] if _n==2 & _N==2
The second line combines the observations by date and group, and for each combo makes advcomp equal to adv of the other observation. _n is the number of the observation within that combo, _N is the total number of observations in that combo (so the & _N==2 is a sanity check to make sure there are only 2 observations per group as expected), and the [1] and [2] refer to the first and second observations in each group.
Result:
Code:. list, compress noobs sep(4) +-------------------------+ | date brand adv | |-------------------------| | 01may2011 A 5 | | 01may2011 B 4 | | 01may2011 C 2 | | 01may2011 D 6 | |-------------------------| | 02may2011 A 5 | | 02may2011 B 4 | | 02may2011 C 2 | | 02may2011 D 7 | |-------------------------| | 03may2011 A 6 | | 03may2011 B 3 | | 03may2011 C 1 | | 03may2011 D 8 | +-------------------------+ . gen group=brand=="A" | brand=="C" if !missing(brand) . bysort date group: gen advcomp=adv[2] if _n==1 & _N==2 (6 missing values generated) . bysort date group: replace advcomp=adv[1] if _n==2 & _N==2 (6 real changes made) . sort date brand . list date brand adv advcomp, compress noobs sep(4) +---------------------------------+ | date brand adv adv~p | |---------------------------------| | 01may2011 A 5 2 | | 01may2011 B 4 6 | | 01may2011 C 2 5 | | 01may2011 D 6 4 | |---------------------------------| | 02may2011 A 5 2 | | 02may2011 B 4 7 | | 02may2011 C 2 5 | | 02may2011 D 7 4 | |---------------------------------| | 03may2011 A 6 1 | | 03may2011 B 3 8 | | 03may2011 C 1 6 | | 03may2011 D 8 3 | +---------------------------------+





Reply With Quote
