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

From |
adiallo5@worldbank.org |

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 14:43:56 -0400 |

Dear Stata Users, Many thanks to David for his help. 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). 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) The solution must perhaps be easy, but I cannot figure how to reach it. Thank you all for the help. Amadou. AFTHD, The World Bank. David Kantor <dkantor@jhu.edu> To: Statalist@Hsphsun2.Harvard.Edu Sent by: cc: owner-statalist@hsphsun2. Subject: Re: st:How to do analysis if the same variable exists in one dataset and harvard.edu is missing or contains no observation in another database? 08/05/2003 06:42 PM Please respond to statalist To Adiallo5: -confirm existence- will just confirm that there is anything at all following it, so confirm existence (`x') will always be true. You probably intended... capture confirm var `x' But then, it is inside a -foreach- that loops through a varlist, so `x' will always be a variable (assuming that v* does expand to at least one variable). So that doesn't do any good either. Perhaps you have a specific list of variables in mind, whose elements may or may not be present an any given dataset. Suppose this is in a local called vlist1. You would assign its value in a -local- command. Note that it is not a varlist in the true sense, as some elements possibly don't exist as variables for the current dataset. Then you would do.. foreach x of local vlist1 { capture confirm var `x' if _rc==0 { ... do whatever you want with `x' ... } else { display "`x' does not exist" } (Note that is -if _rc==0-, rather than -if _rc!=0-.) If you want to separate out the variables that are completely missing, you would want to do... foreach x of local vlist1 { capture confirm var `x' if _rc==0 { capture assert mi(`x') if _rc==0 { disp "`x' is all missing" } else { ... do whatever you want with `x' ... } } else { display "`x' does not exist" } I hope this is what you were looking for. -- David At 06:08 PM 8/5/2003 -0400, you wrote: >Dear stata users, > >I have the same survey conducted in several developing countries. >The databases contain almost the same variables (most of the time >coded the same way, but differently sometimes). > >In each database, I want to tell stata to check for a variable, and if it does >exist or does >contain observations, to perform some task. > >I coded for example: > >foreach x of varlist v* { > capture confirm existence (`x') > if _rc!=0 { > sum `x' > recode `x' 1=1 2/3=2 4/*=3 > } > else { > display "`x' does not exist or may contain no observation" >} > >But this does not seem to work. I am using stata 8. > >How to do if the codification for the same variable is different across >databases? > >Many thanks. David Kantor Institute for Policy Studies Johns Hopkins University dkantor@jhu.edu 410-516-5404 * * 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/ * * 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/

**Follow-Ups**:**Re: st:How to do analysis if the same variable exists in one datasetand is missing or contains no observation in another database?***From:*David Kantor <dkantor@jhu.edu>

**RE: st:How to do analysis if the same variable exists in one dataset and is missing or contains no observation in another database?***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

- Prev by Date:
**RE: st: RE: interpreting output of kdensity command** - Next 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?** - Previous by thread:
**st: RV: endogeneity** - Next 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?** - Index(es):

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