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   Thu, 07 Aug 2003 15:32:20 -0400

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/



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