Title
rev -- Reverse values of variables
Syntax
rev varlist [if] [in] [, options]
options Description ------------------------------------------------------------------------- names prefix(str) use str as prefix for new variables generate(newvarlist) create new variables name1, name2, ... suffix(str) use str as suffix for new variables replace replace variables with reversed version
labels label(namelist) specify names for value labels nolabel do not define a value label for new variables modify modify exisiting value labels mask(str) handle numeric prefix in value labels
functionality reverse(numlist) reverse values specified in numlist swap(numlist) swap values specified in numlist copyrest copy values excluded by the if and/or in qualifier -------------------------------------------------------------------------
Description
rev reverses the value order of each variable specified in varlist. The created variable rv_varname contains the (distinct) values of the original variable in reversed order. A new value label is defined accordingly.
Remarks: Variables are assumed to have integer values incremented by one. If variables have integer values not incremented by one, specify the values to be reversed in option reverse. rev also handles variables that contain non-integer values.
Options
prefix(str) uses str as prefix for the new variables' names. May be combined with generate and/or suffix. The default prefix is rv_ and is set if none of prefix, generate, suffix or replace is specified.
generate(namelist) creates new variables name1, name2, .... May be combined with prefix and/or suffix.
suffix(str) uses str as suffix for the new variables' names. May be combined with generate and/or prefix.
replace replaces variables in varlist with their reversed version. If variables have value labels attached these labels are modified accordingly. Thus, replace implies modify. Alternatively a new label, specified in label, may be defined. May not be combined with prefix, generate or suffix.
label(namelist) uses names as value label names for the created variables. Either one name or as many names as new variables may be specified. If only one name is specified the value label defined for the first variable in varlist is attached to all successive variables. Default is to use new variables' names as value label names.
nolabel specifies that no value labels will be defined for the new variables. If specified with replace any value label attached to variables is detached but not dropped from memory. Value labels are left unmodified.
modify allows exisitng value labels to be overwritten.
mask(str) indicates that value labels have a numeric prefix assigned using numlabel with mask option. If numlabel ,mask(mask) has been used previously, value labels defined by rev will not be appropriate unless the same mask is specified. Note that rev handles a numlabel command without mask option automatically.
reverse(numlist) reverses values specified in numlist. In reverse(1 2 3 4) values 1 and 4, and values 2 and 3 are swapped. valid is a synonym for reverse. Up to 249 values may be specified. See example.
swap(numlist) swaps values specified in numlist pairwise. Thus, in swap(1 2 3 4) values 1 and 2, and values 3 and 4 are swapped. Up to 249 values may be specified.
copyrest copies values excluded by the if and/or in qualifier from the original variable.
Examples
In a fictional dataset we want to reverse the value order of three variables. We can do so using recode.
. recode var1 (5 = 1)(4 = 2)(2 = 4)(1 = 5) ,prefix(rec_) . recode var2 (7 = 1)(6 = 2)(5 = 3)(3 = 5)(2 = 6)(1 = 7) ,prefix(rec_) . recode var3 (4 = 1)(3 = 2)(2 = 3)(1 = 4) ,prefix(rec_)
Using rev we get the same result (and the appropriate value labels) typing only one line of code.
. rev var1 var2 var3 ,prefix(rec_)
The reverse option
. tabulate var1
var1 | Freq. Percent Cum. ------------------+----------------------------------- agree | 2 14.29 14.29 undecided | 3 21.43 35.71 disagree | 4 28.57 64.29 strongly disagree | 5 35.71 100.00 ------------------+----------------------------------- Total | 14 100.00
. label list var1 var1: 1 strongly agree 2 agree 3 undecided 4 disagree 5 strongly disagree 9 missing
Note that the minimum valid value in the data (which is 2) is not the theoretical minimum value in the underlying Likert-item (which is 1). Without specifying reverse we get:
. rev var1
. tabulate rv_var1
rv_var1 | Freq. Percent Cum. ------------------+----------------------------------- strongly disagree | 5 35.71 35.71 disagree | 4 28.57 64.29 undecided | 3 21.43 85.71 agree | 2 14.29 100.00 ------------------+----------------------------------- Total | 14 100.00
. label list rv_var1 rv_var1: 1 strongly agree 2 strongly disagree 3 disagree 4 undecided 5 agree 9 missing
Note that 'strongly disagree' is coded '2', whereas we want it to be coded '1'. Specifying the reverse option yields the appropriate result.
. rev var1 ,reverse(1/5)
. tabulate rv_var1
rv_var1 | Freq. Percent Cum. ------------------+----------------------------------- strongly disagree | 5 35.71 35.71 disagree | 4 28.57 64.29 undecided | 3 21.43 85.71 agree | 2 14.29 100.00 ------------------+----------------------------------- Total | 14 100.00
. label list rv_var1 rv_var1: 1 strongly disagree 2 disagree 3 undecided 4 agree 5 strongly agree 9 missing
Acknowledgments
There are several programs that serve the same purpose as rev. The omscore command written by Marc Jacobs in 1992, revrs by Kyle C. Longest and vreverse command by Nick Cox, to name a view. Implemented in official Stata there is the gsort command which can (in some cases) be used to reverse the value order of variables. Looking at these programs was very helpful writing rev.
The name rev is borrowed from the same-named egen function by Nick Cox who also recommended to change the name from revv.
Marc Kaulisch suggested option suffix.
Author
Daniel Klein, University of Bamberg, klein.daniel.81@gmail.com
Also see
Online: gsort, recode, label
if installed: omscore, revrs, vreverse