Hello everybody!
I would kindly need if you could help me with an issue.
I have a cross-section dataset, in which I have an ID and a variable containing numerical values (let's say "Var_1"). I would like to generate a variable (let's say X) which provides the sum of the first three elements of Var_1, as depicted below:
I tried with egen X = sum(cond(ID<4,sum(Var_1),0)) but it doesn't work.. maybe it is trivial (and I apologize if this is the case), but I can't obtain the right code :/
Many thanks in advance!
Kodi
Ps.: if I had to do the same thing but in a panel data, which would be the code? I guess it would be the same code but starting with "bys date:" (?)
I would kindly need if you could help me with an issue.
I have a cross-section dataset, in which I have an ID and a variable containing numerical values (let's say "Var_1"). I would like to generate a variable (let's say X) which provides the sum of the first three elements of Var_1, as depicted below:
ID | Var_1 | X |
1 | 1 | 7 |
2 | 4 | 7 |
3 | 2 | 7 |
4 | 5 | 7 |
5 | 4 | 7 |
... | ... | 7 |
I tried with egen X = sum(cond(ID<4,sum(Var_1),0)) but it doesn't work.. maybe it is trivial (and I apologize if this is the case), but I can't obtain the right code :/
Many thanks in advance!
Kodi
Ps.: if I had to do the same thing but in a panel data, which would be the code? I guess it would be the same code but starting with "bys date:" (?)
Comment