Creation of the variable "GDP in 1960" from GDP variables for different years

I am new to Stata.

I have a set of comments on the "Country GDP Year" form. I want to create a new variable GDP1960 that gives the 1960 GDP 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 correct syntax for this? (I guess I'm egen

involved in some mysterious way)

+2


a source to share


4 answers


Well I found a solution at the end. It relies on the fact that generate

it is replace

working with data in ordered order and that you can reference 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

      

+1


a source


You found a solution with -cond () -, but here are some suggestions that might make modeling your data easier and help you avoid problems with sorting problems by creating a "rank" variable (and I have -egen-soln which you asked about below):

Paste the below code 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 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.

+2


a source


I can't think of anything less than these two lines:

gen temp = gdp if year == 1960
by country : egen gdp60 = max(temp) 

      

If you need a variable for each year (for example gdp60, gdp61, gdp62,...

) you should probably usereshape

0


a source


One line solution

   egen gdp60 = mean(gdp / (year == 1960)), by(country) 

      

The trick here is division by expression year == 1960

. This is true for 1960, in which case we are dividing by 1, which leaves gdp

that year unchanged. This is not true for all other years, in which case we are dividing by 0. It sounds crazy, but the consequence is every time we divide by zero, there are simply no values ​​to be ignored by the function egen

mean()

.

You can use the other functions egen

, as in this case, for each country should be no more than one value for 1960, that is, for example max()

, min()

, total()

should all work. (If country doesn't matter for 1960 or missing value, we get missing, which is exactly as it should be.)

Discussion http://www.stata-journal.com/article.html?article=dm0055

0


a source







All Articles