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: Generate variables being as the difference between two varlists
From
Robert Maderitsch <[email protected]>
To
[email protected]
Subject
st: Generate variables being as the difference between two varlists
Date
Sat, 18 Jan 2014 13:17:30 +0100
Dear Statalisters,
I have the following problem:
The data looks like this:
a1 a2 a3 b1 b2 b3
1 3 3 2 2 3
2 4 2 3 2 5
3 5 1 5 3 2
Now I want to generate new variables as
c1=b1-a1
c2=b2-a2
c3=b3-a3
However, as my dataset is really big, I want to automate this.
I tried for example a double loop:
foreach x in a1-a3 {
foreach y in b1-b3 {
gen c1_`x'_`y'=`y'-`x'
}
}
The problem with this is, however, that this generates 9 new variables
(for all possible combinations) and this is not what I want.
There must be easier solutions.
Does anyone have a suggestion?
Thank you very much!
Robert
*
* 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/