BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
krzy32
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

krzy32
Fluorite | Level 6

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Reeza
Super User

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=...?

 

krzy32
Fluorite | Level 6

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5 replies
  • 1324 views
  • 3 likes
  • 3 in conversation