BookmarkSubscribeRSS Feed
MaxiHösi
Fluorite | Level 6

Hi SAS-Forum-Members,

 

I have a problem with interpolating quarterly data into monthly data.

I have a data that looks like that:

 

Car_BrandYearSales_Q1Sales_Q2Sales_Q3Sales_Q4
Hyundai

2018

100130115100
BMW2018100130115100
Tesla2018120150135120
..................
Hyundai2019200260305290
BMW 2019220280325310
..................
Hyundai

2020

300600570600
BMW2020320620590620
..................


As you can see there are not always the same Car Brands in every year. The Car Brands can be different every year e.g. when a car_brand is insolvent (usually its the same but in the example Tesla has no sales in 2019, 2020 and so on).

My aim is to interpolate the quarterly data into monthly data. For the first quarter it should look like this:

Car_BrandYearSales_JanSales_FebSales_MarSales_Apr
Hyundai

2019

133,333166,666200...
BMW2019140180220...
..................
Hyundai2020293,333296,666300...
BMW2020313,333316,666320...
..................

 


I need the value of the 4th quarter of 2019 to interpolate the values of January, February and March 2020 and that caused me great difficulties

 

I hope you understand what my problem is and can help me.

 

Thanks a lot and have a nice day.

3 REPLIES 3
LinusH
Tourmaline | Level 20

So you need the previous quarter sales to create a curve for the next quarters months?

But I don't understand what interpollation means here. Aren't the sales figures a sum of sales for each quarter? Wouldn't you then divide each month by three (after creating your curve)...?

Or does month mean rolling 3 months sales? Then it makes sense.

Also, didn't understand how you want to manage brands that doesn't exist certain periods. In you result table you have removed Tesla.

 

First of all, transpose your data so you have one row per brand, year and quarter (this should always be the default structure when storing data).

Sort by brand, year and quarter.

You can then use RETAIN in a data step to keep the previous quarters sales when looking at the next record.

You can keep track on that you are working with the same brand by using BY statement, and use first. and last. logic to assign and calculating values.

Pseudo code for last step, untested:

data want;
   set have;
   by brand;
   retain prev_qsales;
   if not first.brand then do;
      do m=1 to 3;
         /* your calculation logic goes here */
         month = (quarter - 1) * 3  + m;
         output;
      end;
   end;
   if not last.brand prev_qsales = sales; 
run;

 

 

Data never sleeps
Ksharp
Super User

That would be a lot of easy if you have SAS/ETS by using PROC EXPAND.

 

data have;
infile cards expandtabs truncover;
input Car_Brand	$ Year 	Sales_Q1	Sales_Q2	Sales_Q3	Sales_Q4;
cards;
Hyundai	2018 100	130	115	100
BMW	2018	100	130	115	100
Tesla	2018	120	150	135	120
Hyundai	2019	200	260	305	290
BMW	 2019	220	280	325	310
Hyundai	2020 300	600	570	600
BMW	2020	320	620	590	620
;
proc sort data=have out=temp;
by Car_Brand Year;
run;
proc transpose data=temp out=temp2;
by Car_Brand Year;
var Sales_:;
run;
data temp3;
 set temp2; 
 yyq=input(cats(year,scan(_name_,-1,'_')),yyq6.);
 format yyq yymmdd10.;
run;
data temp4;
 merge temp3 temp3(keep=Car_Brand yyq rename=(Car_Brand=_Car_Brand yyq=_yyq) firstobs=2);
 output;
 if Car_Brand=_Car_Brand then do;
   do i=1 to intck('month',yyq,_yyq)-1;
    yyq=intnx('month',yyq,1);
	call missing(col1);
	output;
   end;
 end;
 keep Car_Brand col1 yyq;
run;

proc expand data=temp4 out=want method=join;
by Car_Brand;
convert col1;
id yyq;
run;

Ksharp_0-1741746008402.png

 

MaxiHösi
Fluorite | Level 6

Thanks a lot. This code has already helped me a lot. It doesn't quite work, because not always a third is taken (I think that's due to the different days), but it was a good starting point. I don't have much time today, but maybe I can post the code on the weekend like I did. I still have some problems with retain and solved it very cumbersomely.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 3 replies
  • 522 views
  • 0 likes
  • 3 in conversation