## Calculating Indian financial quarter from given date using user-defined function

Solved
Occasional Contributor
Posts: 11

# Calculating Indian financial quarter from given date using user-defined function

I wanted to calculate the Indian financial quarter which starts in April (April-Jun would be 1st Qtr). I created a user defined function using the proc fcmp procedure yet it is not giving me the correct answer. What is wrong with my logic?

proc fcmp outlib=three.level.name;
function FinQuart(dat);
if month(dat) in(4,5,6) then qurt = 1;
if month(dat) in(7,8,9) then qurt = 2;
if month(dat) in(10,11,12) then qurt = 3;
if month(dat) in(1,2,3) then qurt = 4;
return (qurt);
endsub;
run;

The function returns 4 whatever value you submit.

Accepted Solutions
Solution
‎01-10-2017 12:24 PM
Super User
Posts: 9,599

## Re: Calculating Indian financial quarter from given date using user-defined function

Well, that example is incorrect.  You are taking the month number from 12DEC09, and putting that into the variable TT.  Then you call you function with this variable, hence you are calling as:

tempvar=finquart(12);

Then in your function, you are using the month() function which expects a date (which is a number of days since a certain date), and are passing it 12, which SAS takes as a date number (formatted would be 13JAN1960), and Jan which is month 1 comes out as QTR=4.  So correct to this:

```options cmplib=twolevel.name;
data temp;
tt = month('12DEC09'd);
put tt;
actual_date_variable='12DEC09'd;
tempvar = FinQuart(actual_date_value);
run;```

All Replies
Super User
Posts: 9,599

## Re: Calculating Indian financial quarter from given date using user-defined function

Please post test data in the form of a datastep.  Personally I would write a datastep to do this, you can then easily debug it and save it for later or put it in a macro if needed.  Compiled functions whilst a nice gimmick, are hard to use, proprietary, and obfuscated.

Occasional Contributor
Posts: 11

## Re: Calculating Indian financial quarter from given date using user-defined function

I was testing the function in a datastep itself.

options cmplib=twolevel.name;
data temp;
tt = month('12DEC09'd);
put tt;
tempvar = FinQuart(tt);
run;

output

tt    tempvar

12     4

Solution
‎01-10-2017 12:24 PM
Super User
Posts: 9,599

## Re: Calculating Indian financial quarter from given date using user-defined function

Well, that example is incorrect.  You are taking the month number from 12DEC09, and putting that into the variable TT.  Then you call you function with this variable, hence you are calling as:

tempvar=finquart(12);

Then in your function, you are using the month() function which expects a date (which is a number of days since a certain date), and are passing it 12, which SAS takes as a date number (formatted would be 13JAN1960), and Jan which is month 1 comes out as QTR=4.  So correct to this:

```options cmplib=twolevel.name;
data temp;
tt = month('12DEC09'd);
put tt;
actual_date_variable='12DEC09'd;
tempvar = FinQuart(actual_date_value);
run;```
Super User
Posts: 23,662

## Re: Calculating Indian financial quarter from given date using user-defined function

A custom function is good, but you can also use a nested function.

Quarter = qtr(intnx('month', date, -3, 'b'));

Have you set the cmplib value?

Options cmplib=...?

Occasional Contributor
Posts: 11

## Re: Calculating Indian financial quarter from given date using user-defined function

I can't believe I made such a silly mistake. Thanks a lot. Yes, I have specified the cmplib option. I am new to SAS so I had no idea about nested functions. Thanks again.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 5 replies
• 387 views
• 2 likes
• 3 in conversation