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

Re: st: if/else command

From   Ronán Conroy <[email protected]>
To   [email protected]
Subject   Re: st: if/else command
Date   Thu, 06 Jan 2005 15:28:06 +0000

Ulrich Kohler wrote:

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 ;


Another solution is to use -recode-, which is less prolix

gen age_group=age
recode age_group [recoding rules omitted] if certification==1
recode age_group [recoding rules omitted] if certification==2

Beware of the line replace age = 50 if age > 20 & certification == 1
If age is missing, the condition (age > 20) is true.

I always suffix lines with

if ~missing(variable_being_manipulated)

even when I know it has no missing values. Natural gloom, I suppose.
Ronan M Conroy ([email protected]) Senior Lecturer in Biostatistics Royal College of Surgeons Dublin 2, Ireland +353 1 402 2431 (fax 2764) -------------------- Just say no to drug reps

This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom
they are addressed.
If you have received this email in error please notify the
originator of the message. This footer also confirms that this
email message has been scanned for the presence of computer viruses.

Any views expressed in this message are those of the individual
sender, except where the sender specifies and with authority,
states them to be the views of The Royal College Of Surgeons in Ireland.


* For searches and help try:

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