Hello, I am trying to loop a proc sql calculation for multiple months.
I want the loop to calculate from 2021 Dec to 2022 May.
The months are in one table but separate columns
e.g final_collection_202112, final_collections_202205
The calculation adds the collections for the individual months, calculates VAT and interest and then produces a final payment which is a sum of collections, VAT and interest.
Please see script below.
data _NULL_;
PreviousMonths = substr(left(input(put(intnx('MONTH',today(),-6,'END'),yymmddn8.),$8.)),1,6);
call symputx('PreviousMonths',PreviousMonths);
LastMonth = substr(left(input(put(intnx('MONTH',today(),-2,'END'),yymmddn8.),$8.)),1,6);
call symputx(' LastMonth', LastMonth);
run;
%put &LastMonth;
%put &PreviousMonths;
data _NULL_;
monthdiff = intck('MONTH',&PreviousMonths,&LastMonth);
call symputx('monthdiff',monthdiff);
run;
%put &monthdiff;
data _NULL_;
TM = substr(left(input(put(intnx('MONTH',today(),-1,'END'),yymmddn8.),$8.)),1,6);
call symputx('TM',TM);
run;
%put TM : &TM;
%macro Tbl(sub);
libname Tr "C:\Users\siban004\OneDrive - Vodafone Group\Documents\Tranches2\&sub";
proc sql;
create table Collect_&sub as select distinct
Tranche,
VAT
RunMonth as Current_Month,
&dt as Run_Month,
sum(Final_collection_&dt) as Collections,
sum(Final_collection_&dt)* VAT as VAT,
(sum(Final_Collection_&dt) + (sum(Final_Collection_&dt)* VAT))* 0.15/100 as Interest,
sum(Final_Collection_&dt) + ((sum(Final_Collection_&dt) )* VAT) +
(sum(Final_Collection_&dt) + (sum(Final_Collection_&dt)* VAT))* 0.15/100 as Final_Payment,
sum(Final_Collection_&dt) + ((sum(Final_Collection_&dt) )* VAT) as Final_Payment_excl_Interest
from Tr.Final_collections_&TM
quit;