Quantcast

Stop looping when reaching a certain observation

classic Classic list List threaded Threaded
67 messages Options
1234
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Stop looping when reaching a certain observation

George Chioran
Dear Statalisters,
 
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        
 


Any help will highly appreciated.

Many thanks,
George



     
*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Stop looping when reaching a certain observation

Nick Cox
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
[hidden email]

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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: Stop looping when reaching a certain observation

Martin Weiss-5
In reply to this post by George Chioran

<>


What is it that makes you want to go through all these loops?



HTH
Martin

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

Dear Statalisters,
 
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        
 


Any help will highly appreciated.

Many thanks,
George



     
*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: RE: Stop looping when reaching a certain observation

Martin Weiss-5
In reply to this post by Nick Cox

<>

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




*************
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 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 Smit K 48.5  9        
08-Sep-08 ArcelorV 44.64  2  
08-Sep-08 Fugro K 47.83  6      
end

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

egen ssmean = mean(price) if kv!="V", by(ss)
list, noo
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [hidden email]
[mailto:[hidden email]] Im Auftrag von Nick Cox
Gesendet: Sonntag, 18. April 2010 18:18
An: [hidden email]
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
[hidden email]

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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

George Chioran
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 <[hidden email]>
To: [hidden email]
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: [hidden email]
[mailto:[hidden email]] Im Auftrag von Nick Cox
Gesendet: Sonntag, 18. April 2010 18:18
An: [hidden email]
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
[hidden email]

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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Martin Weiss-5

<>

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: [hidden email]
[mailto:[hidden email]] On Behalf Of George Chioran
Sent: Sonntag, 18. April 2010 23:06
To: [hidden email]
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 <[hidden email]>
To: [hidden email]
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: [hidden email]
[mailto:[hidden email]] Im Auftrag von Nick Cox
Gesendet: Sonntag, 18. April 2010 18:18
An: [hidden email]
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
[hidden email]

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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

George Chioran
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 <[hidden email]>
To: [hidden email]
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: [hidden email]
[mailto:[hidden email]] On Behalf Of George Chioran
Sent: Sonntag, 18. April 2010 23:06
To: [hidden email]
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 <[hidden email]>
To: [hidden email]
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: [hidden email]
[mailto:[hidden email]] Im Auftrag von Nick Cox
Gesendet: Sonntag, 18. April 2010 18:18
An: [hidden email]
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
[hidden email]

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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Martin Weiss-5

<>

" 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: [hidden email]
[mailto:[hidden email]] Im Auftrag von George Chioran
Gesendet: Sonntag, 18. April 2010 23:58
An: [hidden email]
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 <[hidden email]>
To: [hidden email]
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: [hidden email]
[mailto:[hidden email]] On Behalf Of George Chioran
Sent: Sonntag, 18. April 2010 23:06
To: [hidden email]
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 <[hidden email]>
To: [hidden email]
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: [hidden email]
[mailto:[hidden email]] Im Auftrag von Nick Cox
Gesendet: Sonntag, 18. April 2010 18:18
An: [hidden email]
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
[hidden email]

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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Count the number of observation of the same kind

George Chioran
In reply to this post by Nick Cox
Dear statalisters,

This may be a basic question, but how can you create a  variable X in which there we have the sum of the observations of the same kind of variable Y?
If variable Y has 10 integers: 8, 8, 5, 5, 5, 3, 1, 1, 1, 1 variable X would the integers 2, 2, 3, 3, 3, 1, 4, 4, 4, 4?

Thank you a lot for any ideas.

George


     
*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Count the number of observation of the same kind

Martin Weiss-5

<>

*******
clear*

inp byte Y
8
8
5
5
5
3
1
1
1
1
end

gen byte myorder=_n

bys Y: egen X=count(Y)

//reestablish previous order, if important
sort myorder
drop myorder

list, noo sepby(Y)
*******


HTH
Martin


-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of George Chioran
Sent: Samstag, 24. April 2010 16:05
To: [hidden email]
Subject: st: Count the number of observation of the same kind

Dear statalisters,

This may be a basic question, but how can you create a  variable X in which
there we have the sum of the observations of the same kind of variable Y?
If variable Y has 10 integers: 8, 8, 5, 5, 5, 3, 1, 1, 1, 1 variable X would
the integers 2, 2, 3, 3, 3, 1, 4, 4, 4, 4?

Thank you a lot for any ideas.

George


     
*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Count the number of observation of the same kind

