Help using Base SAS procedures

How to Create a Fiscal YTD Column

Reply
Occasional Contributor
Posts: 7

How to Create a Fiscal YTD Column

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)

ACCTAS_OF_DATEMONTHYEARFISCAL_START AMT FiscalYTDAmt
ABC04/30/201142011Y    5,000.00            5,000.00
ABC08/31/201182011      (300.00)           4,700.00
ABC09/30/201192011      (150.00)           4,550.00
ABC10/31/2011102011        200.00            4,750.00
ABC09/30/201292012        (70.00)               (70.00)
ABC10/31/2012102012        120.00                  50.00
ABC12/31/2012122012      (110.00)               (60.00)
ABC06/30/201362013      (600.00)            (600.00)
ABC08/31/201382013      (540.00)         (1,140.00)
ABC09/30/201392013           40.00          (1,100.00)
ABC10/31/2013102013      (250.00)         (1,350.00)
ABC12/31/2013122013      (100.00)         (1,450.00)
ABC02/28/201422014      (100.00)         (1,550.00)
ABC03/31/201432014      (125.00)         (1,675.00)
ABC04/30/201442014Y     (300.00)            (300.00)
ABC08/31/201482014        (50.00)            (350.00)
Trusted Advisor
Posts: 3,214

Re: How to Create a Fiscal YTD Column

Why not use a custom date format that will display the fiscal year?

---->-- ja karman --<-----
Respected Advisor
Posts: 3,799

Re: How to Create a Fiscal YTD Column

You can use the INTNX function to calculate fiscal year from AS_OF_DATE using 4 as the shift index YEAR.4.

data fiscal;
   infile cards dsd dlm='|' firstobs=2;
  
input acct:$3. date:mmddyy. month year fystart:$1.;
  
format date mmddyy.;
   fyear = intnx(
'YEAR.4',date,0);
   format fyear year.;
  
cards;
ACCT|AS_OF_DATE|MONTH|YEAR|FISCAL_START|AMT|FiscalYTDAmt
ABC|4/30/2011|4|2011|Y|5,000.00|5,000.00
ABC|8/31/2011|8|2011||(300.00)|4,700.00
ABC|9/30/2011|9|2011||(150.00)|4,550.00
ABC|10/31/2011|10|2011||200.00|4,750.00
ABC|9/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|6/30/2013|6|2013||(600.00)|(600.00)
ABC|8/31/2013|8|2013||(540.00)|(1,140.00)
ABC|9/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|2/28/2014|2|2014||(100.00)|(1,550.00)
ABC|3/31/2014|3|2014||(125.00)|(1,675.00)
ABC|4/30/2014|4|2014|Y|(300.00)|(300.00)
ABC|8/31/2014|8|2014||(50.00)|(350.00)
;;;;
   run;
proc print;
  
run;

9-6-2014 4-54-04 AM.png
Super User
Super User
Posts: 7,062

Re: How to Create a Fiscal YTD Column

Posted in reply to data_null__

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;

Occasional Contributor
Posts: 7

Re: How to Create a Fiscal YTD Column

Thanks Jaap, data_null_ and Tom for your help Smiley Happy

Ask a Question
Discussion stats
  • 4 replies
  • 291 views
  • 0 likes
  • 4 in conversation