Statalist The Stata Listserver


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

RE: st: Using gen / egen in one step (not two)


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Using gen / egen in one step (not two)
Date   Sun, 7 Jan 2007 18:52:38 -0000

Sorry, but the distinction is not subtle at all, but utterly 
stark. An expression
is very general. You can use expressions in many places, not
just with -generate-. A -egen- function call with parenthesised 
arguments is much more specialised. Nor does it qualify 
as an expression, as only with -egen- is it legal. 

It's a long hard battle, but the game pivots on grasping 
that it's Stata's syntax that counts, not the syntax you 
guess will work or the syntax you would like to have. 

Nick 
[email protected] 

Jeremy Miles
 
> Thanks for that - it makes sense, when you put it like that, although
> it's a pretty subtle distinction in the manuals [-gen- uses =exp ;
> -egen- uses =fcn(arguments)].
> 
> When there's a Wiki, I'll write that in it.  :)
 
> On 07/01/07, n j cox <[email protected]> wrote:
> > There is no need for guessing on this. If you
> > look directly at the help you can see that the
> > syntax of -egen- is
> >
> > egen [type] newvar = fcn(arguments) [if] [in] [, options]
> >
> > and so there is no scope for anything on the right-hand
> > side other than -if- or -in- stuff or options.
> >
> > There is really is a world of difference between functions
> > (strict sense) and -egen- functions: in several ways use
> > of the same term is unfortunate, although a different
> > term would also not have been desirable.
> >
> > In many contexts functions can be combined pretty well
> > arbitrarily with all kinds of other stuff in complicated
> > expressions, but with -egen- functions there is no
> > hidden flexibility -- except, as Scott Merrymsn indicated,
> > the flexibility you provide yourself as programmer
> > through options. Otherwise what you want requires two
> > lines.
> >
> > Nick
> > [email protected]
> >
> > Sergio Correia
> >
> > I don't think you can't do it in one line. Even if you 
> could, I think
> > that it would be a bad idea for more advanced uses of 
> -egen-. Imagine
> > trying to understand (or even program!) something like:
> >
> > egen x = 2+5*log(seq(), from(1) to(10) by(region))
> >
> > On the other hand, with -generate- you can mix all the functions you
> > want, like in:
> > generate x = 3 * (price + mpg) / 2
> >
> > Which is similar to what you did, except for the treatment 
> of missing
> > values.
> >
> > Jeremy Miles
> >
> > Am I missing something obvious here, when I try to combine two steps
> > into one line:
> > . sysuse auto
> > . egen x = rowmean(price mpg) * 3
> >
> > Stata says:
> > varlist not allowed
> > r(101);
> >
> > For:
> > . egen x = 3 * rowmean(price mpg)
> >
> > Stata says:
> > r(198);
> > (Invalid syntax)
> >
> > I can do it in two steps, but that seems inelegant.  Have I missed
> > something obvious?

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