tags:

views:

62

answers:

3

Hi all,

I'm pretty new to stata...

I have a set of observations of the form "Country GDP Year". I want to create a new variable GDP1960, which gives the GDP in 1960 of each country for each year:

USA     $100m   1960        USA    $100m  1960  $100m
USA     $200m   1965   -->  USA    $200m  1965  $100m
Canada  $60m    1960        Canada $60m   1960  $60m

What's the right syntax to make this happen? (I assume egen is involved in some mysterious way)

+1  A: 

Well, I found a solution in the end. It relies on the fact that generate and replace work on the data in its sorted order, and that you can refer to the current observation with _n.

gen rank = 100
replace rank = 50 if year == 1960

gen gdp60 = .

sort country rank
replace gdp60 = cond(iso == iso[_n-1], gdp60[_n-1], gdp[_n])

drop rank

sort country year
Tom Smith
+2  A: 

Hi Tom:

You've found a solution with -cond()-, but here's a couple of suggestions that might make modeling your data easier and help you avoid problems with issues that might arise when sorting by creating your "rank" variable (and I've got the -egen- soln. that you asked about below):

** Paste the code below into your do-file editor and run it: **

*---------------------------------BEGIN EXAMPLE
clear

inp str20 country str10 gdp year
"USA"     "$100m"   1960        
"USA"     "$200m"   1965     
"Canada"  "$60m"    1960 
"Canada"  "$120m"   1965
"USA"     "$250m"   1970
"Mexico"  "$90m"    1970  
"Canada"  "$800m"   1970     
"Mexico"  "$160m"    1960 
"Mexico"  "$220m"   1965
"Mexico"  "$350m"   1975
end

//1. destring gdp so that we can work with it
destring gdp,  ignore("$", "m") replace

//2. Create GDP for 1960 var:
    bys country: g x = gdp if year==1960
    bys country: egen gdp60 = max(x)
    drop x

    **you could also create balanced panels to see gaps in your data**
        preserve
    ssc install panels
    panels country year
    fillin country year
    li   //take a look at the results win. to see how filled panel data would look
        restore

//3. create a gdp variable for each year (reshape the dataset)
    drop gdp60
    reshape wide gdp, i(country) j(year)

    **much easier to use this format for modeling
    su gdp1970
     **here's a fake "outcome" or response variable to work with**
    g outcome =  500+int((1000-500+1)*runiform())
    anova outcome gdp1960-gdp1970  //or whatever makes sense for your situation
*---------------------------------END EXAMPLE

Good luck.


Eric A. Booth [email protected]

eric.a.booth
Thanks for the detailed answer: as you guessed, that egen line is exactly what I had in mind. The panels code looks very useful too - I'll take a look at that.
Tom Smith