The programming function
if <exp> {
<code>
}
evaluates a _single_ logical expression <exp> and executes <code> if it is true, it does not evaluate <exp> on individual observations.
In your example, with <exp> being certification==1, the variable certification is only being evaluated on the first observation in your data (equivalent to certification[1]==1). To get what you want, you would need to program
replace age = 22 if age <= 5 & certification==1
replace age = 30 if age >= 6 & age <= 15 & certification==1
etc
Alternatively, you could do this in fewer steps with the command -recode-.
David
[email protected]
-----Original Message-----
From: Anek Stata [mailto:[email protected]]
Sent: 06 January 2005 14:54
To: [email protected]
Cc: [email protected]
Subject: st: if/else command
Greetings everyone,
I am using Stata 8.1 on Win2000. My data set contains variable
"certification", which takes values 1 or 2 only. I am trying to group
respondents from these two "certification" sets into age groups. For SOME
REASON, the if command for certification==1 executes all the replace
commands, but when it goes to the second if, none of the replace commands
within the curly brackets get executed.
Below is the code I am using:
----------------------------------------
if certification == 1 { ;
replace age = 22 if age <= 5;
replace age = 30 if age >= 6 & age <= 15;
replace age = 40 if age >= 16 & age <= 20;
replace age = 50 if age > 20;
{ ;
else if certification == 2 { ;
replace age = 22 if age <= 3;
replace age = 30 if age >= 4 & age <= 13;
replace age = 40 if age >= 14 & age <= 24;
replace age = 50 if age == 25;
replace age = 50 if age == 26;
replace age = 50 if age == 27;
replace age = 50 if age == 28;
};
----------------
I have tried to break it on purpose, writing "if certification==3". This
gives me NO ERRORS, even though there is never a value 3 in variable
certification. This is obviously wrong. I have also tried the code with and
without "else" (just trying to execute another if), to no avail. I've tried:
...
replace age = 50 if age > 20;
};
else{;
replace age = 22 if age <= 3;
...
and
...
replace age = 50 if age > 20;
};
if certification==2{;
replace age = 22 if age <= 3;
...
Since the help file is USELESS, I'd like to ask for your help, if you can
spot what I am doing wrong.
Thanks a lot,
Ane
_________________________________________________________________
Powerful Parental Controls Let your child discover the best the Internet has
to offer.
http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines
Start enjoying all the benefits of MSN� Premium right now and get the
first two months FREE*.
*
* 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/