BookmarkSubscribeRSS Feed
trungcva112
Obsidian | Level 7

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.

1 REPLY 1
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 564 views
  • 0 likes
  • 2 in conversation