Stata 15 help for expr_query

Title

[P] expr_query -- Obtain variables used in expression

Syntax

expr_query [=] exp

Description

expr_query returns in r() the variable names used in exp, where exp is an expression that might be used with generate or replace.

The variable names are names of variables in the dataset. They do not include scalar or matrix variables.

Remarks

In some programming situations, there are expressions specified by the user that will be evaluated later, after the dataset in memory has been modified. The list returned by expr_query can be added to the list of variables to be kept.

Examples

We begin with out-of-context examples just to demonstrate the r() values expr_query makes available to you.

. sysuse auto

. expr_query 1/mpg

. return list macros: r(type) : "numeric" r(varnames) : "mpg"

. expr_query weight/(1/mpg)

. return list macros: r(type) : "numeric" r(varnames) : "mpg weight"

. expr_query "car " + lower(make)

. return list macros: r(type) : "string" r(varnames) : "make"

. expr_query 1/sqrt(2)

. return list macros: r(type) : "numeric" r(varnames) : " "

expr_query is a programming command. Typical use would be something like this:

program myprog version 15.1 syntax varlist =exp [if] [in], ... marksample touse ... expr_query `exp' local uservars `r(varnames)' ... local vars_to_keep : list varlist | uservars preserve keep if `touse' keep `vars_to_keep' ... tempvar weight gen double `weight' `exp' /* <--- */ ... ... end

In the above example, the user specifies, among other things, an expression on the myprog command line. Command syntax parses what the user types. expr_query provides the list of variables that the user-specified expression uses; myprog then makes a list of variables to be kept -- variables the program will need -- ignoring the expression and presumably based on other parts of the syntax that the user provided. Finally, the variables-to-keep list and the variables used in the expression are combined with the logical operator | (or), producing the union of the two lists. myprog preserves the user's data, keeps the variables it would need, and does whatever other work it needs to do. Eventually (we marked the line with an arrow), myprog generates a temporary new variable based on the user-specified expression. The generate command will be successful because in vars_to_keep we included any variables the user-specified `exp' required.

Stored results

expr_query produces no output but stores the following in r():

Macros r(type) numeric or string r(varnames) space-delimited variable-name list; contains a single blank (" ") if no variables are used in exp r(tsvarlist) time-series variables list r(fvvarlist) factor variables list

Diagnostics

expr_query produces the same error messages and nonzero return codes that generate or replace would produce when exp contains an error.


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