first -difference command on STATA

abc

New Member
#1
I have a mcor panel for about 40 countries for 24 years .I want to take the first -difference for a variable for each country's time series. Do you know what is the command?
 

abc

New Member
#3
I followed your suggestion but sorry to say that it does not seem to work . I run the command
tsset country year
gen diff_CPI = D.xCPI

and what I get is the same main variable data without the first year observation. In other worlds

year CPI DCPI
1990 7.27226
1991 3.22268 7.27226
1992 0.985915 3.22268
1993 1.81311 0.9859155
1994 1.89498 1.81311
1995 4.63814 1.894977
1996 2.61242 4.638136
1997 0.250417 2.61242


A you can see this is not a first difference , I get for the CPI variable and the 1991 year data the observation that was for 1990c instead of getting their difference. This goes for all data and counties .I repeat tat I work on a macro panel that contains 55 countries for a time length of about 20 years and need the first difference of a number of variables.

Thanks

abc..
 
#5
Here is a toy example of Maarten's approach.
Note that tsset can only accept numeric variables, so
you need a numeric variable for country.

Code:
clear

input str2 country year x
US 1990 7.27226 
DE 1990 3.22268 
US 1991 0.985915
DE 1991 1.81311 
US 1992 1.89498 
DE 1992 4.63814 
US 1993 2.61242 
DE 1993 0.250417
end

* tsset variables must be numeric
* so this creates numeric code for country
encode country, gen(ccode)

list 

tsset ccode year
gen diff_x = D.x

list
 
Last edited:

abc

New Member
#6
I don't see anything wrong with Maarten's solution, but I don't
have your data to allow me to do any diagnosis.

Anyway, you may want to read the advice on first-differences from Austin Nichols
on StataList at http://www.stata.com/statalist/archive/2011-09/msg00680.html.


Yes this one works like a charm! Having missing values in the time variable in my panel I managed to solve by running this command

tsset ccode year
g Dx = x-L.x
 

maartenbuis

TS Contributor
#7
Now I am very worried. You should never have to do something like x - L.x. If that "solves" your problem, i.e. if that leads to different results than just D.x, then that is a sign that there is something seriously wrong with your data setup. If you tried RedOwl's example as is and added your "solution" at the bottom you will see that it to leads to missing values at the first observation of each country, as it should: how can you compute a difference with a previous value, if you are the first and by definition no such previous value exists?