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   Phil Clayton <[email protected]>
To   [email protected]
Subject   Re: st: How to check if observation of a particular variable value exists? If not, create that observation.
Date   Tue, 29 May 2012 19:18:54 +1000

Here's a tweak of your existing code:

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 `=_N'
     replace B = `j' in `=_N'
   }
 }
}

Phil

On 29/05/2012, at 7:09 PM, Ran Gu wrote:

> Dear Statalisters:
> 
> Good day. I hope you can help me with the following question.
> 
> 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'
>    }
>  }
> }
> 
> 
> -- 
> All the best,
> Ran
> *
> *   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/

*
*   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