I am looking for a way to calculate the average for each "two consecutive observations" the by group in panel data. Let's say the group or firms is id, the time the is year, the original variable is asset, the wanted variable is asset_ave
asset_ave at year (t)= (asset at year( t) + asset at year (t-1))/2
With three conditions here:
1. The starting observation of asset_av should be missing due to there is no previous-year data for calculating
2. If there is a missing observation in two consecutive observations of a firm => set the value of asset_ave=".". For example, due to the missing observation of asset in year 2002, the value of asset_av in years 2002 and 2003 are missing.
3. If the two consecutive observations for a firm are not for two consecutive years, the asset_av has also been set as missing variables, for example, from the table below, due to years 2001 and 2005 are not two consecutive years, the asset_av receive the missing results for the year 2005
Could you please provide me the code for calculating the asset_ave and satisfy these requirements?
A sample example is
Thanks in advance! Please let me know if there is anything I did not explain clearly.
asset_ave at year (t)= (asset at year( t) + asset at year (t-1))/2
With three conditions here:
1. The starting observation of asset_av should be missing due to there is no previous-year data for calculating
2. If there is a missing observation in two consecutive observations of a firm => set the value of asset_ave=".". For example, due to the missing observation of asset in year 2002, the value of asset_av in years 2002 and 2003 are missing.
firm | asset | asset_av | year |
A | 100 | . | 2000 |
A | 200 | 150 | 2001 |
A | . | . | 2002 |
A | 300 | . | 2003 |
A | 50 | 175 | 2004 |
3. If the two consecutive observations for a firm are not for two consecutive years, the asset_av has also been set as missing variables, for example, from the table below, due to years 2001 and 2005 are not two consecutive years, the asset_av receive the missing results for the year 2005
firm | asset | asset_av | year |
B | 100 | . | 2000 |
B | 200 | 150 | 2001 |
B | 100 | . | 2005 |
B | 500 | 300 | 2006 |
B | 400 | 450 | 2007 |
A sample example is
firm | asset | asset_av | year |
2554Q9 | 100 | . | 2000 |
2554Q9 | 300 | 200 | 2001 |
2554Q9 | 200 | . | 2003 |
2554Q9 | 100 | 150 | 2004 |
2564UU | 200 | . | 2000 |
2564UU | 100 | 150 | 2001 |
2564UU | 800 | 450 | 2002 |
2564UU | 1200 | 1000 | 2003 |
2564UU | 300 | 750 | 2004 |
2563CS | 200 | . | 2000 |
2563CS | 400 | 300 | 2001 |
2563CS | . | . | 2002 |
2563CS | 900 | . | 2003 |
2563CS | 100 | 500 | 2004 |
2557AZ | 200 | . | 2000 |
2557AZ | 600 | . | 2002 |
2557AZ | 100 | . | 2004 |
Comment