Abhimanyu Arora
In reply to this post by George Chioran
You can use bys Y:gen X=_N
Abhimanyu

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of George Chioran
Sent: zaterdag 24 april 2010 16:05
To: [hidden email]
Subject: st: Count the number of observation of the same kind

Dear statalisters,

This may be a basic question, but how can you create a  variable X in which
there we have the sum of the observations of the same kind of variable Y?
If variable Y has 10 integers: 8, 8, 5, 5, 5, 3, 1, 1, 1, 1 variable X would
the integers 2, 2, 3, 3, 3, 1, 4, 4, 4, 4?

Thank you a lot for any ideas.

George


     
*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: RE: Count the number of observation of the same kind

Martin Weiss-5

<>


" You can use bys Y:gen X=_N"



Very true! Abhimanyu`s solution is preferable just in case George has
missings in there which he wants to -count- as well. See this comparison:


*******
clear*

inp byte Y
8
8
5
5
5
3
1
1
1
1
.
.
.
end

gen byte myorder=_n

bys Y: egen byte X=count(Y)
bys Y:gen byte Xalt=_N

//reestablish previous order, if important
sort myorder
drop myorder

list, noo sepby(Y)
*******


HTH
Martin


-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Abhimanyu Arora
Sent: Samstag, 24. April 2010 16:10
To: [hidden email]
Subject: st: RE: Count the number of observation of the same kind

You can use bys Y:gen X=_N
Abhimanyu

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of George Chioran
Sent: zaterdag 24 april 2010 16:05
To: [hidden email]
Subject: st: Count the number of observation of the same kind

Dear statalisters,

This may be a basic question, but how can you create a  variable X in which
there we have the sum of the observations of the same kind of variable Y?
If variable Y has 10 integers: 8, 8, 5, 5, 5, 3, 1, 1, 1, 1 variable X would
the integers 2, 2, 3, 3, 3, 1, 4, 4, 4, 4?

Thank you a lot for any ideas.

George


     
*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Seeing if the last observation equals previous observations from a variable X

George Chioran
In reply to this post by Nick Cox
Dear Statalisters,
 
I want to know how I
could see if the last observation in a variable X (containing strings) is equal
to other observations in the variable. I know I can use X[_N] to identify the
last observation in the variable, but how could I see if I have a previously
identical observation to X[_N] in the variable?
 
Thank you a lot for
the help,
George


     
*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Seeing if the last observation equals previous observations from a variable X

Tim Wade-2
George maybe something like this:

input str10 a b c
one 1 2
two 2 3
one 3 4
two 1 2
five 0 0
one 1 1
end

qui count
local count=r(N)
gen same=0

forvalues i=1/`count'{
replace same=1 in `i' if a[`i']==a[_N]
}


. list

     +---------------------+
     |    a   b   c   same |
     |---------------------|
  1. |  one   1   2      1 |
  2. |  two   2   3      0 |
  3. |  one   3   4      1 |
  4. |  two   1   2      0 |
  5. | five   0   0      0 |
     |---------------------|
  6. |  one   1   1      1 |
     +---------------------+

Hope this helps, Tim


On Sat, Apr 24, 2010 at 5:06 PM, George Chioran
<[hidden email]> wrote:

> Dear Statalisters,
>
> I want to know how I
> could see if the last observation in a variable X (containing strings) is equal
> to other observations in the variable. I know I can use X[_N] to identify the
> last observation in the variable, but how could I see if I have a previously
> identical observation to X[_N] in the variable?
>
> Thank you a lot for
> the help,
> George
>
>
>
> *
> *   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Seeing if the last observation equals previous observations from a variable X

Martin Weiss-5
In reply to this post by George Chioran

<>


You can create a dummy denoting any observation, other than the last one,
that is equal to the string in the last one:

*******
clear*

input str10 mystrvar
"hello"
"good-bye"
"hello"
"good-bye"
"hello"
end

//want comparison with everybody else...
gen byte dupllast=mystrvar==mystrvar[_N] if _n!=_N
list, noo
cou if dupllast==1
*******


HTH
Martin

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of George Chioran
Sent: Samstag, 24. April 2010 23:06
To: [hidden email]
Subject: st: Seeing if the last observation equals previous observations
from a variable X

Dear Statalisters,
 
I want to know how I
could see if the last observation in a variable X (containing strings) is
equal
to other observations in the variable. I know I can use X[_N] to identify
the
last observation in the variable, but how could I see if I have a previously
identical observation to X[_N] in the variable?
 
Thank you a lot for
the help,
George


     
*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Placing a string variable in a local/global

