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

Re: st: if/else command


From   Ulrich Kohler <[email protected]>
To   [email protected]
Subject   Re: st: if/else command
Date   Thu, 6 Jan 2005 15:52:58 +0100

Anek,

Stata isn't SPSS! 

You have used the command -if-, which is intended for programmers. Your code 
checks, whether the first observation in the variable "certification" has the 
value of 1. If so, the replace commands are executed for _all_ observation in 
the data. Most likely this is not what you wanted. 

Moreover, in SPSS "generating" and "replacing" can be done with the same 
command. In Stata there are two. -generate- generates a new variable, 
-replace- changes the contents of an existing variable. You cannot generate a 
non-existing variable with -replace-.

All togheter what you want might be something like this:

gen age = 22 if age <= 5 & certification == 1 ;
replace age = 30 if age >= 6 & age <= 15 & certification == 1 ;
replace age = 40 if age >= 16 & age <= 20 & certification == 1 ;
replace age = 50 if age > 20 & certification == 1 ;

replace age = 22 if age <= 3 & certification == 2 ;
replace age = 30 if age >= 4 & age <= 13 & certification == 2 ;
replace age = 40 if age >= 14 & age <= 24 & certification == 2 ;
replace age = 50 if inlist(age,25,26,27,28) & certification == 2 ;

Hope this helps.

Uli



Anek Stata wrote:
> 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=ht
>tp://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/

-- 
[email protected]
+49 (030) 25491-361


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