1iqv1g

Q: How do I keep the first observation?

A: Often when dealing with certain list of filings, we want to keep either the first or the last observation. For instance, in my IPO research, I am interested in keeping the first and the last prospectuses. So how do we get this done?

Well, let’s use the _n and _N. _n counts the position of the observation (first, second, third…). _N counts the total number of observations.

Let’s use the auto.dta

****************************************

clear

sysuse auto.dta

**Let’s examine only the first observation

list if _n==1

***this will show the first observation.

**Now, let’s see the last observation

list if _n==_N

***The command says show the last observation which is 74.

**Now let’s see the first and last observations

list if _n==1 | _n==_N

***The command says show the first and last observations which are 1 and 74.

**Let’s keep only the first and last observation.

keep if _n==1 | _n==_N

***You will only keep the first and last observation

 

Advertisement