# st: RE: newbie if {} else {} question

 From "David Moore" To statalist@hsphsun2.harvard.edu Subject st: RE: newbie if {} else {} question Date Thu, 19 Sep 2002 12:51:14 -0700

```This is a common error.  You're using a programming if statement when you
really want a transformation if clause.  Stata is unique, in my experience,
with this usage, but it's similar to a conditional transformation in SPSS
(if that helps).  Basically, you want to add your if conditions to the gen
statement itself as in:

gen ascertgp=1 if probip==1 & promig==1

If you're working with dummy variables and there are no missing values, then
what you've written handles all of the possibilities and you can simply
program the remaining transformation statements using replace rather than
generate, as in:

replace ascertgp=2 if probip==0 & promig==1
replace ascertgp=3 if probip==1 & promig==0
replace ascertgp=4 if probip==0 & promig==0

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Richard
Herrell
Sent: Thursday, September 19, 2002 12:27 PM
To: statalist
Subject: st: newbie if {} else {} question

I've just started learning Stata syntax after long years of SAS use and am
completely stumped by a result.  I want to create a new 4-level variable
(ascertgp) based on the crosstabulation of 2 dichotomous variables (probip
and promig).  This is the syntax I wrote:

if      probip==1 & promig==1 {
gen ascertgp=1
}
else if probip==0 & promig==1 {
gen ascertgp=2
}
else if probip==1 & promig==0 {
gen ascertgp=3
}
else if probip==0 & promig==0 {
gen ascertgp=4
}

When I run tab1 ascertgp I get a value of 3 for every observation.  (I
know what the 4 frequencies should be from the crosstab.)  I must be doing
something very obviously wrong, but can see what it is.

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

• Follow-Ups: