<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to loop a calculation for multiple months using proc sql in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-loop-a-calculation-for-multiple-months-using-proc-sql/m-p/817236#M34483</link>
    <description>&lt;P&gt;Hello, I am trying to loop a proc sql calculation for multiple months.&lt;/P&gt;&lt;P&gt;I want the loop to calculate from 2021 Dec to 2022 May.&lt;/P&gt;&lt;P&gt;The months are in one table but separate columns&lt;/P&gt;&lt;P&gt;e.g final_collection_202112, final_collections_202205&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see script below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _NULL_;&lt;/P&gt;&lt;P&gt;PreviousMonths = substr(left(input(put(intnx('MONTH',today(),-6,'END'),yymmddn8.),$8.)),1,6);&lt;BR /&gt;call symputx('PreviousMonths',PreviousMonths);&lt;/P&gt;&lt;P&gt;LastMonth = substr(left(input(put(intnx('MONTH',today(),-2,'END'),yymmddn8.),$8.)),1,6);&lt;BR /&gt;call symputx(' LastMonth', LastMonth);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%put &amp;amp;LastMonth;&lt;BR /&gt;%put &amp;amp;PreviousMonths;&lt;/P&gt;&lt;P&gt;data _NULL_;&lt;BR /&gt;monthdiff = intck('MONTH',&amp;amp;PreviousMonths,&amp;amp;LastMonth);&lt;BR /&gt;call symputx('monthdiff',monthdiff);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%put &amp;amp;monthdiff;&lt;BR /&gt;data _NULL_;&lt;/P&gt;&lt;P&gt;TM = substr(left(input(put(intnx('MONTH',today(),-1,'END'),yymmddn8.),$8.)),1,6);&lt;BR /&gt;call symputx('TM',TM);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%put TM : &amp;amp;TM;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%macro Tbl(sub);&lt;/P&gt;&lt;P&gt;libname Tr "C:\Users\siban004\OneDrive - Vodafone Group\Documents\Tranches2\&amp;amp;sub";&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table Collect_&amp;amp;sub as select distinct&lt;BR /&gt;Tranche,&lt;BR /&gt;VAT&lt;BR /&gt;RunMonth as Current_Month,&lt;BR /&gt;&amp;amp;dt as Run_Month,&lt;BR /&gt;sum(Final_collection_&amp;amp;dt) as Collections,&lt;BR /&gt;sum(Final_collection_&amp;amp;dt)* VAT as VAT,&lt;BR /&gt;(sum(Final_Collection_&amp;amp;dt) + (sum(Final_Collection_&amp;amp;dt)* VAT))* 0.15/100 as Interest,&lt;BR /&gt;sum(Final_Collection_&amp;amp;dt) + ((sum(Final_Collection_&amp;amp;dt) )* VAT) +&lt;BR /&gt;(sum(Final_Collection_&amp;amp;dt) + (sum(Final_Collection_&amp;amp;dt)* VAT))* 0.15/100 as Final_Payment,&lt;BR /&gt;sum(Final_Collection_&amp;amp;dt) + ((sum(Final_Collection_&amp;amp;dt) )* VAT) as Final_Payment_excl_Interest&lt;/P&gt;&lt;P&gt;from Tr.Final_collections_&amp;amp;TM&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;</description>
    <pubDate>Thu, 09 Jun 2022 07:27:17 GMT</pubDate>
    <dc:creator>Nqobile_S14</dc:creator>
    <dc:date>2022-06-09T07:27:17Z</dc:date>
    <item>
      <title>How to loop a calculation for multiple months using proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-loop-a-calculation-for-multiple-months-using-proc-sql/m-p/817236#M34483</link>
      <description>&lt;P&gt;Hello, I am trying to loop a proc sql calculation for multiple months.&lt;/P&gt;&lt;P&gt;I want the loop to calculate from 2021 Dec to 2022 May.&lt;/P&gt;&lt;P&gt;The months are in one table but separate columns&lt;/P&gt;&lt;P&gt;e.g final_collection_202112, final_collections_202205&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see script below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _NULL_;&lt;/P&gt;&lt;P&gt;PreviousMonths = substr(left(input(put(intnx('MONTH',today(),-6,'END'),yymmddn8.),$8.)),1,6);&lt;BR /&gt;call symputx('PreviousMonths',PreviousMonths);&lt;/P&gt;&lt;P&gt;LastMonth = substr(left(input(put(intnx('MONTH',today(),-2,'END'),yymmddn8.),$8.)),1,6);&lt;BR /&gt;call symputx(' LastMonth', LastMonth);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%put &amp;amp;LastMonth;&lt;BR /&gt;%put &amp;amp;PreviousMonths;&lt;/P&gt;&lt;P&gt;data _NULL_;&lt;BR /&gt;monthdiff = intck('MONTH',&amp;amp;PreviousMonths,&amp;amp;LastMonth);&lt;BR /&gt;call symputx('monthdiff',monthdiff);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%put &amp;amp;monthdiff;&lt;BR /&gt;data _NULL_;&lt;/P&gt;&lt;P&gt;TM = substr(left(input(put(intnx('MONTH',today(),-1,'END'),yymmddn8.),$8.)),1,6);&lt;BR /&gt;call symputx('TM',TM);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%put TM : &amp;amp;TM;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%macro Tbl(sub);&lt;/P&gt;&lt;P&gt;libname Tr "C:\Users\siban004\OneDrive - Vodafone Group\Documents\Tranches2\&amp;amp;sub";&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table Collect_&amp;amp;sub as select distinct&lt;BR /&gt;Tranche,&lt;BR /&gt;VAT&lt;BR /&gt;RunMonth as Current_Month,&lt;BR /&gt;&amp;amp;dt as Run_Month,&lt;BR /&gt;sum(Final_collection_&amp;amp;dt) as Collections,&lt;BR /&gt;sum(Final_collection_&amp;amp;dt)* VAT as VAT,&lt;BR /&gt;(sum(Final_Collection_&amp;amp;dt) + (sum(Final_Collection_&amp;amp;dt)* VAT))* 0.15/100 as Interest,&lt;BR /&gt;sum(Final_Collection_&amp;amp;dt) + ((sum(Final_Collection_&amp;amp;dt) )* VAT) +&lt;BR /&gt;(sum(Final_Collection_&amp;amp;dt) + (sum(Final_Collection_&amp;amp;dt)* VAT))* 0.15/100 as Final_Payment,&lt;BR /&gt;sum(Final_Collection_&amp;amp;dt) + ((sum(Final_Collection_&amp;amp;dt) )* VAT) as Final_Payment_excl_Interest&lt;/P&gt;&lt;P&gt;from Tr.Final_collections_&amp;amp;TM&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2022 07:27:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-loop-a-calculation-for-multiple-months-using-proc-sql/m-p/817236#M34483</guid>
      <dc:creator>Nqobile_S14</dc:creator>
      <dc:date>2022-06-09T07:27:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to loop a calculation for multiple months using proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-loop-a-calculation-for-multiple-months-using-proc-sql/m-p/817239#M34484</link>
      <description>&lt;P&gt;First of all, you overcomplicate things by a mile, causing non-working code.&lt;/P&gt;
