Example: marketing

Title stata.com generate — Create or change …

Create or change contents of variableSyntaxMenuDescriptionOptionsRema rks and examplesMethods and formulasReferencesAlso seeSyntaxCreate new variablegenerate[type]newvar[:lblname]=e xp[if][in]Replace contents of existing variablereplaceoldvar=exp[if][in][, nopromote]Specify default storage type assigned to new variablesset type{float|double}[, permanently]wheretypeis one ofbyte|int|long|float|double|str|str1|st r2|..| for an explanation ofstr. For the other types, see[U] 12 allowed withgenerateandreplace; see [D] > Create or change data> Create new variablereplaceData> Create or change data> change contents of variableDescriptiongeneratecreates a new variable . The values of the variable are specified by= notypeis specified, the new variable type is determined by the type of result returned by= (or adouble, according toset type) is created if the result is numeric, and astring variable is created if the result is a string.

Title stata.com generate — Create or change contents of variable SyntaxMenuDescriptionOptions Remarks and examplesMethods and formulasReferencesAlso see Syntax Create new variable generate type newvar

Tags:

  Change, Content, Variable, Create, Stata, Generate, Generate create or change, Generate create or change contents of variable

Information

Domain:

Source:

Link to this page:

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

Other abuse

Transcription of Title stata.com generate — Create or change …

1 Create or change contents of variableSyntaxMenuDescriptionOptionsRema rks and examplesMethods and formulasReferencesAlso seeSyntaxCreate new variablegenerate[type]newvar[:lblname]=e xp[if][in]Replace contents of existing variablereplaceoldvar=exp[if][in][, nopromote]Specify default storage type assigned to new variablesset type{float|double}[, permanently]wheretypeis one ofbyte|int|long|float|double|str|str1|st r2|..| for an explanation ofstr. For the other types, see[U] 12 allowed withgenerateandreplace; see [D] > Create or change data> Create new variablereplaceData> Create or change data> change contents of variableDescriptiongeneratecreates a new variable . The values of the variable are specified by= notypeis specified, the new variable type is determined by the type of result returned by= (or adouble, according toset type) is created if the result is numeric, and astring variable is created if the result is a string.

2 In the latter case, if the string variable contains valuesgreater than 2,045 characters or contains values with a binary 0 (\0), astrLvariable is , astr# variable is created, where#is the smallest string that will hold the atypeis specified, the result returned by=expmust be a string or numeric according to whethertypeis string or numeric. Ifstris specified, astrLor astr# variable is created using the samerules as generate Create or change contents of variableSee [D]egenfor extensions the contents of an existing variable . Becausereplacealters data, the commandcannot be typespecifies the default storage type assigned to new variables (such as those created bygenerate) when the storage type is not explicitly promoting the variable type to accommodate the change . Forinstance, consider a variable stored as an integer type (byte,int, orlong), and assume thatyoureplacesome values with nonintegers.

3 By default,replacechanges the variable type to afloating point (floatordouble) and thus correctly stores the changed values. Similarly,replacepromotesbyteandintvaria bles to longer integers (intandlong) if the replacement value is aninteger but is too large in absolute value for the current storage stringsto longer doing this; instead, the replacement valuesare truncated to fit into the current storage that, in addition to making the change right now, the new limit be rememberedand become the default setting when you invoke and are presented under the following headings: generate and replaceset typegenerate and replacegenerateandreplaceare used to Create new variables and to modify the contents of existingvariables, respectively. Although the commands do the same thing, they have different names so thatyou do not accidentally replace values in your data.

4 Detailed descriptions of expressions are given in[U] 13 Functions and see [D] 1We have a dataset containing the variableage2, which we have previously defined asage^2(thatis,age2). We have changed some of theagedata and now want to correctage2to reflect the newvalues:. use (Wages of women). generate age2=age^2age2 already definedr(110);When we attempt to re- generate age2, stata refuses, telling us thatage2is already defined. Wecoulddrop age2and then re-generateit, or we could use thereplacecommand:. replace age2=age^2(204 real changes made) generate Create or change contents of variable 3 When we usereplace, we are informed of the number of actual changes made to the can explicitly specify the storage type of the new variable being created by putting thetype,such asbyte,int,long,float,double, orstr8, in front of the variable name. For example,you could typegenerate double revenue = qty * price.

