Good Morning world

English is not my language so google help me. Please understand me.

I have stata 15.

I`m using panel data with 191 variables and this data has been investigated for about 22years

ID goes from 101 to 102, 103...... like this.

wave is time variable

Please check the attached file for the data shape. Array



Here`s my question.

If var1 has 2 or 3, I'd like to keep all the IDs.

If var1 doesn't have 2 or 3, I want to get rid of all of that ID.

So I wrote down like

Code:
gen nid=ID if var1 == 2 | var1 == 3
gen nnid =1 if nid < .
I don't know what kind of logic to proceed from now on.

Using loop(ex.forvalues) and "keep if" don't seem to work properly.

Code:
gen nid=ID if var1 == 2 | var1 == 3
gen nnid =1 if nid < .
keep if nnid == 1
OR

Code:
gen nid=ID if var1 == 2 | var1 == 3
gen nnid =1 if nid < .
forvalues wave = 1/22{
    keep if nnid == 1
    }
It`s not work and I know why this code doesn`t bring me to answer

But I don`t know how do i solve this case.

Please give me an advice on what logic to proceed with.

I hope this article followed the Advice on posting to Statalist.

Thankyou