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: replacing previous observations according to current observation, foreach? replace?


From   Eric Booth <ebooth@ppri.tamu.edu>
To   "<statalist@hsphsun2.harvard.edu>" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: replacing previous observations according to current observation, foreach? replace?
Date   Thu, 2 Dec 2010 20:11:35 +0000

<>

On Dec 2, 2010, at 1:14 PM, Charlotte Picard wrote:
> I am trying to create code that will find negative values for Cases and subtract them to the cases from the previous week, if that weeks cases then become negative it will subtract them from the week before that and so on until the case count isn't negative anymore and fill in these new values of case in a new variable.


I'm not sure I completely understand.  An example of what you want this new variable to look like would be helpful.

Below is some code that does what I _think_ you are asking for -- that is, when "cases" value is negative, it adds that negative "cases" value to the previous week's "cases" value within (-by-) each region and puts that new value in a new variable ("cases2").  If "cases2" is still not positive after being added to the prior week's "case" value, then the negative "case2" is added to "cases" from to 2 weeks prior and so on until "case2" is >= 0 or the number of weeks runs out.

I changed the value in region 4, week 3 to show an example of a negative value that never goes positive because it runs out of weeks to go back in time.

Look at the changes in the "list" command at each step of the loop to see that changes that are being made to "cases2".  Only regions 2 and 4 are affected because the others do not have negative "cases".

*******!
clear
inp region week cases
1             1       5
2             1       4
3             1       0
4             1       2
1             2       3
2             2       2
3             2       1
4             2       0
1             3       2
2             3      -3
3             3       0
4             3      -4
end


sort region week
clonevar cases2 = cases

**compare tables in the loop to this table**
l if inlist(region, 2, 4), sepby(region)

qui su week, d
forval n = 1/`r(max)' {
by region: replace cases2 = cases[_n-`n']+cases2  ///
	if cases2<0 & region[_n] == region[_n-`n']
di in r "Changes based on `n' Weeks Prior to Week `n' -->"
li if inlist(region, 2, 4) , sepby(region)
}

*******!

- Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu
Office: +979.845.6754


On Dec 2, 2010, at 1:14 PM, Charlotte Picard wrote:

> Hello Stata users,
> I am a new stata user and I am trying to work backwards in my data.
> 
> Region Week Cases
> 1             1       5
> 2             1       4
> 3             1       0
> 4             1       2
> 1             2       3
> 2             2       2
> 3             2       1
> 4             2       0
> 1             3       2
> 2             3      -3
> 3             3       0
> 4             3       4
> 
> I am trying to create code that will find negative values for Cases and subtract them to the cases from the previous week, if that weeks cases then become negative it will subtract them from the week before that and so on until the case count isn't negative anymore and fill in these new values of case in a new variable.  I want to do this within each region seperately but I'm having trouble determining if I need to make some sort of foreach loop, which I've never tried before of if there is a simpler way.
> 
> *
> *   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/




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