Example: barber

Creating new variables - Stata

11 Creating new variablesgenerate and replaceThis chapter shows the basics of Creating and modifying variables in Stata . We saw how to workwith the Data Editor in[GSW] 6 Using the Data Editor this chapter shows how we would do thisfrom the Command window. The two primary commands used for this are generatefor Creating new variables . It has a minimum abbreviation ofg. replacefor replacing the values of an existing variable. It may not be abbreviated because italters existing data and hence can be considered most basic form for Creating new variables isgeneratenewvar=exp, whereexpis any kindofexpression.

3. Chev. Monza D Monza Monza D 4. D D 5. Datsun 510 F 510 510 F 6. Buick Regal D Regal Regal D 7. Datsun 810 F 810 810 F There are a few things to note about how these commands work: 1. strpos(s 1,s 2) produces an integer equal to the first position in the string s 1 at which the string s 2 is found or 0 if it is not found. In this example ...

Tags:

  Monza, Monza monza

Information

Domain:

Source:

Link to this page:

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

Other abuse

Advertisement

Transcription of Creating new variables - Stata

1 11 Creating new variablesgenerate and replaceThis chapter shows the basics of Creating and modifying variables in Stata . We saw how to workwith the Data Editor in[GSW] 6 Using the Data Editor this chapter shows how we would do thisfrom the Command window. The two primary commands used for this are generatefor Creating new variables . It has a minimum abbreviation ofg. replacefor replacing the values of an existing variable. It may not be abbreviated because italters existing data and hence can be considered most basic form for Creating new variables isgeneratenewvar=exp, whereexpis any kindofexpression.

2 Of course, bothgenerateandreplacecan be used withifandinqualifiers. Anexpression is a formula made up of constants, existing variables , operators, and functions. Someexamples of expressions (using variables from theautodataset) would be2 + price,weight^2orsqrt(gearratio).The operators defined in Stata are given in the table below:RelationalArithmeticLogical(numeri c and string)+addition!not>greater than-subtraction|or<less than*multiplication&and>=>or equal/division<=<or equal^power==equal!=not equal+string concatenationStata has many mathematical, statistical, string, date, time-series, and programming functions.

3 Seehelp functionsfor the basics, and see [D]functionsfor a complete list and full details of all thebuilt-in can use menus and dialogs to create new variables and modify existing variables by selectingmenu items from theData > Create or change datamenu. This feature can be handy for findingfunctions quickly. However, we will use the Command window for the examples in this chapterbecause we would like to illustrate simple usage and some has some utility commands for Creating new variables : Theegencommand is useful for working across groups of variables or within groups ofobservations.

4 See [D]egenfor more information. Theencodecommand turns categorical string variables into encoded numeric variables , whileits counterpartdecodereverses this operation. See [D]encodefor more information. Thedestringcommand turns string variables that should be numeric, such as numbers withcurrency symbols, into numbers. To go from numbers to strings, thetostringcommand isuseful. See [D]destringfor more will focus our efforts [ GSW ] 11 Creating new variablesgenerateThere are some details you should know about thegeneratecommand: The basic form of thegeneratecommand isgeneratenewvar=exp, wherenewvaris anew variable name andexpis any valid expression.

5 You will get an error message if you trytogeneratea variable that already exists. An algebraic calculation using a missing value yields a missing value, as does division by zero,the square root of a negative number, or any other computation which is impossible. If missing values are generated, the number of missing values innewvaris always reported. IfStata says nothing about missing values, then no missing values were generated. You can usegenerateto set the storage type of the new variable as it is generated. You mightwant to create an indicator (0/1) variable as abyte, for example, because it saves 3 bytes perobservation over using the default storage type are some examples of Creating new variables from theafewcarslabdataset, which wecreated inLabeling values of variablesin [GSW]9 Labeling data.

6 (To work along, start by openingtheautodataset withsysuse auto. We are using a smaller dataset to make shorter listings.) Thelast example shows a way to generate an indicator variable for cars weighing more than 3,000 expressions in Stata result in1for true and0for false . Theifqualifier is used to ensurethat the computations are done only for observations whereweightis not missing.[ GSW ] 11 Creating new variables 3 . use afewcarslab(A few 1978 cars). list make mpg weightmake mpg Rabbit 25 98 21 monza

7 510 24 Regal 20 810

8 2750. * changing MPG to liters per 100km. generate lphk = * (100 / ) / mpg(2 missing values generated). label var lphk "Liters per 100km".

9 * getting logarithms of price. g lnprice = ln(price). * making an indicator of hugeness. gen byte huge = weight >= 3000 if !missing(weight). l make mpg weight lphk lnprice hugemake mpg weight lphk lnprice Rabbit

10 25 1930 98 21 4060 monza .


Related search queries