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

Re: st: RE: Multiple condition statement

From   Philip Ryan <[email protected]>
To   [email protected]
Subject   Re: st: RE: Multiple condition statement
Date   Thu, 12 Aug 2004 14:45:07 +0930

Be certain you are using the LEFT single quote as the leading character delimiting the local macro name. That is to say:

... date`i' should work. Fonts don't always translate well on plain text email, so let me spell it out, as it were:<left single quote>i<right single quote>

Mostly, the left single quote is found with the tilde ~ key at the top left of the keyboard


At 12:42 PM 12/08/2004 +0800, you wrote:

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

[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:

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
Ph: +61 (8) 9489 7781
Fax: +61 (8) 9489 7700
Mob: 0409 100 007
Email: [email protected]

*   For searches and help try:
Philip Ryan
Associate Professor,
Department of Public Health
Associate Dean (Information Technology)
Faculty of Health Sciences
University of Adelaide 5005
South Australia
tel 61 8 8303 3570
fax 61 8 8223 4075
CRICOS Provider Number 00123M
This email message is intended only for the addressee(s)
and contains information that may be confidential and/or
copyright. If you are not the intended recipient please
notify the sender by reply email and immediately delete
this email. Use, disclosure or reproduction of this email
by anyone other than the intended recipient(s) is strictly
prohibited. No representation is made that this email or
any attachments are free of viruses. Virus scanning is
recommended and is the responsibility of the recipient.

*   For searches and help try:

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