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 on April 23, and its replacement, statalist.org is already up and running.


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

RE: st: How to list all the observation meeting the conditions which are the former results?


From   "Keith Dear" <Keith.Dear@anu.edu.au>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: How to list all the observation meeting the conditions which are the former results?
Date   Mon, 13 Dec 2010 09:49:28 +1100

Or maybe something like this?


preserve
  keep if z==5
  keep x y
  duplicates drop
  sort x y
  tempfile pairs 
  save `pairs'
restore
sort x y
merge m:1 x y using `pairs'
list if _merge==3





-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Eric Booth
Sent: Monday, 13 December 2010 9:26 AM
To: <statalist@hsphsun2.harvard.edu>
Subject: Re: st: How to list all the observation meeting the conditions
which are the former results?

<>

You could build your -if- conditions in a macro, see the example below:

**********************!
//1. dataset_target//
/*
you will apply the conditions in which z<=5 
in the "dataset_source" to this dataset later on
*/

clear
set obs 5000
foreach a in x y z {
	g `a' = int(abs(runiform()*50))
}
sa "dataset_target.dta", replace




//2.  dataset_source//
/*
this is the dataset where you get the values 
of x & y where z<=5
*/

clear
set obs 50
foreach a in x y z {
	g `a' = int(abs(runiform()*50))
}

l if z<=5

//3.  put  x&y pairs where z<=5 in global macro //
global myif 
forval n = 1/`=_N' {
if `=z[`n']'<=5 {
global myif | (x==`=x[`n']' & y==`=y[`n']') $myif
di in r "$myif" 
}
}
global myif:subinstr global myif "|" ""
global myif if $myif


	*--Here are the conditions where z<=5--*
di in y "$myif"

	*--list these obs. in source dataset--*
li z $myif



//4. use conditions in global $myif on target dataset(s)//
u "dataset_target.dta", clear
 li z $myif
**********************!
 
- Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu
Office: +979.845.6754
Fax: +979.845.0249
http://ppri.tamu.edu



On Dec 12, 2010, at 8:38 AM, Grace Jessie wrote:

> Thank you for reply.
> It seems the problem has not been solved.
> I do not want to list all the observations meeting "z<5".
> Firstly, I get the combination of x and y appearing in the results of
"z<5". Then I want to list all the observations with the combination of
x and y, maybe in the current data or in another data.
> In my firt posting,
> "I know -levelsof- can solve the problem if not the combination but
just one variable."
> For example,
> levelsof x if z<5,loc(a)
> foreach i of loc a {
> l if x==`i'
> }
> Now I doubt if not a variable but a combination (such as two
variables),
> how to do it?
> Grace
> 
> 
> ----------------------------------------
>> From: eric.de_souza@coleurope.eu
>> To: statalist@hsphsun2.harvard.edu
>> Date: Sun, 12 Dec 2010 15:13:40 +0100
>> Subject: RE: st: How to list all the observation meeting the
conditions which are the former results?
>> 
>> input x y z
>> ....
>> gen id = _n
>> l
>> preserve
>> keep if z<5
>> l
>> restore
>> 
>> This way you have the observation numbers in the original dataset
>> 
>> Or do you want something else?
>> 
>> Eric de Souza
>> College of Europe
>> BE-8000 Brugge (Bruges)
>> Belgium
>> 
>> -----Original Message-----
>> From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Grace Jessie
>> Sent: 12 December 2010 13:46
>> To: statalist@hsphsun2.harvard.edu
>> Subject: st: How to list all the observation meeting the conditions
which are the former results?
>> 
>> Dear statalists,
>> How to list all the observation meeting the conditions which are the
former results?
>> For example,
>> . clear
>> . input x y z
>> x y z
>> 1. 1 2 8
>> 2. 1 2 2
>> 3. 2 3 4
>> 4. 2 3 9
>> 5. 2 4 8
>> 6. end
>> . l
>> +-----------+
>> | x y z |
>> |-----------|
>> 1. | 1 2 8 |
>> 2. | 1 2 2 |
>> 3. | 2 3 4 |
>> 4. | 2 3 9 |
>> 5. | 2 4 8 |
>> +-----------+
>> . l if z<5
>> +-----------+
>> | x y z |
>> |-----------|
>> 2. | 1 2 2 |
>> 3. | 2 3 4 |
>> +-----------+
>> Now I want to list all the observations for each x and y combination
in the former results,i.e.,(x==1 & y==2) | (x==2 | y==3).
>> I know I can type "l if (x==1 & y==2) | (x==2 | y==3)".
>> However, typing one by one is troublesome if the number of the
combinations for the former results is large.
>> I know -levelsof- can solve the problem if not the combination but
just one variable.
>> Is there a way like -levelsof- ?
>> Thank you!
>> Grace
>> 



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