[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

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

 From Kaleb Michaud To statalist@hsphsun2.harvard.edu Subject Re: st: newbie if {} else {} question Date Thu, 19 Sep 2002 15:00:30 -0500

Here's a quickee reply (since I imagine there are few more on the way) as I had this problem a lot when I first started to learn Stata.

First - the if and else statements aren't meant to be used with variables in a general sense. When you use the if statement, usually best to use it with a scalar or macro of some sort whereas the variables are best to be used in the function argument of a generate/replace statement.

Second - the reason you're getting everything to be 3 is because the first observation (I'm betting) has probip=1 and promig=0 and so it executes the command gen ascertgp=3 for all observations because the if/else is true.

Here's one way to do all of this using the cond() and inlist function:

gen ascertgp = cond(probip==1, cond(promig==1,1,3),cond(promig==1,2,4)) if inlist(probip,0,1)==1 & inlist(promig,0,1)==1

Easier to understand way might be to generate it the first time and then replace:

gen ascertgo = 1 if probip==1 & promig==1
replace ascertgo = 2 if probip==0 & promig==1
etc.

Good luck!
Kaleb...

At 02:26 PM 9/19/2002 -0500, you wrote:

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

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