Example: stock market

Recode categorical variables - Stata

Recode categorical variablesDescriptionQuick startMenuSyntaxOptionsRemarks and examplesAcknowledgmentAlso seeDescriptionrecodechanges the values of numeric variables according to the rules specified. Values that donot meet any of the conditions of the rules are left unchanged, unless anotherwiserule is range#1/#2refers to all (real and integer) values between #1and#2, including the boundaries#1and#2. This interpretation of#1/#2differs from that in a convenient way to refer to the minimum and maximum for each variable invarlistand may be used in both the from-value and the to-value parts of the specification.

value labels on these new variables. It is possible to do this in three steps: 1. Create the new variables (recode :::, gen()). 2. Define the value label (label define :::). 3. Link the value label to the variables (label value :::). Inconsistencies may emerge from mistakes between steps 1 and 2. Especially when you make

Tags:

  Between, Variable

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Recode categorical variables - Stata

1 Recode categorical variablesDescriptionQuick startMenuSyntaxOptionsRemarks and examplesAcknowledgmentAlso seeDescriptionrecodechanges the values of numeric variables according to the rules specified. Values that donot meet any of the conditions of the rules are left unchanged, unless anotherwiserule is range#1/#2refers to all (real and integer) values between #1and#2, including the boundaries#1and#2. This interpretation of#1/#2differs from that in a convenient way to refer to the minimum and maximum for each variable invarlistand may be used in both the from-value and the to-value parts of the specification.

2 Combinedwithifandin, the minimum and maximum are determined over the restricted keyword rules specify transformations for values not changed by the previous rules:nonmissingall nonmissing values not changed by the rulesmissingall missing values (.,.a,.b,..,.z) not changed by the ruleselseall nonmissing and missing values not changed by the rules*synonym forelserecodeprovides a convenient way to define value labels for the generated variables during thedefinition of the transformation, reducing the risk of inconsistencies between the definition and valuelabeling of variables .

3 Value labels may be defined for integer values and for the extended missingvalues (.a,.b,..,.z), but not for noninteger values or for sysmiss (.).Although this is not shown in the syntax diagram, the parentheses around therules and keywordclauses are optional if you transform only one variable and if you do not define value startRecode 3 to 0, 4 to 1, and 5 to 2 inv1, and store result innewv1recode v1 (3=0) (4=-1) (5=-2), generate(newv1)As above, and Recode missing values to 9recode v1 (3=0) (4=-1) (5=-2) (missing=9), gen(newv1)Also recodev2using the same rule and store result innewv2recode v1 v2 (3=0) (4=-1) (5=-2) (missing=9), gen(newv1 newv2)

4 Same as above when adding a prefix to the old variable namerecode v1 v2 (3=0) (4=-1) (5=-2) (missing=9), prefix(new) Recode 3 through 5 to 0 and 1 through 2 to 1, and create value labelmylabelrecode v1 (3/5=0 "Value 0") (1/2=1 "Value 1"), gen(newv1) ///label(mylabel)12 Recode Recode categorical variablesAs above, but set all other values to 9 and label them Invalid Recode v1 (3/5=0 "Value 0") (1/2=1 "Value 1") ///(else=9 "Invalid"), gen(newv1) label(mylabel)MenuData>Create or change data>Other variable -transformation commands> Recode categorical variableSyntaxBasic syntaxrecodevarlist(rule)[(rule).]

5 ][, generate(newvar)]Full syntaxrecodevarlist(erule)[(erule)..][if ][in][,options]where the most common forms forruleareruleExampleMeaning#=#3 = 13 recoded to 1# #=#2 . = 92 and . recoded to 9#/#=#1/5 = 41 through 5 recoded to 4nonmissing =#nonmiss = 8all other nonmissing to 8missing =#miss = 9all other missings to 9whereerulehas the formelement[ ]=el["label"]nonmissing =el["label"]missing =el["label"]else|* =el["label"]elementhas the formel|el/elandelis#|min|maxThe keyword rulesmissing,nonmissing, andelsemust be the last rules notbe combined Recode categorical variables 3optionsDescriptionOptionsgenerate(newva r)generatenewvarcontaining transformed variables .

6 Default is to replaceexisting variablesprefix(str)generate new variables withstrprefixlabel(name)specify a name for the value label defined by the transformation rulescopyrestcopy out-of-sample values from original variablestesttest that rules are invoked and do not overlapOptions Options generate(newvar)specifies the names of the variables that will contain the transformed ()is a synonym forgenerate(). Values outside the range implied byiforinare set tomissing (.), unless thecopyrestoption is ()is not specified, the input variables are overwritten; values outside theiforinrange are not modified.

7 Overwriting variables is dangerous (you cannot undo changes, value labelsmay be wrong, etc.), so we strongly recommend specifyinggenerate().prefix(str)specifie s that the recoded variables be returned in new variables formed by prefixingthe names of the original variables (name)specifies a name for the value label defined from the transformation ()may be defined only withgenerate()(or its synonym,into()) andprefix(). If a variable isrecoded, the label name defaults tonewvarunless a label with that name already that out-of-sample values be copied from the original variables .

8 In line withother data management commands,recodedefaults to settingnewvarto missing (.) outside theobservations selected that Stata test whether rules are ever invoked or that rules overlap; for example,(1/5=1) (3=2).Remarks and are presented under the following headings:Simple examplesSetting up value labels with recodeReferring to the minimum and maximum in rulesRecoding missing valuesRecoding subsets of the dataOtherwise rulesTest for overlapping rulesVideo example4 Recode Recode categorical variablesSimple examplesMany users experienced with other statistical software use therecodecommand often, but easierand faster solutions in Stata are available.

9 On the other hand,recodeoften provides simple ways tomanipulate variables that are not easily accomplished otherwise. Therefore, we show other ways toperform a series of tasks with and want to change 1 to 2, leave all other values unchanged, and store the results in the new Recode x (1 = 2), gen(nx)or. generate nx = x. replace nx = 2 if nx==1or. generate nx = cond(x==1,2,x)We want to swap 1 and 2, saving them Recode x (1 = 2) (2 = 1), gen(nx)or. generate nx = cond(x==1,2,cond(x==2,1,x))We want to recodeitemby collapsing 1 and 2 into 1, 3 into 2, and 4 to 7 (boundaries included)into Recode item (1 2 = 1) (3 = 2) (4/7 = 3), gen(Ritem)or.

10 Generate Ritem = item. replace Ritem = 1 if inlist(item,1,2). replace Ritem = 2 if item==3. replace Ritem = 3 if inrange(item,4,7)We want to change the direction of the 1,..,5 valued variablesx1,x2,x3, storing the transformedvariables innx1,nx2, andnx3(that is, we form new variable names by prefixing old variablenames with an n ).. Recode x1 x2 x3 (1=5) (2=4) (3=3) (4=2) (5=1), pre(n) testor. generate nx1 = 6-x1. generate nx2 = 6-x2. generate nx3 = 6-x3. forvalues i = 1/3 {generate nx i = 6-x i }In the categorical variablereligion, we want to change 1, 3, and the real and integer numbers 3through 5 into 6; we want to set 2, 8, and 10 to 3 and leave all other values Recode religion 1 3/5 = 6 2 8 10 = 3or.


Related search queries