Statalist


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

Re: st: Creating a variable


From   "Mike Kim" <[email protected]>
To   <[email protected]>
Subject   Re: st: Creating a variable
Date   Mon, 28 Dec 2009 13:50:45 -0600

<>

Thank you very much, Martin and Eric. I have a follow-up question. The data
structure is repeated by country and I have around 60 countries. So, I
modified Martin's foreach loop, but it works only for the first country. The
result is wrong for the remaining countries. Can you please advise me what
is wrong?

Mike.

* I tried only two countries in the code below:
* The data structure is like this and the number of id is different for each
country.

inp str10 country byte id str10 date byte revenue
arg  1  8-Mar-07  1
arg  2  15-Mar-07 2
arg  3  15-Mar-07 3
arg  4  22-Mar-07 4
arg  5  29-Mar-07 5
arg  6  29-Mar-07 6
arg  7  5-Apr-07  7
arg  8  5-Apr-07  8
bel  1  8-Mar-07  10
bel  2  15-Mar-07 20
bel  3  15-Mar-07 30
bel  4  22-Mar-07 40
bel  5  29-Mar-07 50
bel  6  29-Mar-07 60
bel  7  5-Apr-07   70
end

encode country, gen(cntry)
drop country

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

gen byte newvar=.
forv i=1/2 {
 loc j 0
 levelsof id
 foreach lev in `r(levels)'{
 egen mytotal`lev'=total(inrange(mydate, mydate[`lev']-14,
mydate[`lev'])*revenue) if cntry==`i'
 loc ++j
 replace newvar=mytotal`lev'-revenue[`lev'] in `j' if cntry==`i'
 }
drop mytotal*
}

----- Original Message ----- 
From: "Martin Weiss" <[email protected]>
To: <[email protected]>
Sent: Monday, December 28, 2009 3:49 AM
Subject: Re: st: Creating a variable


<>

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/

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