Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

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 <[email protected]>
To   [email protected]
Subject   Re: st:How to do analysis if the same variable exists in one datasetand is missing or contains no observation in another database?
Date   Tue, 05 Aug 2003 18:42:54 -0400

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
[email protected]
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/



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