BookmarkSubscribeRSS Feed
jimbobob
Quartz | Level 8

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)
4 REPLIES 4
jakarman
Barite | Level 11

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

---->-- ja karman --<-----
data_null__
Jade | Level 19

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
Tom
Super User Tom
Super User

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;

jimbobob
Quartz | Level 8

Thanks Jaap, data_null_ and Tom for your help Smiley Happy

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1777 views
  • 1 like
  • 4 in conversation