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

RE: st: multiple )))brackets, is there a more efficient way?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: multiple )))brackets, is there a more efficient way?
Date   Tue, 25 May 2004 11:28:04 +0100

I think Ulrich went to the heart of the matter: 
this really pivots on taste. However, that is 
linked partly to readability. Moreover, it 
is also important to have code that you 
can understand, can check, can debug, and can come 
back to and revise. If you are in a group, it 
is naturally also important that others can 
understand your code. 

-cond()- can be a natural way of doing it, and 
there are problems in which it offers a very 
elegant solution. Your problem appears so 
messy that breaking it up into bits as Ulrich 
suggested is certainly what I would prefer. 

As for (machine) efficiency, my guess is that the 
advantage must lie with a single statement. 
Stata is clearly much faster and much better 
at interpreting a Stata command than people 
are, but doing it through multiple statements 
will usually be slower in machine time. However, 
if it takes the user longer to write the code, 
that is no advantage. 

The problem of balancing parentheses is 
lessened in any good text editor which will 
have a facility for finding a matching parenthesis
and a way of highlighting an unmatched parenthesis. 

David Kantor is very fond of -cond()- and 
may offer counter-arguments. 

Nick 
[email protected] 

Annelies Vos
 
> Thanks Ulrich,
> I liked the cond () solution because I didn't know this one...
> This seems much more efficient.

Ulrich Kohler 

> > I don't know why you like the solution with cond(), but I prefer a
> > solution
> > with -inlist()- or -inrange()- for tasks like this this. 
> > With inlist() your
> > your code-snippet might be coded like this:
> >
> > gen byte origin = 7 if nation == 3
> > replace origin = 10 if inlist(nation,8,12,69,139,141)
> > replace origin = 8 if inlist(nation,14,82)
> > ...
> >
> > Others might propose a solution with -recode- but thats a 
> matter of taste.
> > In any case: see -help inlist- or -help recode- for more.

Annelies Vos

> >> in the FAQs I found the following very useful recommendation:
> >> instead of:
> >>            . generate byte a = 1 if y <= 20
> >>            . replace a = 2 if y > 20 &  y <= 30
> >>            . replace a = 3 if y > 30 & y <= 40
> >>            . replace a = 4 if y > 40 & y <.
> >>
> >> do the following:
> >>
> >>            . #delim ;
> >>            . generate byte a =
> >>              cond(y<=20, 1,
> >>              cond(y<=30, 2,
> >>              cond(y<=40, 3,
> >>              cond(y<., 4,
> >>                . ))));
> >>
> >> However, the variable I want to use it for (nationality) has many
> >> values (every country in the world), which should be recoded into
> >> countrygroups. I don't really like the idea of having to count the
> >> number of "opening brackets": "(" , to know with how many "closing
> >> brackets": ")" I should end. Is there any easier solution for this?
> >>
> >> to explain a piece of my syntax:
> >> > #delim;
> >> > generate byte origin =
> >> > cond(natio==3, 7,
> >> > cond(natio==8, 10,
> >> > cond(natio==12, 10,
> >> > cond(natio==14, 8,
> >> > cond(natio==28, -9,
> >> > cond(natio==54, 6,
> >> > cond(natio==69, 10,
> >> > cond(natio==82, 8,
> >> > cond(natio==139, 10,
> >> > cond(natio==141, 10,
> >>
> >> ...etcetera
> >>
> >> ...which I would like to end on another way than:
> >> > . ))))))))))

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