How kan I lag variables in a panel data

Reply
Occasional Contributor MCB
Occasional Contributor
Posts: 15

How kan I lag variables in a panel data

I hava data organized as a panel data:

PersonIDYearIncomeLag_income
12001100.
12002200100
12003250200
22000400.
22001500400
22007700500
22008800700

The data in this exempel consists of two individuals, five years, income for each year and the lag(income) that I created by the following data step:

data x;

set x;

by personID year;

Lag_income=lag(income);

if first.PersonID then do;

     Lag_income=.;

end;

run;

I am content with most of the results except för the Lag_income för PersonID nr 2 on year 2007 (value 500 bold). I have actually no information in the data about the income between 2001 and 2007 which I would like SAS to concider by giving the Lag(income) a missing value in stead of 500. If I use STATA it would work that way, but I can't find the way to tix it in SAS.  Thanks for any tip!

Marcela

Super User
Posts: 10,458

Re: How kan I lag variables in a panel data

Use LAG or DIF on the year as well. If the difference is greater than one year set to missing.

Respected Advisor
Posts: 3,124

Re: How kan I lag variables in a panel data

As suggested, here is one way of doing it:

data want;

set x;by personid;

  lag_income=ifn(first.personid or dif(year) ne 1,.,lag(income));

run;

Haikuo

Occasional Contributor MCB
Occasional Contributor
Posts: 15

Re: How kan I lag variables in a panel data

Thank you Haikuo and ballardw, I tried your syntax but my SAS wont recognize the funcion ifn. In my real panel data I have to lag more then one variable.

Thans any way

Marcela

Super Contributor
Posts: 644

Re: How kan I lag variables in a panel data

This should work on older versions of SAS ;

data x;

set x;

by personID year;

Lag_income=lag(income);

Dif_Year = DIF (Year) ;

if first.PersonID then Lag_income=.;

Else if Dif_Year > 1 then Lag_income=.;

run;

Richard

Occasional Contributor MCB
Occasional Contributor
Posts: 15

Re: How kan I lag variables in a panel data

Thank you Richard!

I tried somthing close to your suggestion and it worked well:

data x;

set x;

by personID Year;

lag_income=lag(income);

if first PersonID or year-lag(year)>1 then do;

lag_income=.;

end;

run;

Super Contributor
Posts: 644

Re: How kan I lag variables in a panel data

Note that

     dif (year) = year - lag(year)

     do; <statement>; end; is redundant use of do

Be careful with lag() and dif() used in IF clauses - yours seems OK but the following can be a problem

if <condition> then X = lag(Y) ;

Its because the lag function needs to be evaluated in the record before <condition> is true, so there is a retained lagged value to be used in the rest of the statement.

Richard

Contributor
Posts: 52

Re: How kan I lag variables in a panel data

Haikuo

Does ifn command work only in case we are creating lag for one variable?

I was looking at my results and realized that the same does not work while creating lags for multiple variables.

However, I had not known about this ifn function; it is a pretty neat one!

Ask a Question
Discussion stats
  • 7 replies
  • 1355 views
  • 4 likes
  • 5 in conversation