I have some data that is monthly for most accts I need to be able to calculate a Fiscal YTD Amount. If every month I had a value for an account then my code works fine, but when there are gaps in the months, it doesn't work right.
This Code works fine when there is continuous monthly data:
data joe;
set work.bob;
by acct;
if first.acct then fiscalytd=0;
fiscalytd+amt;
if not first.acct and fiscal_start = 'Y' then fiscalytd=amt;
if not first.acct and fiscal_start ne 'Y' then fiscalytd=fiscalytd;
run;
But When the Data Looks like the table below it doesn't work right since i am using the FISCAL_START column to identify when the fiscal year starts
but in the sample below I don't always have a "APRIL" to restart the running total. (Note: The FiscalYTDAmt listed below are the correct values I want to have)
ACCT | AS_OF_DATE | MONTH | YEAR | FISCAL_START | AMT | FiscalYTDAmt |
ABC | 04/30/2011 | 4 | 2011 | Y | 5,000.00 | 5,000.00 |
ABC | 08/31/2011 | 8 | 2011 | (300.00) | 4,700.00 | |
ABC | 09/30/2011 | 9 | 2011 | (150.00) | 4,550.00 | |
ABC | 10/31/2011 | 10 | 2011 | 200.00 | 4,750.00 | |
ABC | 09/30/2012 | 9 | 2012 | (70.00) | (70.00) | |
ABC | 10/31/2012 | 10 | 2012 | 120.00 | 50.00 | |
ABC | 12/31/2012 | 12 | 2012 | (110.00) | (60.00) | |
ABC | 06/30/2013 | 6 | 2013 | (600.00) | (600.00) | |
ABC | 08/31/2013 | 8 | 2013 | (540.00) | (1,140.00) | |
ABC | 09/30/2013 | 9 | 2013 | 40.00 | (1,100.00) | |
ABC | 10/31/2013 | 10 | 2013 | (250.00) | (1,350.00) | |
ABC | 12/31/2013 | 12 | 2013 | (100.00) | (1,450.00) | |
ABC | 02/28/2014 | 2 | 2014 | (100.00) | (1,550.00) | |
ABC | 03/31/2014 | 3 | 2014 | (125.00) | (1,675.00) | |
ABC | 04/30/2014 | 4 | 2014 | Y | (300.00) | (300.00) |
ABC | 08/31/2014 | 8 | 2014 | (50.00) | (350.00) |
Why not use a custom date format that will display the fiscal year?
You can use the INTNX function to calculate fiscal year from AS_OF_DATE using 4 as the shift index YEAR.4.
Now that you have the FYEAR variable calculated you can use FIRST. processing to generate the year to date amount.
data want ;
set fiscal;
by acct fyear ;
if first.fyear then want=amt;
else want=sum(want,amt);
retain want ;
run;
Thanks Jaap, data_null_ and Tom for your help
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!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.