5 Not specifying a type is equivalentto specifyingfloatif the variable is numeric, or, more correctly, it is equivalent to specifying thedefault type set by theset typecommand; see below. If the variable is alphanumeric, not specifyinga type is equivalent to specifyingstr#, where#is the length of the largest string in the may also specify a value label to be associated with the new variable by including :lblname after the variable name. This is seldom done because you can always associate the value label laterby using thelabel valuescommand; see[U] Value 2 Among the variables in our dataset isname, which contains the first and last name of each wish to Create a new variable calledlastname, which we will then use to sort the string use , clear. list Bimslager. generate lastname=word(name,2). describeContains data from : 5vars: 2 18 Jan 2013 12:24size: 130storage display valuevariable name type format label variable labelname str17 %17slastname str9 %9sSorted by:Note: dataset has changed since last savedStata is smart.

6 Even though we did not specify the storage type in ourgeneratestatement, Stataknew to Create astr9 lastnamevariable, because the longest last name isBimslager, which hasnine generate Create or change contents of variableExample 3We wish to Create a new variable ,age2, that represents the variableagesquared. We realize thatbecauseageis an integer,age2will also be an integer and will certainly be less than 32,740. Wetherefore decide to storeage2as anintto conserve memory:. use , clear. generate int age2=age^2(9 missing values generated)Precedingage2withinttold stata that the variable was to be stored as anint. After creatingthe new variable , stata informed us that nine missing values were uswhenever it produces missing [U] 13 Functions and expressionsand[U] 25 Working with categorical data and factorvariablesfor more information and examples.

7 Also see [D]recodefor a convenient way to recodecategorical noteIf you specify theifmodifier orinrange, the=expis evaluated only for those observations thatmeet the specified condition or are in the specified range (or both, if bothifandinare specified).The other observations of the new variable are set to missing:. use , clear. generate int age2=age^2 if age>30(290 missing values generated)Example 4replacecan be used to change just one value, as well as to make sweeping changes to our instance, say that we enter data on the first five odd and even positive integers and then discoverthat we made a mistake:. use , clear. listodd 10 The third observation is wrong; the value ofoddshould be 5, not 8. We can usereplacetocorrect the mistake:. replace odd=5 in 3(1 real change made)We could also have corrected the mistake by typingreplace odd=5 if odd== Create or change contents of variable 5set typeWhen you Create a new numeric variable and do not specify the storage type for it, say, by typinggenerate y=x+2, the new variable is made afloatif you have not previously issued theset typecommand.

8 If earlier in your session you typedset type double, the new numeric variable wouldbe made and formulasYou can do anything withreplacethat you can do withgenerate. The only difference betweenthe commands is thatreplacerequires that the variable already exist, whereasgeneraterequiresthat the variable be new. In fact, inside stata ,generateandreplacehave the same code. BecauseStata is an interactive system, we force a distinction between replacing existing values and generatingnew ones so that you do not accidentally replace valuable data while thinking that you are creatinga new piece of , J. R. 1997a. dm50: Defining variables and recording their Technical Bulletin40: 9 inStata Technical Bulletin Reprints, vol. 7, pp. 48 49. College Station, TX: stata 1997b. : Update to Technical Bulletin51: 2.

9 Reprinted inStata Technical Bulletin Reprints,vol. 9, pp. 14 15. College Station, TX: stata , R. B. 2004. stata tip 13: generate and replace use the current sort Journal4: 484 , P. 2013. cmpute: A tool to generate or replace a Journal13: 862 , J. 1997. dm43: Automatic recording of Technical Bulletin35: 6 7. Reprinted inStataTechnical Bulletin Reprints, vol. 6, pp. 18 20. College Station, TX: stata see[D]compress Compress data in memory[D]corr2data Create dataset with specified correlation structure[D]drawnorm Draw sample from multivariate normal distribution[D]edit Browse or edit data with Data Editor[D]egen Extensions to generate [D]encode Encode string into numeric and vice versa[D]label Manipulate labels[D]recode Recode categorical variables[D]rename Rename variable [U] 12 Data[U] 13 Functions and expressions


Related search queries