Compute lag mean sale with conditions

Reply
Contributor
Posts: 42

Compute lag mean sale with conditions

Hi everyone,

 

I have a panel dataset below.

Now I have to compute a variable named "Lag mean sales". For example, the "lag mean sale" of firm 1 in the year 2002 = mean of sales over 4 quarters in the year 2001.

 

The tricky part is that I have to remove rows with missing sale data and rows with growth of sales is >75% or <-75%.

For example, the "lag mean sales" of firm 1 in the year 2002 = (10+20+21)/3 = 17

                      the "lag mean sales" of firm 1 in the year 2003 =  (12+18)/2 = 15

firmsYearSalesGrowth
1QI/200110.
1QII/200117.575%
1QIII/20012014%
1QIV/2001215%
1QI/200212-43%
1QII/20021417%
1QIII/2002..
1QIV/2002            .             .
1QI/200311             .
1QII/20038-27%
1QIII/20031250%
1QIV/20031525%

 

The output data is like this:

firmsYearSalesGrowthLag mean sale
1QI/200110. 
1QII/200117.575% 
1QIII/20012014% 
1QIV/2001215% 
1QI/200212-43%17
1QII/20021417%17
1QIII/20021614%17
1QIV/2002..17
1QI/200311.14
1QII/20038-27%14
1QIII/20031250%14
1QIV/20031525%14

 

Could someone show me how to do it, please? Any idea is much appreciated.

Super User
Super User
Posts: 9,441

Re: Compute lag mean sale with conditions

Posted in reply to trungcva112

You would use two retained variables, perhaps something like:

data want;
  set have;
  retain lag_mean_sale no_elem;
  by year;
  if first.year then do;
    lag_mean_sale=sales;
    no_elem=1;
  end;
  else do;
    if sales ne . then do;
      lag_mean_sale=sum(lag_mean_sale);
      no_elem=sum(no_elem,1);
    end;
  end;
run;
Ask a Question
Discussion stats
  • 1 reply
  • 85 views
  • 0 likes
  • 2 in conversation