📄 forvalues.hlp
字号:
{smcl}
{* 08feb2005}{...}
{cmd:help forvalues}
{hline}
{title:Title}
{p2colset 5 22 24 2}{...}
{p2col :{hi:[P] forvalues} {hline 2}}Loop over consecutive values{p_end}
{p2colreset}{...}
{title:Syntax}
{cmdab:forv:alues} {it:lname} {cmd:=} {it:range} {cmd:{c -(}}
{it:commands referring to} {cmd:`}{it:lname}{cmd:'}
{cmd:{c )-}}
where {it:range} is
{center:{it:#1}{cmd:(}{it:#d}{cmd:)}{it:#2} {space 4} meaning {it:#1} to {it:#2} in steps of {it:#d} }
{center:{it:#1}{cmd:/}{it:#2} {space 7} meaning {it:#1} to {it:#2} in steps of 1 }
{center:{it:#1} {it:#t} {cmd:to} {it:#2} {space 1} meaning {it:#1} to {it:#2} in steps of {it:#t} - {it:#1}}
{center:{it:#1} {it:#t} {cmd::}{space 2}{it:#2} {space 1} meaning {it:#1} to {it:#2} in steps of {it:#t} - {it:#1}}
{pstd}
The loop is executed as long as calculated values of {cmd:`}{it:lname}{cmd:'}
are {ul:<} {it:#2}, assuming {it:#d} > 0.
{pstd}
Braces must be specified with {cmd:forvalues}, and
{phang2}
1. the open brace must appear on the same line as {cmd:forvalues};
{phang2}
2. nothing may follow the open brace except, of course, comments;
the first command to be executed must appear on a new line;
{phang2}
3. the close brace must appear on a line by itself.
{pstd}
In versions prior to Stata 8, putting the open brace, command, and close
brace on the same line as {cmd:forvalues} was allowed. Old behavior is
restored under version control; see {helpb version}.
{title:Description}
{pstd}
{cmd:forvalues} repeatedly sets local macro {it:lname} to each element of
{it:range} and executes the commands enclosed in braces. The loop is executed
zero or more times.
{pstd}
{cmd:forvalues} is the fastest way to execute a block of code for different
numeric values of {it:lname}.
{title:Examples}
{pstd}
Generate 100 uniform random variables named x1, x2, ..., x100.
{cmd:. forvalues i = 1(1)100 {c -(}}
{cmd:2. generate x`i' = uniform()}
{cmd:3. {c )-}}
{pstd}
For variables var5, var6, ..., var13 output the # of obs. greater than 10.
{cmd:. forval num = 5/13 {c -(}}
{cmd:2. count if var`num' > 10}
{cmd:3. {c )-}}
{pstd}
Produce individual summarize commands for variables x5, x10, ..., x300.
{cmd:. forvalues k = 5 10 to 300 {c -(}}
{cmd:2. summarize x`k'}
{cmd:3. {c )-}}
{pstd}
A loop over noninteger values that includes more than one command.
{cmd:. forval x = 31.3 31.6 : 38 {c -(}}
{cmd:2. count if var1 < `x' & var2 < `x'}
{cmd:3. summarize myvar if var1 < `x'}
{cmd:4. {c )-}}
{title:Also see}
{psee}
Manual: {bf:[P] forvalues}
{psee}
Online: {helpb continue}, {helpb foreach}, {helpb while}; {help ifcmd}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -