[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Obtain the list of variables used in an if condition

From   "Nick Cox" <>
To   <>
Subject   st: RE: Obtain the list of variables used in an if condition
Date   Wed, 12 Aug 2009 21:13:43 +0100

I don't know a neat way to do this. Among other problems, a complete
solution would need to consider that  

1. An -if- condition might contain temporary variable names, too. Your
logic seems to hold despite that. 

2. An -if- condition could refer to r(), e(), c() and Stata matrices.
Your logic might need some modification there. 


Sergiy Radyakin

how do I get the list of variables used in an if-condition passed to
my Stata program?
obviously all sorts of logical expressions and functions are permitted
in the if-condition.

program foobar
    syntax [if]

    // ... determine which vars are used in the if-condition and put
them in var_list ...
    // ... compute something ...


I am looking for an elegant solution, the one that would not involve,
say, dropping vars one
by one and checking if the expression is still computable.

One solution is to replace all the ! ~ ^ + - * / = < > ] and )  to
spaces, then look at the tokens left.
Those with ( immediately following are function names, those with [ or
a space immidetely
following are variables. (and we can use -capture confirm variable- to
check that).

Is there any problem with such an approach? (e.g. factor variables in
Stata 11?)
Is there a better (or a standard/existing) approach to the problem?

*   For searches and help try:

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