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

Re: RE: st: nesting commands

From   n j cox <>
Subject   Re: RE: st: nesting commands
Date   Tue, 06 May 2008 20:11:25 +0100

I'd put it a slightly different way.

Say you have variables a, b, and you are interested in a - b.
a - b is, in Stata terms, an expression.

a is also an expression. b is also an expression. Expressions thus include individual variable names as one kind of special case.
But as a and b are variables, that is what we usually emphasise.

Tunga's question in these terms is "Where can I use expressions instead of variables in command syntax?".

Maarten's answer is, in effect, almost nowhere in Stata, as Stata isn't
written that way. Maarten is correct, of course.

Another answer is: Wherever the syntax diagram for a command says you can use an expression (and not just a variable). And there are many examples, e.g. within -generate-, many -egen- functions, -anova- (but with only limited kinds of expressions). But that's still only occasionally.

Yet another answer is: You can write a program that feeds on an expression, not a variable. There are several ways to do that.
But on the whole it is usually easiest just to create the variable first, as Maarten says.


Maarten buis replied to Tunga Kantarci

> Suppose I generate a variable and then have a histogram on the
> generated variable:
> gen ab=1-b
> hist ab, disc freq
> Is it possible to nest the gen command in the hist command:
> hist (gen ab=a-b), disc freq
> This is, by command, not possible but what would be the syntax for
> this purpose? I searched for nesting in stata but could not find
> really what I want to do.

There is a good reason why you could not find that, because it can't be
done. The only special case where something like this is possible is
with categorical variables than can be split up in dummies in commands
that allow the -xi- or -xi3- prefix. (-xi- is part of official Stata,
-xi3- is user written and can be downloaded by typing -findit xi3-)
Generally, I think that the absence of this capability is a good thing:
It forces you to write code step by step, leading to more readable,
less bug prone code.

* For searches and help try:

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