Statalist The Stata Listserver


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

Re: st: Two Easy Questions


From   n j cox <[email protected]>
To   [email protected]
Subject   Re: st: Two Easy Questions
Date   Tue, 31 Oct 2006 21:37:23 +0000

Some answers below.

Nick
[email protected]

Socrates Mokkas

------------------------------ #1
I need to write down a command that has too many variables but I do not want to write down all of the variables that I use. Suppose that I have var1, var2,...to Var12 and I want to have a command like:

replace Dot=Quick if var==0 & var2==0 & ....& var12==0.
Is there a way to make a shortage of that? I tried possible different varlist definitions like:

replace Dot=Quick if var*=0

but they do not seem to work.
-----------------------------

Correct. Wildcards are only allowed where varlists are allowed
and an -if- condition isn't one of those places.

All the variables have to be zero. Caleb Southworth posted
a solution of checking for a zero row total, which is fallacious
as there are many sums of positive and negative values that
are zero, although they might not occur in your data, and
so his method might select the right observations in your case.

Sounds as if there should be a quick way, but in any language
some things cannot be said concisely. Perhaps this is one of them.

You could do this.

gen byte OK = 1

qui foreach v of var var1-var12 {
replace OK = 0 if `v' != 0
}

replace Dot = Quick if OK

Thus each value of -OK- will stay at 1 if and only if all corresponding values of -var*- are 0.


----------------------------------------- #2
An additional question is that I tried to write a the commands within an if but Stata is not doing anything. So I wrote:

if var1==0 & var2==0 {
replace Dot=Quick
}

and it is not doing any changes whasoever. Whereas if I write it

replace Dot=Quick if var1==0 & var2==0

it does the changes. Is there a problem with my syntax of the if command?
-----------------------------------------

Yes, there is a problem. These are not equivalent. This FAQ, also referred to earlier today, explains:

FAQ . . . . . . . . . . . . . . . . . . . . . if command vs. if qualifier
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . J. Wernow
6/00 I have an if command in my program that only seems
to evaluate the first observation, what's going on?
http://www.stata.com/support/faqs/lang/ifqualifier.html




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