Generating new variables

1.1
Other Commands How to...

gen [s|i|d|f|b] var1 [= expression|resultvar|value]

The GEN command will create a new variable based on the expression, or equal to a constant from a result variable. It is a equivalent to a define and let commands, with the variable type implied by the expression. If the result of expression is boolean, var1 will be 0 (FALSE) or 1 (TRUE). Result variables are created by some commands, e.g. means and describe.
If the user specifies type, that type of variable is generated:
gen s var1 = expression (s=Text/String)
gen d var1 = expression (d=Date)
gen i var1 = expression (i=Integer)
gen f var1 = expression (f=Float)
gen b var1 = expression (b=Boolean)
If the user does not specify type, then a float variable will be created.
Variable names in the GEN statement must be 10 or fewer characters.Variable names may not begin with a number or contain space, hyphens or other punctuation, and cannot start with a number.
Numeric variables are float if no decimal is specified; otherwise they are real or "floating point" numbers.
Generated variables begin with values set to missing if no expression is indicated, otherwise the variable will be initialized to the value indicated in the expression.

Output:


 gen t10 = 1.0
 Var Name T10 of type Float and length: 14 decimals 6                                    
 

As an output, EpiData writes a line informing the user that the new variable has been generated and the type and length of it.


Examples and Hints
GEN i AGELIMIT= (AGE>18) // (Will generate an integer variable AGELIMIT, the value will be 1 if AGE is greater than 18 and else 0, unless age is missing then agelimit will be missing value)
GEN b AGELIMIT= (AGE>18) //(Will generate a boolean variable AGELIMIT, the value will be 1 (true if AGE is greater than 18)
GEN COUNT               //(Will generate a floating variable named COUNT. Initial value =.)
GEN d REFDATE=dmy(15,05,1999) //(Will create a date variable, with 15/05/1999 as initial value) 

Notice:
dmy is the date funtion

Related Commands:
Define Recode generate

Options:
No Options for this command
Output variables:
No output variables for this command
Output definition:
References:
No specific references
Top