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

st: RE: Generate


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Generate
Date   Wed, 23 Feb 2005 17:00:49 -0000

Stata has a construct 

if ... { 
	... 
} 

but it is not what you want here. Two 
rules seem immediately pertinent: 

1. when you test for equality, you must use == not =

AND  

2. once a variable has been -generate-d, any 
further changes must be through -replace-. 

gen var1 = varold01 if nivel==1 & area==1 & sex==1
gen var2 = varold02 if nivel==1 & area==1 & sex==2
 
gen var3 = varold03 if nivel==1 & area==2 & sex==1

replace var2 = varold02 if nivel==1 & area==2 & sex==2

gen var4 = varold04  if nivel==1 & area==1
gen var5 = varold05  if nivel==1 & area==2

Nick 
n.j.cox@durham.ac.uk 

GLORIA ZAMBRANO ROZAS

> I am new in stata and want to generate several new variables, 
> some of them
> under  the same conditionals, something like this:
> 
> gen var1= varold01  if  ( nivel=1 & area=1 & sex=1)
> gen var2=varold02   if ( nivel=1 & area=1 & sex=2)
> 
> gen var3= varold03  if  ( nivel=1 & area=2 & sex=1)
> gen var2=varold02   if ( nivel=1 & area=2 & sex=2)
> 
> gen var4= varold04  if  ( nivel=1 & area=1 )
> gen var5=varold05   if ( nivel=1 & area=2 )
> 
> I want to know if stata has a command that let me do 
> something like this:
> 
>       if nivel=1 & area=1 then do;
> 
>                 gen var2=varold02   if (sex=2)
>                 gen var4= varold04
> 
>        if nivel=1 and area=2 then do;
> 
>                gen var3= varold03  if  (  sex=1)
>                gen var2=varold02   if (  sex=2)
>                gen var5=varold05
> 
> that let me reduce the number of conditionals

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index