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 dataset and is missing or contains no observation in another database?


From   [email protected]
To   [email protected]
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 19:29:48 -0400

Thanks Nick and David for the help.

David, I mean the exact correspondence: var1 for a, var2 for b, etc.

If a variable is absent, it would be preferable not to create it, since the
program is huge.
For example, if a does not exist or contains no observation, thus, it would be
preferable not to create var1.
Even though we create new variables with missing values, they would be
irrelevant for my regressions.

For var5, if at least one of the variables exists, then I want to use it to
create var5. If not, it will not be created.

Nick, all the code is there. My intend is simple.
Let me rewrite all my program below: my dependent variable is depvar (which is
common to all files).

local varlist1 "a b c d e f g h"
foreach x of local varlist1 {
capture confirm var `x'
if _rc==0 {
          capture assert mi(`x')
          if _rc==0 {
                    drop `x'
         }
else {
         g var1=a
         g var2=b
         g var3=c
         g var4=d
         g var5=.
         replace var5=1 if e==1| f==1| g==1| h==1
        }
   }
regress depvar var1 var2 var3 var4 var5     /*if one of them exists*/

The first suggestion of Nick seems good, but since I have a lot of variables to
create, it will be very difficult to rewrite the code for each of them.
I will try his second suggestion.

Best regards.

Amadou DIALLO,
AFTHD, The World Bank.



                                                                                                                                           
                      David Kantor                                                                                                         
                      <[email protected]>                                                  To:  [email protected]               
                      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/07/2003 03:32 PM                                                                                                  
                      Please respond to                                                                                                    
                      statalist                                                                                                            
                                                                                                                                           
                                                                                                                                           
                                                                                                                                           
                                                                                                                                           




By this ...
  g var1=a
  g var2=b
  g var3=c
  g var4=d

do you mean that this exact correspondence should always hold? -- or are
the new variables filled in the order that original variables appear?  That
is, what happens with absent variables?  Suppose, for example, that b is
absent.  Do you want...
  g var1=a
  g var3=c
  g var4=d
or do you want...
  g var1=a
  g var2=c
  g var3=d

?

Also, for the absent original variable, do you want the corresponding new
variable (var2 or var4 in the above examples) to be...
   not created ?
   created and filled with missing values ?

Similarly, if none of the original variables exist, then do you want var5
to be...
   not created ?
   created and filled with missing values ?

If you can answer these, I'll see what I can do.
-- David

At 02:43 PM 8/7/2003 -0400, you wrote:
>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
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/






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