<p>Not sure if this is on-topic, but I'll give a simple solution. It's perhaps not the most efficient, but should be easy enough to follow.</p>
<p>Assuming you also want to keep &quot;category&quot; and not just &quot;id&quot;, how about something like the following:</p>
<pre><code>use sample.dta, clear
tempfile month10
preserve
keep if month==8 | month==9
collapse value , by(year category id)
gen month=10
save `month10' , replace
restore
append using `month10'
</code></pre>

CC BY-SA 4.0
2020-10-21T21:10:36.453Z
2020-10-21T21:10:36.453Z
BB King
<p>this is my sample dataset</p>
<p><a href="https://i.sstatic.net/TQnp8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/TQnp8.png" alt="enter image description here" /></a></p>
<p>How can I add for each id a new observation for 2020 month 10 which would be the average of 2020 month 8 and 2020 month 9?</p>
<p>I have tried the following, however, it almost works but it does not assign category name to the corresponding id value and the average is not taken correctly</p>
<pre><code>egen year_m_id=group(year month) /* 127 &amp; 128 correspond to 2020 month 8,9 */
global id=r(levels)
foreach id of global id{
generate tmp=category if id==`id'
local 2020_9=_N+1
set obs `2020_9'
local 2020_9=_N
replace id =`id' if _n==`2020_9'
sum value if year_m_id===127|year_m_id==128 &amp; id == `id'
replace value = r(mean) if _n==`2020_9' &amp; id == `id'
replace year = 2020 if _n==`2020_9' 
replace month = 2020 if _n==`2020_9' 
replace category = tmp if _n==`2020_9' &amp; category==&quot;&quot; &amp; _n==`2020_9' 
drop tmp
}
</code></pre>

CC BY-SA 4.0
2020-10-21T17:03:24.067Z
Can you show us what did you tried so far?
1muflon1
CC BY-SA 4.0
2020-10-21T19:37:10.057Z
Please see my edit
user30936
<stata>
How to add additional observations to panel data?
user30936
# How to add additional observations to panel data?

(Asked by: username_0 on 2020-10-21)

this is my sample dataset

[![enter image description here](https://i.sstatic.net/TQnp8.png)](https://i.sstatic.net/TQnp8.png)

How can I add for each id a new observation for 2020 month 10 which would be the average of 2020 month 8 and 2020 month 9?

I have tried the following, however, it almost works but it does not assign category name to the corresponding id value and the average is not taken correctly

```
egen year_m_id=group(year month) /* 127 & 128 correspond to 2020 month 8,9 */
global id=r(levels)
foreach id of global id{
generate tmp=category if id==`id'
local 2020_9=_N+1
set obs `2020_9'
local 2020_9=_N
replace id =`id' if _n==`2020_9'
sum value if year_m_id===127|year_m_id==128 & id == `id'
replace value = r(mean) if _n==`2020_9' & id == `id'
replace year = 2020 if _n==`2020_9' 
replace month = 2020 if _n==`2020_9' 
replace category = tmp if _n==`2020_9' & category=="" & _n==`2020_9' 
drop tmp
}

```

**Tags:** stata

> **Comments:**
> **username_1**: Can you show us what did you tried so far?  
> **username_0**: Please see my edit  

---

## 1 answers

### Answer by username_2 on 2020-10-21. Score: 2 | Accepted answer

Not sure if this is on-topic, but I'll give a simple solution. It's perhaps not the most efficient, but should be easy enough to follow.

Assuming you also want to keep "category" and not just "id", how about something like the following:

```
use sample.dta, clear
tempfile month10
preserve
keep if month==8 | month==9
collapse value , by(year category id)
gen month=10
save `month10' , replace
restore
append using `month10'

```


---

