Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: documentation of old -for- syntax
From
Richard Williams <[email protected]>
To
[email protected]
Subject
Re: st: documentation of old -for- syntax
Date
Fri, 18 Apr 2014 12:43:08 -0500
Laslo, here is what the help for -for- from Stata 7 says.
-------------------------------------------------
Repeat Stata command
Basic syntax
for listtype list : stata_cmd_containing_X
If listtype is then list is a
-------------- --------------------------
varlist varlist
newlist new varlist
numlist numlist (see help numlist)
anylist list of words
Basic syntax examples:
. for var m*: replace X = X/10
. for new u1-u10: gen X = uniform()
. for num 1/5: count if freq==X
. for any . -1: replace x = . if y==X
In each example, elements from the list are substituted one
at a time for the capital X and the command executed.
Full syntax
for [id in] listtype list [\ [id in] listtype list [\ ...]]
[, dryrun noheader pause nostop ] : stata_cmd [\ stata_cmd [\ ...]]
Up to 9 parallel lists may be specified. Any number of
commands may be specified. If ids are not specified, then
Elements of 1st list substitute for X in stata_cmd
... 2nd ... Y ...
... 3rd ... Z ...
... 4th ... A ...
... 5th ... B ...
... 6th ... C ...
... 7th ... D ...
... 8th ... E ...
... 9th ... F ...
Description
for repeats stata_cmd. At each repetition, the members of list are
substituted for the ids in stata_cmd. More generally, at each
repetition, the members of the first list are substituted for all
occurrences of
the first id in the stata_cmds, members of the second list are
substituted for all occurrences of the second id in the stata_cmds,
and so forth.
Options
dryrun specifies that stata_cmd is not to be executed; for is merely
to display the commands that would be run had dryrun not been specified.
noheader suppresses the display of the command before each repetition.
pause pauses output after each execution of stata_cmd. This may be
useful, for example, when for is combined with graph.
nostop does not stop the repetitions if one of them results in an error.
Remarks
X is the default id (the character marking where substitutions are to
be made when you do not specify otherwise).
. for num 1/20: append using "my dir\fileX"
specifies that file1, file2, ..., file20 are to be appended. So does
. for FILENO in num 1/20: append using "my dir\fileFILENO"
or
. for fno in num 1/20: append using "my dir\filefno"
Be careful when explicitly specifying ids because all occurrences of
id are substituted.
. for f in num 1/20: append using "my dir\filef"
would yield the surprising result of replacing all occurrences of
letter f with the numbers 1, then 2, ..., then 20 in append using "my
dir\filef"; the first command would be append using "my dir\1ile1".
Multiple lists are processed concurrently, in parallel. X, Y, Z, A,
B, C, D, E, F are the default ids when you do not specify otherwise.
. for new x2-x5 \ num 2/5: gen X=myvar^Y
creates gen x2=myvar^2, gen x3=myvar^3, etc. You could instead type
. for VAR in new x2-x5 \ POWER in num 2/5: gen VAR = myvar^POWER
and produce the same result.
Examples
. for var ch1 ch2 ch3: ttest X=0
. for var ch1-ch3: ttest X=0
. for var ch*: ttest X=0
. for var q6-q25: replace X = X+1
. for new u1-u3: gen X = uniform()
. for var pre1 pre2 \ var post1 post2: ttest X=Y
. for var pre* \ var post*: ttest X=Y
. for var pre* post* \ num 2.53 4.2 0.93 2.53: ttest X=Y
. for num 1/20: rename vX answerX
. for any male female: reg y x1 x2 if sex=="X" \ predict yX
for loops may be nested. For instance
. for A in num 1/5 : for B in num 1/3 : gen aAbB = aA * bB
would generate variables a1b1, a1b2, a1b3, a2b1, ..., a5b3 from
products of each of the a1, a2, ..., a5 variables with each of the
b1, b2, and b3 variables.
Warning
If you refer to filenames containing backslash (\), you must enclose
the filename in double quotes ("") even if the filename does not
contain blanks because otherwise for will think the \ is a separator.
. for any male female: save c:\data\X if sex=="X"
will not work. for will think there are three commands:
save c:
data
X if sex=="X"
Instead, you must type
. for any male female: save "c:\data\X" if sex=="X"
At 11:30 AM 4/18/2014, you wrote:
Hi,
I am not aware of documentation on some syntax I found in another
researcher's public code base. It still runs on Stata 13.1 MP (for
mac, e.g.), but I would feel better if I could double check what is
going on. Could anyone guide me to some readings on code like the
lines below? (-help for- says that "for is an out-of-date command as
of Stata 8. See foreach and forvalues for its replacement.")
gen byte child18=0
for num 1/18: bysort serial year: egen temp1=count(pernum) if
(momloc==X | poploc==X) & age<=18 \ bysort serial year: egen
temp2=max(temp1) \ replace child18=temp2 if pernum==X \ drop temp*
or:
gen stcode=""
for @ in any AK AL AR AZ CA CO CT DC DE FL GA HI IA ID IL IN KS KY LA
MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK OR PA RI SC SD
TN TX UT VA VT WA WI WV WY \ # in num 02 01 05 04 06 08 09 11 10 12 13
15 19 16 17 18 20 21 22 25 24 23 26 27 29 28 30 37 38 31 33 34 35 32
36 39 40 41 42 44 45 46 47 48 49 51 50 53 55 54 56: replace stcode="@"
if statefip==#
or:
gen generosity=.
for Y in num 1984/2002 \ A in num 0.578 0.598 0.609 0.631 0.658 0.689
0.727 0.757 0.780 0.803 0.824 0.847 0.872 0.892 0.906 0.926 0.957
0.984 1.000 \ D in num 500 550 550 851 874 910 953 1192 1324 1434 2038
2094 2152 2210 2271 2312 2353 2428 2506: replace
generosity=ln(.4*((D*(1+eitc_s_child1))/A)+.4*((D*(1+eitc_s_child2))/A)+.2*((D*(1+eitc_s_child3))/A))
if year==Y
Thanks,
Laszlo
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/
-------------------------------------------
Richard Williams, Notre Dame Dept of Sociology
OFFICE: (574)631-6668, (574)631-6463
HOME: (574)289-5227
EMAIL: [email protected]
WWW: http://www.nd.edu/~rwilliam
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/