Bookmark and Share

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]

st: RE: loop in stata


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: loop in stata
Date   Thu, 11 Aug 2011 20:18:21 +0100

Various ways to do it, none of them exactly beautiful: 

tokenize "a b c m" 
local j = 1 

foreach v in z y x n { 
	tab `v' ``j''
	local ++j
}

Or more symmetrically: 

local J : word count "a b c m"

forval j = 1/`J' { 
	local x : word `j' of "a b c m"
	local y : word `j' of "z y x n"
	tab `x' `y' 
}  

See also 

SJ-3-2  pr0009  . . . . . . . . . . . . . Speaking Stata:  Problems with lists
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/03   SJ 3(2):185--202                                 (no commands)
        discusses ways of working through lists held in macros

or (directly)

http://www.stata-journal.com/sjpdf.html?articlenum=pr0009

for a much longer discussion. 

Nick 
[email protected] 

fernando andrade

I am trying to make a simple loop in stata but i cannot figure out how to.
i have two sets of variables let's say:
set1: a b c ...m
set2: z y x...n

i want to make a loop to run the tabs
tab a z
tab b y
tab c x
.
.
.
tab m n

i tried
foreach X in a b c... m{
foreach Y in z y x... n{
tab `X' `Y',m
}
}

the loop gave me all the posible combinatios between the two sets of
variables. Is there a way to make a loop that tabs only a with z, b
with y and so on

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index