BookmarkSubscribeRSS Feed
viofra
Calcite | Level 5

Dear SAS users,

 

I'm writing a code and struggling how to write a recursive code in a proper way.

Problem:

I have to calculate exposures in days in the last one year (0-1) period and in the last 1-7 years period from the contract start date.  Information is calculated per each contract.

 

Example:

 

 

Contract dates

0-1 period dates

1-7 period dates

 

 

HHLD

Start_date

End_date

from1

to1

from7

to7

Exposure_1

Exposure_7

Maria

04.06.2018

04.12.2018

04.06.2017

03.06.2018

04.06.2011

03.06.2017

57

487

Maria

01.02.2017

01.08.2017

01.02.2016

31.01.2017

01.02.2010

31.01.2016

0

365

Maria

02.02.2012

02.02.2013

02.02.2011

01.02.2012

02.02.2005

01.02.2011

0

0

Oskar

01.01.2020

31.12.2020

01.01.2019

01.01.2020

01.01.2013

01.01.2019

51

401

Oskar

03.05.2016

02.08.2016

03.05.2015

03.05.2016

03.05.2009

03.05.2015

0

0

Oskar

23.02.2018

22.02.2019

23.02.2017

23.02.2018

23.02.2011

23.02.2017

0

90

 

Maria and Oskar has 3 policies and I want to fill in columns „Exposure_1“ and „Exposure_7“ based on previous contract dates.

 

Maria has a contract that started on 04.06.2018 which means that 1 year prior she had 57 days of contracts (01.08.2017-04.06.2017-1=57) and 1-7 years ago she had 487 days of contracts (02.02.2013 - 02.02.2012 = 365 and 04.06.17-01.02.17=122 (we have to eliminate first year); 365+122 = 487).

 

I would be grateful if you could help me with this problem.

4 REPLIES 4
andreas_lds
Jade | Level 19

Please post the data in usable form.

I don't understand: "Maria has a contract that started on 04.06.2018 which means that 1 year prior she had 57 days of contracts (01.08.2017-04.06.2017-1=57)" - why 01.08.2017?

viofra
Calcite | Level 5

Hi, thank you for the respond and sorry for incorrect formats, attaching excel file with right dates formats, hope it is better now.
For 0-1 years I am checking how many days in that period a person had contracts.

Maria has a contract that started on 04.06.2018, that means that we check period from 2017.06.04 to 2018.06.03. During this period she had the contract from second row, but we count only days in wanted period. That's the reason we count days from 2017.06.04 to 2017.08.01 (the contract ends here).

andreas_lds
Jade | Level 19

Sorry, but an excel-file is light-years away from "data in usable form". Post the data as data-step using datalines.

ballardw
Super User

The only way SAS would have what you show as "data" is if every value is character.

And if you "dates" are not SAS date values then that is the first step, make them so.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against.

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

 

And what does recursion have to do with this?

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 548 views
  • 0 likes
  • 3 in conversation