BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sanjaymane7
Obsidian | Level 7

Hello Sir,

I am a new user and need help in coding. I have a one record which I want to break financial year wise but should not go beyond the RED. I have given the desire output for your reference.

 

Input          
  TRAN_ID NET_PREMIUM NWP RSD RED
  ICM_NOV_22_807600                 22,937          8,257 19-Nov-22 18-Nov-25
           
Output          
  TRAN_ID NET_PREMIUM NWP RSD RED
  ICM_NOV_22_807600                 22,937          8,257 19-Nov-22 31-Mar-23
  ICM_NOV_22_807600                 22,937          8,257 01-Apr-23 31-Mar-24
  ICM_NOV_22_807600                 22,937          8,257 01-Apr-24 31-Mar-25
  ICM_NOV_22_807600                 22,937          8,257 01-Apr-25 18-Nov-25
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

First thing we have to do is convert your spreadsheet listing into actual SAS datsets so we have something we can program with.

 

data have;
 input TRAN_ID :$20. NET_PREMIUM :comma. NWP :comma. RSD :date. RED :date.;
 format RSD RED date9. NET_PREMIUM NWP comma.;
cards;
ICM_NOV_22_807600 22,937 8,257 19-Nov-22 18-Nov-25
;

data want; 
 input TRAN_ID :$20. NET_PREMIUM :comma. NWP :comma. RSD :date. RED :date.;
 format RSD RED date9. NET_PREMIUM NWP comma.;
cards;
ICM_NOV_22_807600 22,937 8,257 19-Nov-22 31-Mar-23
ICM_NOV_22_807600 22,937 8,257 01-Apr-23 31-Mar-24
ICM_NOV_22_807600 22,937 8,257 01-Apr-24 31-Mar-25
ICM_NOV_22_807600 22,937 8,257 01-Apr-25 18-Nov-25
;

So it looks like fiscal years start on first of April.  So that translates into the YEAR.4 interval.  We can use the INTCK() function find out how many year intervals are covered by your date range.  Then we can loop over that number and calculate the start/end dates of the fiscal year.  We just need to add code to make sure the first and last dates do not extend beyond the original range.

data test;
  set have;
  do offset=0 to intck('year.4',RSD,red);
     new_rsd = max(RSD,intnx('year.4',RSD,offset,'b'));
     new_red = min(RED,intnx('year.4',RSD,offset,'e'));
     format new_rsd new_red date9.;
     output;
  end;
  rename rsd=old_rsd new_rsd=RSD red=old_red new_red=RED ;
run;

Results:

Tom_0-1751733686967.png

 

 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Could you please provide a more detailed explanation?

 

In addition, please answer this:

How do we know the first record that you desire starts 19-Nov-22 (okay, I think I can figure that out) and ends 31-Mar-23 (I have no idea why 31-Mar-23 is what you want).

--
Paige Miller
sanjaymane7
Obsidian | Level 7
Hello sir, I want to start from RSD and end every line at the financial year as Our financial year is APR TO MAR. Hence, I want to end 31MAR
Tom
Super User Tom
Super User

First thing we have to do is convert your spreadsheet listing into actual SAS datsets so we have something we can program with.

 

data have;
 input TRAN_ID :$20. NET_PREMIUM :comma. NWP :comma. RSD :date. RED :date.;
 format RSD RED date9. NET_PREMIUM NWP comma.;
cards;
ICM_NOV_22_807600 22,937 8,257 19-Nov-22 18-Nov-25
;

data want; 
 input TRAN_ID :$20. NET_PREMIUM :comma. NWP :comma. RSD :date. RED :date.;
 format RSD RED date9. NET_PREMIUM NWP comma.;
cards;
ICM_NOV_22_807600 22,937 8,257 19-Nov-22 31-Mar-23
ICM_NOV_22_807600 22,937 8,257 01-Apr-23 31-Mar-24
ICM_NOV_22_807600 22,937 8,257 01-Apr-24 31-Mar-25
ICM_NOV_22_807600 22,937 8,257 01-Apr-25 18-Nov-25
;

So it looks like fiscal years start on first of April.  So that translates into the YEAR.4 interval.  We can use the INTCK() function find out how many year intervals are covered by your date range.  Then we can loop over that number and calculate the start/end dates of the fiscal year.  We just need to add code to make sure the first and last dates do not extend beyond the original range.

data test;
  set have;
  do offset=0 to intck('year.4',RSD,red);
     new_rsd = max(RSD,intnx('year.4',RSD,offset,'b'));
     new_red = min(RED,intnx('year.4',RSD,offset,'e'));
     format new_rsd new_red date9.;
     output;
  end;
  rename rsd=old_rsd new_rsd=RSD red=old_red new_red=RED ;
run;

Results:

Tom_0-1751733686967.png

 

 

Ksharp
Super User

Interesting question. I like it.

 

data have;
 input TRAN_ID :$20. NET_PREMIUM :comma. NWP :comma. RSD :date. RED :date.;
 format RSD RED date9. NET_PREMIUM NWP comma.;
cards;
ICM_NOV_22_807600 22,937 8,257 19-Nov-22 18-Nov-25
;

data want;
 set have;
 new_rsd=RSD;
 do date=RSD to RED;
  if (month(date)=4 and day(date)=1) or date=RED then do;
    new_red=ifn(date=RED,date,date-1);output;new_rsd=date;
  end;
 end;
 drop date RSD RED;
 format new_rsd new_red date9.;
run;

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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