[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
RE: st:How to do analysis if the same variable exists in one dataset and is missing or contains no observation in another database? |

Date |
Thu, 7 Aug 2003 20:10:28 +0100 |

adiallo5@worldbank.org > > But I still have some troubles. I want to create a unique > and global do file for > twenty datasets from the same survey with almost the same variables. > > Suppose that I have the following local macro containing > variables that may be > present or not in a giving database (see below): > > local mylist "a b c d" > > I have 3 questions: > > 1/ I want to check for the presence of the 4 variables of > the macro in the > dataset. I thus apply David 's suggestions: > > foreach x of local mylist { > capture confirm var `x' > if _rc==0 { > capture assert mi(`x') > if _rc==0 { > drop `x' > } > > > 2/ For the remaining variables, I want to take each one and > create a new > variable in the giving order : var1 with a, var2 with b, > etc,... if they exist : > > else { > g var1=a > g var2=b > g var3=c > g var4=d > } > } > > > > My problem at this point is that I got the error message: > var1 already exists > (probably because of the looping). This must reflect some code you are not showing us, so it is difficult to make suggestions. > 3/ In the third stage, I want to create more complicated variables: > > > > g var5=. > replace var5=1 if a==1| b==1| c==1| d==1 > > My problem is how to tell Stata to check for a, b, c, d and > do the following: > create var5 with the four variables in the datasets where > they all exist, > create var5 with any combination of them, if they do exist > (at least one of > them) > or create nothing if none of them do exist (to avoid my > program to stop) Perhaps another approach would help. Let's do it one by one: capture confirm var a if _rc == 0 { gen var1 = a local OKlist "a" } capture confirm var b if _rc == 0 { gen var2 = b local OKlist "`OKlist' b" } capture confirm var c if _rc == 0 { gen var3 = c local OKlist "`OKlist' c" } capture confirm var d if _rc == 0 { gen var4 = d local OKlist "`OKlist' d" } What we're doing is testing whether a variable exists if it does { use it to generate another one add it to the list of variables which exist } ===================================== Aside: this can be re-expressed more concisely, but possibly more cryptically: tokenize a b c d forval i = 1/4 { capture confirm var ``i'' if _rc == 0 { gen var`i' = ``i'' local OKlist "`OKlist' ``i''" } } ==================================== if trim("`OKlist'") != "" { egen var5 = eqany(`OKlist'), v(1) * next line optional replace var5 = . if var5 == 0 } Nick n.j.cox@durham.ac.uk * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**References**:

- Prev by Date:
**Re: st:How to do analysis if the same variable exists in one dataset and is missing or contains no observation in another database?** - Next by Date:
**Re: st:How to do analysis if the same variable exists in one datasetand is missing or contains no observation in another database?** - Previous by thread:
**Re: st:How to do analysis if the same variable exists in one dataset and is missing or contains no observation in another database?** - Next by thread:
**Re: st:How to do analysis if the same variable exists in one datasetand is missing or contains no observation in another database?** - Index(es):

© Copyright 1996–2014 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |