Search code examples
stata

Count the missing values in panel


I have a data set that looks like this:

id     A
1      5
1      5
1      .    
1      5
5      .
5      .   
5      8
13     .
13     .
13     .
13     .

I want to calculate the number of A values when at least one A value is not missing in that panel in Stata. For example, in the example above there are 3 missing values that are not the only missing value in that panel.

There is one missing A value when id is 1 and as there also are non-missing A values when id=1, I want to count that one.

Similarly, there are two missing A values when id is 5 and as there are also non-missing values when id=5, I want to count those two too.

There are 4 missing A values when id=13 but as there are no non-missing values when id=13, I don't want to count these.


Solution

  • I can't follow this, but the number of observations in each panel is

      bysort id : gen count = _N 
    

    and the number of non-missing values of A is

      by id : egen A_nm = count(A) 
    

    from which the missing values can be counted by subtraction. Alternatively, missing values can be counted directly by

      by id: egen A_m = total(missing(A)) 
    

    If that doesn't help, you may have to expand your question by showing what the new variable(s) you want looks like.

    EDIT What you want may be just an application of this: you want to look at A_m values conditional on A_nm being positive.