Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Generating new variables with values of difference in 2 variables


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Generating new variables with values of difference in 2 variables
Date   Wed, 18 Apr 2012 17:06:32 +0100

forval j = 1/7 {
     local J = `j' + 1
     gen var`j' = day`J' - day`j'
}

or

forval j = 1/7 {
    gen var`j' = day`=`j' + 1' - day`j'
}

All that said, this sounds a very bad idea strategically. Presumably
-day1- is what happens on day 1, and so forth, so this is time series
or panel data. You start out with 8 day variables, then add 7
difference variables. When this is going to stop as you add further
analyses?

Better to -reshape long-. Then you can work with one response
variable, one set of differences, etc.

Nick

On Wed, Apr 18, 2012 at 4:59 PM, Hitesh Chandwani
<hchandwani.stata@gmail.com> wrote:

> This is another query about loops. I have 8 variables day1-day8. I am
> trying to run a loop that does:
>
> gen var1=day2-day1
> gen var2=day3-day2
> gen var3=day4-day3...and so on.
>
> From previous statlist emails, I found how to run loops subtracting
> one series of variables from another
> (http://www.stata.com/statalist/archive/2012-01/msg00303.html), but
> I'm having a hard time trying to generate a loop where variables
> within the same series are subtracted from each other.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index