📄 generate.hlp
字号:
{smcl}
{* 16mar2005}{...}
{cmd:help generate}, {cmd:help replace},{right:dialogs: {bf:{dialog generate}} {bf:{dialog replace}}}
{cmd:help set seed}, {cmd:help set type}
{hline}
{title:Title}
{p2colset 5 21 23 2}{...}
{p2col :{hi:[D] generate} {hline 2}}Create or change contents of variable{p_end}
{p2colreset}{...}
{title:Syntax}
{phang}
Create new variable
{p 8 17 2}
{opt g:enerate}
{dtype}
{newvar}[{cmd::}{it:lblname}] {cmd:=}{it:exp}
{ifin}
{phang}
Change contents of existing variable
{p 8 17 2}
{cmd:replace}
{it:oldvar}
{cmd:=}{it:exp}
{ifin}
[{cmd:,} {opt nop:romote}]
{phang}
Specify initial value of random-number seed
{p 8 17 2}
{marker seed}
{cmd:set}
{opt se:ed}
{c -(}{it:#}{c |}{it:code}{c )-}
{phang}
Specify default storage type assigned to new variables
{p 8 17 2}
{cmd:set}
{opt ty:pe} {c -(}{opt float}{c |}{opt double}{c )-}
[{cmd:,} {opt perm:anently}]
{phang}
where {it:type} is one of{break}
{opt byte}{c |}{opt int}{c |}{opt long}{c |}{opt float}{c |}{opt double}{c |}
{opt str}{c |}{opt str1}{c |}{opt str2}{c |}...{c |}{cmd:str{ccl maxstrvarlen}}
{phang}
See Description below for an explanation of {opt str}. For the other
types, see {help data types}. Types {opt str81}, {opt str82}, ...,
{opt str244} may be specified only if you are using Stata/SE.
{phang}
{opt by} may be used with {opt generate} and {opt replace}; see {helpb by}.
{title:Description}
{pstd}
{opt generate} creates a new variable. The values of the variable are
specified by {cmd:=}{it:exp}.
{pstd}
If no {it:type} is specified, the new variable type is determined by the type
of result returned by {cmd:=}{it:exp}. A {opt float} variable (or a
{opt double}, according to {opt set type}) is created if the result is numeric,
and a string variable is created if the result is a string. In the latter
case, a {opt str}{it:#} variable is created where {it:#} is the smallest
string that will hold the result.
{pstd}
If a {it:type} is specified, the result returned by {cmd:=}{it:exp} must
be string or numeric according to whether {it:type} is string or numeric. If
{opt str} is specified, a {opt str}{it:#} variable is created where {it:#} is
the smallest string that will hold the result.
{pstd}
See {helpb egen} for extensions to {opt generate}.
{pstd}
{opt replace} changes the contents of an existing variable.
Because {opt replace} alters data, the command cannot be abbreviated.
{pstd}
{opt set seed} {it:#} specifies the initial value of the random-number seed
used by the {helpb random:uniform()} function.
{it:#} should be a positive integer. Alternatively, you may specify {it:code}
to indicate a previous internal state.
{pstd}
{opt set type} specifies the default storage type assigned to new variables
(such as those created by {opt generate}) when the storage type is not
explicitly specified.
{title:Options}
{phang}
{opt nopromote} prevents {opt replace} from promoting the
{help data types:variable type} to accommodate the change. For instance,
consider a variable stored as an integer type ({opt byte}, {opt int}, or
{opt long}), and assume that you {opt replace} some values with nonintegers.
By default, {opt replace} changes the variable type to a floating point
({opt float} or {opt double}) and thus correctly stores the changed
values. Similarly, {opt replace} promotes {opt byte} and {opt int}
variables to longer integers ({opt int} and {opt long}) if the replacement
value is an integer but is too large in absolute value for the current storage
type. {opt replace} promotes strings to longer strings. {opt nopromote}
prevents {opt replace} from doing this; instead, the replacement values are
truncated to fit the current storage type.
{phang}
{opt permanently} specifies that, in addition to making the change right now,
the new limit be remembered and become the default setting when you invoke
Stata.
{title:Examples}
{phang}{cmd:. generate age2 = age*age}{p_end}
{phang}{cmd:. generate biginc = income>100000 & income<.}{p_end}
{phang}{cmd:. replace income=. if income<=0}{p_end}
{phang}{cmd:. replace age = 25 in 1007}{p_end}
{phang}{cmd:. gen double unitpr = cost/quantity}{p_end}
{phang}{cmd:. gen byte biginc = income>100000 & income<.}{p_end}
{phang}{cmd:. gen xlag = x[_n-1]}{p_end}
{phang}{cmd:. set seed 339487731}{p_end}
{phang}{cmd:. gen r = uniform()}{p_end}
{phang}{cmd:. gen normals = invnorm(uniform())}{p_end}
{phang}{cmd:. set seed 1829382}{p_end}
{phang}{cmd:. gen random = uniform()}{p_end}
{phang}{cmd:. sort random}{p_end}
{phang}{cmd:. local holdseed = c(seed)}{p_end}
{phang}{cmd:. gen unif10 = floor(uniform()*10) + 1}{p_end}
{phang}{cmd:. ... {it:much later} ...}{p_end}
{phang}{cmd:. set seed `holdseed'}{p_end}
{phang}{cmd:. gen same10 = floor(uniform()*10) + 1}{p_end}
{phang}{cmd:. sort loc}{p_end}
{phang}{cmd:. by loc: gen avgx = sum(x)/sum(x<.)}{p_end}
{phang}{cmd:. by loc: replace avgx = avgx[_N]}{p_end}
{title:Also see}
{psee}
Manual: {bf:[D] generate}
{psee}
Online:
{helpb compress},
{helpb corr2data},
{helpb drawnorm},
{helpb edit},
{helpb egen},
{helpb encode},
{help exp},
{help functions},
{helpb label},
{help operators},
{helpb recode},
{helpb rename}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -