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
Steve Nakoneshny <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: foreach command with multiple varlists
Date
Thu, 28 Mar 2013 14:33:09 -0600
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/