help grpvars https://github.com/gaksaray/stata-gautils/

Title

grpvars -- Create and manage variable groups

Syntax

grpvars varlist, name(grpname)
[refcat_options]

grpvars add|remove varlist
[, name(grpname)
refcat_options]

grpvars replace
[(]out[)] [(]in[)]
[, name(grpname)
refcat_options]

grpvars list|drop|keep
[, name(grpname)]

grpvars dir

grpvars clear

grpvars vl
[, refcat_options excluding title]

refcat_options

Title
title(string) specify a title for the variable group

prefix(string) specify a string to be added at the beginning of title

suffix(string) specify a string to be added at the end of title

Formatting
latex|tex|html specify whether formatting options are for LaTeX or HTML

italic make title italic

bold make title bold

emspace(#) specify \hspace spacing (only for LaTeX)

Description

grpvars is a collection of commands to create and manage variable groups.
A variable group is essentially a list of variables saved in a global macro
with the name specified by the name() option. grpvars commands can
conveniently manipulate these macros, and maintain their properties in memory.
This is especially useful for managing sets of variables to be used in several
tables. For example, you can specify the base model for the whole project, and
rather than redefining the global macro for the variable list,
add/remove/replace variables according to the specific needs of the current
table. This enables one to more easily narrate the variable selection process.
grpvars also maintains $refcat global macro if title option is
specified, to be used in estout.

grpvars by itself creates (or overwrites to) a variable group. name
option is required.

grpvars add|remove adds or removes varlist to/from the variable group specified by the name option (or all groups if name is not specified).

grpvars replace replaces either a variable, or a sequence of multiple
variables, with the specified variables. Parentheses are not required, except
if a sequence of variables are to be replaced.

grpvars list lists the variables inside the group specified by the
name option. If name option is omitted, it lists all variables in
all variable groups.

grpvars drop drop the group specified by the name option. If
name option is omitted, it drops all groups, i.e., triggers
grpvars clear command.

grpvars keep keeps the group specified by the name option and
drops the rest of the groups.

grpvars clear drops all groups from memory.

grpvars vl picks up all user-defined variable lists saved by the
vl command as variable groups.
It clears all previously saved variable groups,
goes through each variable list defined by vl,
saves their labels as titles (defined by vl label),
and reformats all titles according to refcat_options.
Needless to say, vl set or vl rebuild must be run before running
grpvars vl, and the user-defined variable lists must be labeled.

Options

title(string) specifies a title for the variable group.
The title is added to the $refcat global macro, which includes the first
variable in variable group and the specified title. For example, a variable
group that consists var1 var2 var3 with title Title is going to
be represented as var1 "Title" in $refcat.

The rest of the options modify the title specified by the title option,
and thus have no functionality if title is not specified.

prefix(string) specifies a prefix to be added at the
beginning of title.

prefix(string) specifies a prefix to be added at the
end of title.

latex|tex|html specifies whether the rest of the options are
evaluated as LaTeX or HTML. tex is a synonym for latex.

italic makes the title italic.

bold makes the title bold.

emspace(#) adds \hspace(#em) to the title, if latex or
tex is specified.

Examples

Setup and define a variable group

. grpvars clear
. sysuse auto, clear
(1978 Automobile Data)

. grpvars mpg rep78, name(x_main) title("Main variables")
x_main: mpg rep78

. grpvars dir
vargroups: x_main
refcat: mpg "Main variables"

Add and remove variables

. grpvars add weight length, name(x_main)
x_main: mpg rep78 weight length

We did not need to specify name(), as x_main is the only
variable group, but it is generally better to be explicit. If there were other
groups, weight and length would have been added to all of them.

. grpvars add weight length, name(x_main)
variable weight is already in group x_main
variable length is already in group x_main
x_main: mpg rep78 weight length

. grpvars remove mpg turn, name(x_main)
variable turn not found in group x_main
x_main: rep78 weight length

. grpvars dir
vargroups: x_main
refcat: rep78 "Main variables"

Replace variables

. grpvars headroom trunk, name(x_inter) title("Interior variables")
x_inter: headroom trunk

. grpvars replace headroom turn
x_inter: turn trunk

. grpvars dir
vargroups: x_main x_inter
refcat: rep78 "Main variables" turn "Interior variables"

. grpvars replace (turn trunk) (headroom displacement), ///
> name(x_inter) title("Controls") tex b it em(5)
x_inter: headroom displacement

. grpvars dir
vargroups: x_main x_inter
refcat: rep78 "Main variables" headroom "\hspace{5em}\textbf{\textit{Controls}}"

Using vl subcommand after vl

. grpvars drop, name(x_inter)
. vl create x_other = (gear_ratio displacement)
note: $x_other initialized with 2 variables.

. vl label x_other "Other controls"
. grpvars vl, tex italic
x_other: gear_ratio displacement

. grpvars dir
vargroups: x_main x_other
refcat: rep78 "Main variables" gear_ratio "\textit{Other controls}"

Stored results

grpvars maintains two global macros, in addition to those created for
inividual variable groups. $vargroups contains a list of all variable
groups defined. $refcat contains the list of titles, if title
option was set.

grpvars list, name() returns r(varlist) macro containing the collection
of variables in variable groups specified by name(), and r(k) scalar
containing the number of variables in this list.

grpvars dir returns r(vargroups) macro containing the list of names
of variable groups, and r(n_vargroups) scalar containing the number of
variable groups in memory.

Author

Gorkem Aksaray, Koc University.

Email: gaksaray@ku.edu.tr

Personal Website: sites.google.com/site/gorkemak

GitHub: github.com/gaksaray