George Chioran
In reply to this post by George Chioran
Dear Statalisters,

I am assigning the names of certain observations from variable X to a local/global with the command: local n = X[_N]. Say that now `n' has the string "bear" stored in it.
Next I load a larger data base where variable X has several string observations named bear among others.
For the final step I am trying to record the observations from variable Z where `n' = X in a variable y. My code is this:

local n = X[_N]
use "C:\Users\George\Desktop\Methodology\Final data1.dta", clear
gen y = Z if `n' == X

However when I do this, I get the error message: unrecognized command:  bear

Do you know how I could store observations from X so that when I load a larger data set I can use them to see how many similar observations I have in the larger data set?


Thank you a lot for the help,
George


     
*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Placing a string variable in a local/global

Martin Weiss-5

<>


You want your qualifier to be

*************
if X  =="`n'"
*************


HTH
Martin


-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of George Chioran
Sent: Dienstag, 27. April 2010 22:15
To: [hidden email]
Subject: st: Placing a string variable in a local/global

Dear Statalisters,

I am assigning the names of certain observations from variable X to a
local/global with the command: local n = X[_N]. Say that now `n' has the
string "bear" stored in it.
Next I load a larger data base where variable X has several string
observations named bear among others.
For the final step I am trying to record the observations from variable Z
where `n' = X in a variable y. My code is this:

local n = X[_N]
use "C:\Users\George\Desktop\Methodology\Final data1.dta", clear
gen y = Z if `n' == X

However when I do this, I get the error message: unrecognized command:  bear

Do you know how I could store observations from X so that when I load a
larger data set I can use them to see how many similar observations I have
in the larger data set?


Thank you a lot for the help,
George


     
*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: RE: Placing a string variable in a local/global

Martin Weiss-5

<>

See this example:


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

input str10 X
        bee
        lion
        duck
        rabbit
        bear
end

list, noo

local n = X[_N]

cou if X=="`n'"

*************


HTH
Martin


-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Martin Weiss
Sent: Dienstag, 27. April 2010 22:21
To: [hidden email]
Subject: st: RE: Placing a string variable in a local/global


<>


You want your qualifier to be

*************
if X  =="`n'"
*************


HTH
Martin


-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of George Chioran
Sent: Dienstag, 27. April 2010 22:15
To: [hidden email]
Subject: st: Placing a string variable in a local/global

Dear Statalisters,

I am assigning the names of certain observations from variable X to a
local/global with the command: local n = X[_N]. Say that now `n' has the
string "bear" stored in it.
Next I load a larger data base where variable X has several string
observations named bear among others.
For the final step I am trying to record the observations from variable Z
where `n' = X in a variable y. My code is this:

local n = X[_N]
use "C:\Users\George\Desktop\Methodology\Final data1.dta", clear
gen y = Z if `n' == X

However when I do this, I get the error message: unrecognized command:  bear

Do you know how I could store observations from X so that when I load a
larger data set I can use them to see how many similar observations I have
in the larger data set?


Thank you a lot for the help,
George


     
*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Dropping string observations that have a sequence of characters

George Chioran
In reply to this post by George Chioran

Dear Statalisters,

I have a basic question, but which I still have not been able to solve. How can you delete observations from a variable that contains strings that have the letters "ur" for instance.

For example if the variable has the observations: "venus" "mercury" "mars" "Jupiter" "saturn" "uranus" "Neptune". The how could I delete the observations that contain the letters "ur", in this case "saturn" and "uranus" ?

Thank you for your help,
George


     
*
*   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/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dropping string observations that have a sequence of characters

Dr. Bill Westman
assuming that var1 is type string with those planets, will this work?

g var2 = subinstr( var1,"ur","",.)

--- On Tue, 4/27/10, George Chioran <[hidden email]> wrote:

> From: George Chioran <[hidden email]>
> Subject: st: Dropping string observations that have a sequence of characters
> To: [hidden email]
> Date: Tuesday, April 27, 2010, 5:35 PM
>
> Dear Statalisters,
>
> I have a basic question, but which I still have not been
> able to solve. How can you delete observations from a
> variable that contains strings that have the letters "ur"
> for instance.
>
> For example if the variable has the observations: "venus"
> "mercury" "mars" "Jupiter" "saturn" "uranus" "Neptune". The
> how could I delete the observations that contain the letters
> "ur", in this case "saturn" and "uranus" ?
>
> Thank you for your help,
> George
>
>
>      
> *
> *   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/
1234
Loading...