BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
JohnK
Calcite | Level 5

Dear SAS Users,

I have panel sales data as follows for different firms. How can I calculate year-on-year sales growth? I want something like growth=100*dif12(sales)/lag12(sales) but I don't want dif12 to compare values of different firms.

Thanks for any help in advance.

Firmyearmonthsales
120021
120022
120023
120024
120025
120026
120027
120028
120029
1200210
1200211
1200212
120031
120032
120033
100201112
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

ArthurT,

Your code would not be able to work, If there are some missing monthes or duplicated monthes. But yours is a fast solution.

data have;
input Firm     year     month     sales ;
date=mdy(month,1,year);
format date yymmdd.;
cards;
1     2002     1     1
1     2002     2     1
1     2002     3     1
1     2002     4     1
1     2002     5     1
1     2002     6     1
1     2002     7     1
1     2002     8     1
1     2002     9     1
1     2002     10     1
1     2002     11     1
1     2002     12     1
1     2003     1     1
1     2003     2     1
1     2003     3    1
;
run;
proc sql;
 create table want as
  select *,((select sales from have where firm=h.firm and date=intnx('month',h.date,-12)) -
            (select sales from have where firm=h.firm and date=intnx('month',h.date,-13)) ) /
            (select sales from have where firm=h.firm and date=intnx('month',h.date,-12)) as growth
   from have as h;
quit;


Ksharp

View solution in original post

2 REPLIES 2
art297
Opal | Level 21

Does the following do what you want?:

data want;

  set have;

  by firm;

  if first.firm then counter=1;

  else counter+1;

  growth=ifn(counter gt 12,100*dif12(sales)/lag12(sales),.);

run;

Ksharp
Super User

ArthurT,

Your code would not be able to work, If there are some missing monthes or duplicated monthes. But yours is a fast solution.

data have;
input Firm     year     month     sales ;
date=mdy(month,1,year);
format date yymmdd.;
cards;
1     2002     1     1
1     2002     2     1
1     2002     3     1
1     2002     4     1
1     2002     5     1
1     2002     6     1
1     2002     7     1
1     2002     8     1
1     2002     9     1
1     2002     10     1
1     2002     11     1
1     2002     12     1
1     2003     1     1
1     2003     2     1
1     2003     3    1
;
run;
proc sql;
 create table want as
  select *,((select sales from have where firm=h.firm and date=intnx('month',h.date,-12)) -
            (select sales from have where firm=h.firm and date=intnx('month',h.date,-13)) ) /
            (select sales from have where firm=h.firm and date=intnx('month',h.date,-12)) as growth
   from have as h;
quit;


Ksharp

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 4337 views
  • 3 likes
  • 3 in conversation