Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Foreach loop for subsequent variables

From   Nick Cox <>
To   "''" <>
Subject   st: RE: Foreach loop for subsequent variables
Date   Tue, 1 Nov 2011 14:56:01 +0000

Let's suppose your variables run var1 ... var42 so that the last pair is var41-var42. 

Then first I would use -forvalues- here. It is easier and a smidgen faster. 

forval i = 1(2)41 { 

	local j = `i' + 1 
	<whatever> var`i' var`j' 


You can also go 

forval i = 1(2)41 { 

	<whatever> var`i' var`=`i'+1' 


See [U] 18.3.8 and -help macro-. 


Dmitriy Glumov

I have recently begun to use Stata and have run into the problem
writing a foreach loop. I have a large dataset of variables, paired in
a way that var1 and var2 are together, var3 and var4 are together, and
so on. I am writing a foreach loop for each individual odd variable
and, everytime the program loops, I need to include a subsequent even
variable as well. So, for example, given that var1 is `x', I need to
include var2 into the program, too (in essence, `x' + 1). The problem
doesn't seem to be very difficult, and I am sure there is a simple
solution to it, but I just can't find it anywhere. Thank you for your
consideration, any help would be greatly appreciated!

*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index