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

Re: st: RE: Multiple condition statement


From   Hannah Moore <[email protected]>
To   [email protected]
Subject   Re: st: RE: Multiple condition statement
Date   Thu, 12 Aug 2004 12:42:29 +0800

That all seems to make sense to me - the forval bit but STATA does not like the line
replace entry = date'i' if var1=='i'

It says date'i' invalid name.

any suggestions?




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
[email protected]

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/

--
_________________________________________
Hannah Moore BSc(Hons)
Research Assistant

InterRett - IRSA Rett Phenotype Database
Australian Rett Syndrome Study

Telethon Institute for Child Health Research
PO Box 855, West Perth
AUSTRALIA, 6872
Ph: +61 (8) 9489 7781
Fax: +61 (8) 9489 7700
Mob: 0409 100 007
Email: [email protected]
:)
__________________________________________

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