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


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: RE: Generate variables being as the difference between two varlists
Date   Sat, 18 Jan 2014 14:52:20 +0000

In fact, a loop is a smidgen slower here, and usually, as it adds
scaffolding. The main point of a loop here, and usually, is just
simplifying code.

Nick
[email protected]

On 18 January 2014 14:29, Joe Canner <[email protected]> wrote:
> 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.

Robert Maderitsch [[email protected]]

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

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