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: Generate variables being as the difference between two varlists


From   Joe Canner <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: RE: Generate variables being as the difference between two varlists
Date   Sat, 18 Jan 2014 14:29:15 +0000

Robert,

In addition to Nick's helpful advice, it's worth pointing out that the use of loops has nothing to do with the size of your data. Loops are useful for avoiding repetitive coding tasks. In your case, you only had three -gen- statements to begin with, so there's no particular advantage to using loops.

Regards,
Joe Canner
Johns Hopkins University School of Medicine
________________________________________
From: [email protected] [[email protected]] on behalf of Robert Maderitsch [[email protected]]
Sent: Saturday, January 18, 2014 7:17 AM
To: [email protected]
Subject: st: Generate variables being as the difference between two varlists

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/

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