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]

RE: st: foreach command with multiple varlists


From   "Sarah Edgington" <[email protected]>
To   <[email protected]>
Subject   RE: st: foreach command with multiple varlists
Date   Thu, 28 Mar 2013 13:42:19 -0700

Fatma,
Steve's solution will work if you really have variables with names of the
form x1 x2 x3 x4.  If your variable names don't have numbers in them then
you probably want a single loop using extended macro functions to reference
multiple lists containing the actual variable names.

For example:

	gen w=.

	forvalues i=1/4 {
		local x : word `i' of x_one x_two x_three x_four
		local y : word `i' of y_one y_two y_three y_four
		local z : word `i' of z_one z_two z_three z_four

		replace w=`x' if `y'==1 & `z'==1
	}

Note that, as with Steve's example, you want to -replace w- rather than -gen
w- in the loop because otherwise you'll get an error message that w already
exists in your second iteration.

-Sarah

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Steve Nakoneshny
Sent: Thursday, March 28, 2013 1:33 PM
To: [email protected]
Subject: Re: st: foreach command with multiple varlists

Fatma,

One initial problem I see is that you can only -gen- a var once. Every
subsequent manipulation of that var will have to be done using -replace-. I
suspect that you might be able to get away with something like as follows
(untested):

g w=.
forval i 1/4 {
	replace w=x`i' if y`i'==2 & z`i'==1
}

Steve

On 2013-03-28, at 2:26 PM, Fatma Romeh wrote:

> Dear statalist,
> 
> I have a question regarding the "foreach" command.  I'm wondering whether
I can use multiple valists with "foreach"? More specifically, I'm looking
for something like that:
> 
> foreach x of varlist  x1-x4  &  y of varlist y1-y4   &   z of varlist
z1-z4 {
> 
>     gen  w= `x' if `y'==2 & `z'==1
> }
> 
> when I did that, I got this error message: "& invalid name". What I want
to do is to use the "foreach" command to replace these commands:
> gen  w= x1 if y1==2 & z1==1
> gen  w= x2 if y2==2 & z2==1
> gen  w= x3 if y3==2 & z3==1
> gen  w= x4 if y4==2 & z4==1
> 
> Is there a way to do that?
> 
> 
> 
> Many thanks,
> 
> 
> Fatma, 
> 
> *
> *   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/


*
*   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/

*
*   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/


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