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: Generate variables being as the difference between two varlists
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: Generate variables being as the difference between two varlists
Date
Sat, 18 Jan 2014 12:43:14 +0000
Your difficulty arises because your problem calls for a single loop. Consider
forval j = 1/3 {
gen c`j' = a`j' - b`j'
}
For more detailed tutorialw on -for*- see (.pdf freely available at
the cited URLs)
SJ-3-2 pr0009 . . . . . . . . . . . . . Speaking Stata: Problems with lists
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
Q2/03 SJ 3(2):185--202 (no commands)
discusses ways of working through lists held in macros
http://www.stata-journal.com/sjpdf.html?articlenum=pr0009
SJ-2-2 pr0005 . . . . . . Speaking Stata: How to face lists with fortitude
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
Q2/02 SJ 2(2):202--222 (no commands)
demonstrates the usefulness of for, foreach, forvalues, and
local macros for interactive (non programming) task
http://www.stata-journal.com/sjpdf.html?articlenum=pr0005
Nick
[email protected]
On 18 January 2014 12:17, Robert Maderitsch <[email protected]>
> 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?
*
* 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/