Statalist


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

Re: st: Creating a variable


From   "Martin Weiss" <[email protected]>
To   [email protected]
Subject   Re: st: Creating a variable
Date   Mon, 28 Dec 2009 10:49:02 +0100

<>

Mike may want to try this code. It is not as elegant as I (and Eric apparently) had hoped for, but it works.

***
clear*

inp byte id str10 date byte revenue  
1  8-Mar-07  1     
2  15-Mar-07 2      
3  15-Mar-07 3      
4  22-Mar-07 4      
5  29-Mar-07 5      
6  29-Mar-07 6      
7  5-Apr-07  7    
8  5-Apr-07  8    
end

gen int mydate=date(date, "DM20Y")
form mydate %tdMonth_DD,_CCYY
drop date

gen byte newvar=.
loc j 0
levelsof id

foreach lev in `r(levels)'{
	egen mytotal`lev'=total(inrange(mydate, mydate[`lev']-14, mydate[`lev'])*revenue)
	loc ++j
	replace newvar=mytotal`lev'-revenue[`lev'] in `j'
}

drop mytotal*

list, noo
***


HTH 
Martin
-------- Original-Nachricht --------
> Datum: Sun, 27 Dec 2009 22:11:06 -0600
> Von: "Mike Kim" <[email protected]>
> An: [email protected]
> Betreff: st: Creating a variable

> Dear all,
> 
> I would like to create a variable that adds all revenues from all OTHER id
> within the past 14 days (including current date). How can I create the 
> "newvar" in the following example?
> 
> id      date       revenue  newvar
> 1    8-Mar-07      1         0
> 2    15-Mar-07    2         4     (=1+3)
> 3    15-Mar-07    3         3     (=1+2)
> 4    22-Mar-07    4         6     (=1+2+3)
> 5    29-Mar-07    5        15     (=2+3+4+6)
> 6    29-Mar-07    6        14     (=2+3+4+5)
> 7    5-Apr-07       7        23     (=4+5+6+8)
> 8    5-Apr-07       8        22     (=4+5+6+7)
> ......
> 
> 
> Thank you in advance.
> Mike.
> 
> *
> *   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/

-- 
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/atbrowser
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index