&lt;P&gt;Since your macro variables&amp;nbsp;&lt;SPAN&gt;PreviousMonths and&amp;nbsp;LastMonth contain strings with year and month in YYYYMM format, this calculation will not work as intended:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;monthdiff = intck('MONTH',&amp;amp;PreviousMonths,&amp;amp;LastMonth);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;as it calculates the month difference of a date 202112 days after 1960-01-01 and a date 202204 days after 1960-01-01.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I suggest you first create a list of datasets to process, then create a view from that list where you also keep the month from the dataset name as a new variable, and then run your SQL query with a GROUP BY that incorporates the new variable:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
length datasets $500;
do i = -6 to -4;
  mth = put(intnx('month',today(),i),yymmn6.);
  datasets = catx(" ",datasets,"tr.final_collections_"!!mth);
end;
call symputx("datasets",datasets);
run;

data fc_all / view=fc_all;
length dsname $41 month $6;
set &amp;amp;datasets. indsname=dsname;
month = scan(dsname,-1,"_");
run;

proc sql;
create table Collect_&amp;amp;sub as select
  month,
  Tranche,
  VAT,
  &amp;amp;dt as Run_Month,
  RunMonth as Current_Month,
  sum(Final_collection_&amp;amp;dt) as Collections,
  sum(Final_collection_&amp;amp;dt)* VAT as VAT,
  (sum(Final_Collection_&amp;amp;dt) + (sum(Final_Collection_&amp;amp;dt)* VAT))* 0.15/100 as Interest,
  sum(Final_Collection_&amp;amp;dt) + ((sum(Final_Collection_&amp;amp;dt) )* VAT) +
  (sum(Final_Collection_&amp;amp;dt) + (sum(Final_Collection_&amp;amp;dt)* VAT))* 0.15/100 as Final_Payment,
  sum(Final_Collection_&amp;amp;dt) + ((sum(Final_Collection_&amp;amp;dt) )* VAT) as Final_Payment_excl_Interest
  from fc_all
  group by month, Tranche, VAT, calculated Run_month, calculated Current_month
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2022 08:32:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-loop-a-calculation-for-multiple-months-using-proc-sql/m-p/817239#M34484</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-09T08:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to loop a calculation for multiple months using proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-loop-a-calculation-for-multiple-months-using-proc-sql/m-p/817245#M34485</link>
      <description>Thank you so much for the help, Let me try the solution. I appreciate it.</description>
      <pubDate>Thu, 09 Jun 2022 08:59:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-loop-a-calculation-for-multiple-months-using-proc-sql/m-p/817245#M34485</guid>
      <dc:creator>Nqobile_S14</dc:creator>
      <dc:date>2022-06-09T08:59:47Z</dc:date>
    </item>
  </channel>
</rss>

