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 at the end of May, and its replacement, statalist.org is already up and running.


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

st: AW: If-condition


From   "Mareike" <MareikeHahr@aol.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: AW: If-condition
Date   Sat, 3 Jul 2010 10:52:18 +0200

Dear Eric,

Thanks a lot for your answer. In general, I understand the logic behind your
code and I think it might work for me. 
I actually just realized that I need to make the condition a bit more
complex: I need to tell Stata to only work with a specific subsample of the
data, in which two specific variables take non-missing values, and then to
take the latest observation that is available for a certain country in this
restricted sample.
 I tried to change your code a bit to do so, but it didn't have the expected
result
	...
 	 by idcode: g insample = 1 if  var1!=. & var2!=. & _n ==_N
	...

Insample correctly takes the value "1" in 2008 for those countries that are
part of the subsample and for which there exists an observation for the year
2008. But if for a certain country the latest observation available is for
example 2005, 'insample' doesn't display a "1" but still a missing value.

Mareike


-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Eric Booth
Gesendet: Freitag, 2. Juli 2010 23:14
An: statalist@hsphsun2.harvard.edu
Betreff: st: If-condition

<>


Mareike:

Here's one way to do it:
************************
webuse union, clear

//setup the example data//
keep idcode year age black union south
replace year = 1900+year
        **
ds year idcode, not
foreach x in `r(varlist)' {
        bys idcode: egen ln_mean_`x' = mean(`x')
        drop `x'
    }


//create "insample" indicator for last obs in panel//
sort idcode year
by idcode: g insample = 1 if _n ==_N
ta year insample, miss   

//use "insample" to select the last observation, despite the year//
regress ln_mean_age ln_mean_south ln_mean_union ln_mean_black if insample==1
************************

 ~ Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu
Office: +979.845.6754




> From    "Mareike" <MareikeHahr@aol.com>
> To      <statalist@hsphsun2.harvard.edu>
> Subject     st: If-condition
> Date    Fri, 2 Jul 2010 21:55:25 +0200
> Dear Statagroup,
> 
> the original data I'm working with has a panel structure but as a
robustness
> test, I now would like to run an OLS regression on average values.
> Therefore, I used mean values to build a cross section of the independent
> variables and defined the dependent variable to be the sum of its values
> over the whole observation period. 
> In order to tell Stata to use only one observation per country I
originally
> added an If-condition that restricted the sample to include only the
latest
> observation available in the dataset
>   regress ln_sum_y ln_mean_x1 ln_mean_x2 mean_x3 ln_mean_x4 if year
> ==2008
> 
> But the problem is that for some countries there are no observations for
> 2008, but only for earlier years. So I need to tell Stata to use the
latest
> observation that is available for a certain country. As easy as it may
sound
> I could not come up with an idea of how to define the If-condition in the
> right way. 
> 
> I would be really grateful if anybody of you could come up with an idea.
> 
> Mareike
> 



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