BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hk24
Obsidian | Level 7

I'm looking to calculate YoY growth rate of a few products that fall into different categories. Below is a sample data

YearCategoryProductProduct_IDsales
2022DairyMilk1011200
2022DairyPlain Yogurt1021434.5
2022DairyFlavored Yogurt1032452
2022CondimentSalsa1041345
2022CondimentSoy Sauce1053456
2022CondimentRanch1061934
2022DairyEgg1072950.6
2022Cleaning SuppliesMop1082745
2022Cleaning SuppliesLiquid Detergent1095548
2023DairyFlavored Yogurt1033756
2023DairyMilk1013658
2023DairyPlain Yogurt1022534.4
2023CondimentSoy Sauce1050
2023CondimentSalsa1042455
2023CondimentRanch1063456
2023DairyEgg1074793
2023Cleaning SuppliesLiquid Detergent1092356
2023Cleaning SuppliesMop1082669

 

How can I calculate YoY growth rate of each product ID in the above scenario such that my final output looks like below using proc sql? If the sales for current year is 0, I would like its YoY to be "-". Please help me on this.

 

CategoryProductProduct_IDYoY
DairyMilk10167%
DairyPlain Yogurt10243%
DairyFlavored Yogurt10335%
CondimentSalsa10445%
CondimentSoy Sauce105-
CondimentRanch10644%
DairyEgg10738%
Cleaning SuppliesMop108-3%
Cleaning SuppliesLiquid Detergent109-135%

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile datalines truncover;
input Year        Category & $40.        Product & $40.         Product_ID        sales;
datalines;
2022        Dairy        Milk        101        1200
2022        Dairy        Plain Yogurt        102        1434.5
2022        Dairy        Flavored Yogurt        103        2452
2022        Condiment        Salsa        104        1345
2022        Condiment        Soy Sauce        105        3456
2022        Condiment        Ranch        106        1934
2022        Dairy        Egg        107        2950.6
2022        Cleaning Supplies        Mop        108        2745
2022        Cleaning Supplies        Liquid Detergent        109        5548
2023        Dairy        Flavored Yogurt        103        3756
2023        Dairy        Milk        101        3658
2023        Dairy        Plain Yogurt        102        2534.4
2023        Condiment        Soy Sauce        105        0
2023        Condiment        Salsa        104        2455
2023        Condiment        Ranch        106        3456
2023        Dairy        Egg        107        4793
2023        Cleaning Supplies        Liquid Detergent        109        2356
2023        Cleaning Supplies        Mop        108        2669
;

proc sql;
create table want as
select *,divide(sales-(select sales from have 
where  Category=a.Category and  Product=a.Product and Product_ID=a.Product_ID and year=a.year-1)
,sales) as YoY format=percentn8.2
 from have as a 
  where year=2023;
quit;
data want;
 set want;
 if YoY=.M then YoY=._;
run;

View solution in original post

3 REPLIES 3
Oligolas
Barite | Level 11

Shouldn't the YoY for milk be 204% reflecting a 204% increase in 2023 compared to 2022  😲?

(3658-1200)/1200

________________________

- Cheers -

s_lassen
Meteorite | Level 14

Something like this, i suppose:

proc sort data=have;                                                                                                                    
  by year product_id;                                                                                                                   
run;                                                                                                                                    
data want;                                                                                                                              
  merge have(where=(year=2022))                                                                                                         
     have(where=(year=2023) keep=year product_id sales rename=(sales=sales23));                                                         
  by product_id;                                                                                                                        
  if sales23>0 then                                                                                                                     
    YoY=(sales23-sales)/sales23;                                                                                                        
  drop sales: year;                                                                                                                     
  format YoY percent7.0;                                                                                                                
run;         

This will get the numbers you showed, but like @Oligolas I think the correct calculation would be YoY=(sales23-sales)/sales

Ksharp
Super User
data have;
infile datalines truncover;
input Year        Category & $40.        Product & $40.         Product_ID        sales;
datalines;
2022        Dairy        Milk        101        1200
2022        Dairy        Plain Yogurt        102        1434.5
2022        Dairy        Flavored Yogurt        103        2452
2022        Condiment        Salsa        104        1345
2022        Condiment        Soy Sauce        105        3456
2022        Condiment        Ranch        106        1934
2022        Dairy        Egg        107        2950.6
2022        Cleaning Supplies        Mop        108        2745
2022        Cleaning Supplies        Liquid Detergent        109        5548
2023        Dairy        Flavored Yogurt        103        3756
2023        Dairy        Milk        101        3658
2023        Dairy        Plain Yogurt        102        2534.4
2023        Condiment        Soy Sauce        105        0
2023        Condiment        Salsa        104        2455
2023        Condiment        Ranch        106        3456
2023        Dairy        Egg        107        4793
2023        Cleaning Supplies        Liquid Detergent        109        2356
2023        Cleaning Supplies        Mop        108        2669
;

proc sql;
create table want as
select *,divide(sales-(select sales from have 
where  Category=a.Category and  Product=a.Product and Product_ID=a.Product_ID and year=a.year-1)
,sales) as YoY format=percentn8.2
 from have as a 
  where year=2023;
quit;
data want;
 set want;
 if YoY=.M then YoY=._;
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
  • 3 replies
  • 673 views
  • 1 like
  • 4 in conversation