Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: How to check if observation of a particular variable value exists? If not, create that observation.


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: How to check if observation of a particular variable value exists? If not, create that observation.
Date   Tue, 29 May 2012 10:19:30 +0100

If it is really a case of whether you have four records (Stata
terminology is "observations"), you don't need a program, but just to
work in the Editor. But I guess your real problem is more substantial.

Look at the help for -fillin-.

This version of your code should work

qui forvalues i = 1/2 {
             forvalues j = 1/2 {
                     count if A == `i' & B == `j'
                     if r(N) == 0 {
                             set obs `=_N + 1'
                             replace A = `i' in L
                             replace B = `i' in L
                     }
        }
}

It is best not to use "missing" to mean "not present". Missing in
Stata usually means that a value is missing.

Nick

On Tue, May 29, 2012 at 10:09 AM, Ran Gu <lionup@gmail.com> wrote:

> Suppose I have a data with 2 variables, say A and B, both with values
> 1 and 2. The data are sorted by A and B. Then there should be 4
> records: (A=1 B=1) (A=1 B=2) (A=2 B=1) (A=2 B=2)
>
> Suppose I don't know whether the 4 observations are all exist or not.
> How could I check that in a do file?
> Then, If a record is missing, say (A=1 B=1), I would like to create it.
>
> for example:
> forvalues i = 1/2 {
>  forvalues j = 1/2 {
>    if obs with A == `i' and B == `j' is missing {
>      local new = _N+1
>      set obs `new'
>      replace A == `i' if _n==`new'
>      replace B == `j' if _n==`new'
>    }
>  }
> }
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index