lbl_list_matching_var_lbls
Title
lbl_list_matching_var_lbls - Identify variables whose label matches a pattern.
Syntax
lbl_list_matching_vars pattern(string), [negate varlist(varlist)]
options | Description |
---|---|
pattern | Provides to find in the variable label. Provide either a substring or a regular expression. |
negate | Returns variables whose label does not match |
varlist(varlist) | Restrict the scope of variables to consider |
Description
Stata provides relatively few and mostly imperfect tools for searching variable labels for a matching string. The label tools do not offer any methods for querying the variable labels. The lookfor command, while the nearest match, falls short in a few ways. First, it searches over both variable names and variable labels. Second, it does not provide a means for restricting the scope of search to a variable list. And third, it fails to provide a means for inverting the search (i.e., returning everything that does not match).
This function to fill the gap the following gaps:
- Search over variable labels only
- Specify search through (regex) patterns
- Restrict search to an (optionally) user-provided variable list
- Invert search to identify variables whose variables do not match
Options
pattern(string) provides the text pattern to find in the contents of variable labels. Rather than the traditional Stata glob pattern, this pattern is a sub-string or a regular expression.
negate inverts the match. Rather than return variables with matching variable labels, this option returns variables whose variable label do not match.
varlist(varlist) restricts the scope of the search to the user-provided variable list. By default, the command searches for matches in all variables in memory. With varlist(), the scope of the search can be narrowed.
Examples
Example 1: Simple search
* create a set of variables
gen var1 = .
gen var2 = .
gen var3 = .
gen var4 = .
* apply variables
label variable var1 "First label"
label variable var2 "2. label"
label variable var3 "3. label"
label variable var4 "Fourth label"
* find variables whose label contains "First"
lbl_list_matching_vars, pattern("First")
Example 2: Regex search
* find variables whose labels start with a number
lbl_list_matching_vars, pattern("^[0-9]")
Example 3: Restrict search to a variable list
* find variables whose label starts with "F" in var1 - var3
lbl_list_matching_vars, pattern("^F") varlist(var1 - var3)
Example 4: Return variables whose labels do not match
* find variables whose labels do NOT start with a number
lbl_list_matching_vars, pattern("^[0-9]") negate
Feedback, bug reports and contributions
Read more about these commands on this repo where this package is developed. Please provide any feedback by opening an issue. PRs with suggestions for improvements are also greatly appreciated.