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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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