Bookmark and Share

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]

AW: st: AW: RE: Stop looping when reaching a certain observation


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   AW: st: AW: RE: Stop looping when reaching a certain observation
Date   Mon, 19 Apr 2010 09:45:53 +0200

<> 

" Although I would also need to somehow keep the observations on line V, so
dropping everything after V."




Say -gen byte num=sum(kv[_n-1]=="V")- then:

*************
clear*

inp str15(date fondsnaam kv) price byte ss  
16-May-08 Grontmij K 24.7  7        
03-Jul-08 Fortis K 10.175  
11-Jul-08 AMG K 49.73  1  
14-Jul-08 AMG K 49.59  1  
17-Jul-08 Arcelor V 51.92  2  
25-Jul-08 Fugro V 41.35  6  
11-Aug-08 Fugro K 46.69  6  
18-Aug-08 Nutreco K 43.35  8  
21-Aug-08 Fugro K 49.42  6        
05-Sep-08 Smit K 48.5  9        
08-Sep-08 Arcelor V 44.64  2  
08-Sep-08 Fugro K 47.83  6      
end

gen mydate=date(date, "DM20Y")
form mydate %td


gen byte num=sum(kv[_n-1]=="V")

list, noo sepby(num)
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von George Chioran
Gesendet: Sonntag, 18. April 2010 23:58
An: [email protected]
Betreff: Re: st: AW: RE: Stop looping when reaching a certain observation

Thank's a lot Martin!

I was searching for something like gen byte num=sum(kv=="V"). With it I can
now drop the observations after V appears, by conditioning it drop if
num>0/1, and simply doing the average of what is left.Although I would also
need to somehow keep the observations on line V, so dropping everything
after V.
ss is an identifier for fondsnaam, by which I group the prices that I need
to average until I reach a V in the sequence. 


----- Original Message ----
From: Martin Weiss <[email protected]>
To: [email protected]
Sent: Sun, April 18, 2010 11:21:22 PM
Subject: RE: st: AW: RE: Stop looping when reaching a certain observation


<>

Remember you can condition on the event "kv=="V"" via 

*******
gen byte num=sum(kv=="V")
*******

which creates an indicator variable for the number of times you have
witnessed the event. 

What is the relevance of "ss", given that you are watching out for changes
in "kv"?


HTH
Martin

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of George Chioran
Sent: Sonntag, 18. April 2010 23:06
To: [email protected]
Subject: Re: st: AW: RE: Stop looping when reaching a certain observation

Thank you for the insights they are useful. 
However, that does not solve my problem. I need to do the average price
until a V is encountered and then stop, not just exclude the V's.
Maybe a way of achieving this would be to drop all the observations after a
V is encountered, and then simply do the average.
Does anyone know how to drop all the data after a certain observation? Or
maybe segment the data  into different files according to the V's?


________________________________
From: Martin Weiss <[email protected]>
To: [email protected]
Sent: Sun, April 18, 2010 6:31:30 PM
Subject: st: AW: RE: Stop looping when reaching a certain observation


<> 

If ever you wanted Stata to stop calculating means when it sees a "V" in
"kv", you could of course condition Nick`s call as -egen ssmean =
mean(price) if kv!="V", by(ss)-

HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Nick Cox
Gesendet: Sonntag, 18. April 2010 18:18
An: [email protected]
Betreff: st: RE: Stop looping when reaching a certain observation

There is absolutely no need to loop to solve your problem, of
calculating means by company. 

egen ssmean = mean(price), by(ss) 

or 

using -table- or -tabstat- 

are two of many much simpler methods. 

Nick 
[email protected] 

George Chioran

I am trying to figure out how to stop at certain observations while
looping through a variable. Currently I am looping through the price
variable, and making the average price sorted by company. I would like
my program to stop doing the loop when variable kv is equal to V
However, I cannot manage to stop the loop when variable kv is equal to V
(at the price of 51.92 in the first instance) 
as in a sample of my database attached below. 
  
My program looks like this:

local j = 1
while `j' <= 10 {      
               preserve
               keep if ss == `j'
               su price
               local `j' = _result(3) 
               restore
               local j = `j' +1
               }

Data base sample:

date fondsnaam kv price ss  
16-May-08 Grontmij K 24.7  7        
03-Jul-08 Fortis K 10.175  
11-Jul-08 AMGK 49.73  1  
14-Jul-08 AMGK 49.59  1  
17-Jul-08 Arcelor V 51.92  2  
25-Jul-08 Fugro V 41.35  6  
11-Aug-08 Fugro K 46.69  6  
18-Aug-08 Nutreco K 43.35  8  
21-Aug-08 Fugro K 49.42  6        
05-Sep-08 SmitK 48.5  9        
08-Sep-08 ArcelorV 44.64  2  
08-Sep-08 Fugro K 47.83  6        


      

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



      

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index