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

st: RE: RE: Multiple condition statement


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: Multiple condition statement
Date   Wed, 11 Aug 2004 10:34:49 +0100

Another comment is quite different. If 
this example is your problem, you probably 
would benefit considerably by using -reshape- 
to get a long structure. 

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

> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Nick Cox
> Sent: 11 August 2004 10:30
> To: statalist@hsphsun2.harvard.edu
> Subject: st: RE: Multiple condition statement
> 
> 
> Yes; it is possible in one step. For example, 
> you can use -cond()-. Some users love it, unconditionally, 
> but others love it only under certain limited conditions. 
> 
> gen entry = cond(var1 == 0, entrydate, 
>             cond(var1 == 1, date1, 
>             cond(var1 == 2, date2, 
> 		...
> 		)))))))))) 
> 
> My layout here is designed to show structure, clearly. 
> (or, structure clearly). 
> To Stata this is all one command line. In a do-file 
> or program, I would put some effort in laying it 
> out neatly: otherwise I'd get lost somewhere in 
> the middle. (There is a choice between commenting 
> out ends of line and using -#delimit ;-.) But that 
> takes time.  
> 
> At this point there are various reactions: 
> 
> 1. Great! I can do it in one line. Now how I 
> do become a LISP programmer? 
> 
> 2. How do I check that my parentheses are all 
> balanced? (Any decent text editor will do it. 
> In Stata's do file editor, it's Ctrl-B. In Vim, 
> it's %. ... (If you can't do this within your text 
> editor, it is not a decent text editor (and 
> if you are trying to do this in a word 
> processor, that's a bad idea too).)) 
> 
> 3. Nevertheless I wouldn't do your example this 
> way, even though it has a pretty clear structure. 
> Others might disagree: David Kantor is an 
> articulate proponent of -cond()-, for example.
> 
> I'd do it this way. 
> 
> gen entry = entrydate if var1 == 0 
> 
> forval i = 1/9 { 
> 	replace entry = date`i' if var1 == `i' 
> } 
> 
> Naturally you need to know about -forval- 
> to do it like this. My recommendation assumes
> that, and also is based on the following: 
> 
> * This is more code, but I'm more likely 
> to write it down correctly first time. 
> 
> * If I don't get it right first time,
> it is easier to fix. 
> 
> * This construct in do files, programs, and logs is 
> going to be easier to understand and 
> to modify when revisited days, months, 
> years later. This is especially important 
> if you work in groups and/or your files
> will be inherited or borrowed by others
> who want to understand them (or modify 
> them). 
> 
> Nick 
> n.j.cox@durham.ac.uk 
> 
> Another way is this: 
> 
> gen entry = (var1 == 0) * entrydate 
> 		+ (var1 == 1) * date1 
> 		+ (var1 == 2) * date2 
> 		...
> 		+ (var1 == 9) * date9 
> 
> but you may still have to 
> 
> replace entry = . if entry == 0 
> 
> or at least 
> 
> assert entry > 0 
> 
> I'd still prefer the -forval- way, 
> at least for examples like yours. 
> 
> Hannah Moore
>  
> > Is it possible to have a multiple condition statement?
> > 
> > I would like to generate a new variable:
> > 
> > entry  = entrydate if var1==0, date1 if var1==1, date2 if 
> > var2==2.........., date9 if var9==9
> > 
> > Can someone please tell me whether this is possible in 1 step, and 
> > what the correct syntax would be?
> 
> *
> *   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index