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

Hi, i need help in extracting latest lapse_score and adding another variable into my data and calculate age in months of that latest lapse_score(i.e difference between extracted latest lapse_score and previous lapse score) .If the are three different(date_received) for one policy, i just need the difference between latest two dates. And if previous lapse score is null, then return 0 as difference in months. dot represent null received date

 

Data Have

               itc_data table

policy_no     date_received   lapse_score 

1                  01Jan2018        30

1                  01Apr2018        70

1                  01Jun2018        20

2                  01May2019       60

2                  01Oct2019        80

 

 

Data Want

policy_no       date_received   lapse_score    Age(Months)

1                    01Jun2018          20                   2

2                    01Oct2019          80                   5

             

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Take care of that by checking for first.:

data want;
set have;
by policy_no;
age = intck('month',lag(date_received),date_received);
if last.policy_no;
if first.policy_no then age = 0;
run;

 

View solution in original post

9 REPLIES 9
Solly7
Pyrite | Level 9

Hi, i need help in extracting latest lapse_score and adding another variable into my data and calculate age in months of that latest lapse_score(i.e difference between extracted latest lapse_score and previous lapse score) .If the are three different(date_received) for one policy, i just need the difference between latest two dates.

 

Data Have

               itc_data table

policy_no     date_received   lapse_score 

1                  01Jan2018        30

1                  01Apr2018        70

1                  01Jun2018        20

2                  01May2019       60

2                  01Oct2019        80

 

 

Data Want

policy_no       date_received   lapse_score    Age(Months)

1                    01Jun2018          20                   2

2                    01Oct2019          80                   5

             

Solly7
Pyrite | Level 9

Apologies for double-posting

Solly7
Pyrite | Level 9

Hi your solutions works but i still have a problem with policy numbers which consist of only 1 row(date_received). I need those particular rows to return 0 as Age since they do not have matching previous row. see example below.

 

Data Have

               itc_data table

policy_no     date_received   lapse_score 

3                 01Jan2020        30

3                 01Apr2020        70

4                 01Jun2018        20

               

 

 

Data Want

policy_no       date_received   lapse_score    Age(Months)

3                    01Apr2020          70                   2

4                   01Jun2018          20                    0

novinosrin
Tourmaline | Level 20
data want;
 do until(last.policy_no);
  set have;
  by policy_no;
  if first.policy_no and last.policy_no then _n_=date_received;
  age=intck('mon',_n_,date_received);
  _n_=date_received;
 end;
run;
Solly7
Pyrite | Level 9
Hi your solutions works but i still have a problem with policy numbers which consist of only 1 row(date_received). I need those particular rows to return 0 as Age since they do not have matching previous row. see example below.



Data Have

itc_data table

policy_no date_received lapse_score

3 01Jan2020 30

3 01Apr2020 70

4 01Jun2018 20







Data Want

policy_no date_received lapse_score Age(Months)

3 01Apr2020 70 2

4 01Jun2018 20 0
Kurt_Bremser
Super User

Take care of that by checking for first.:

data want;
set have;
by policy_no;
age = intck('month',lag(date_received),date_received);
if last.policy_no;
if first.policy_no then age = 0;
run;

 

Solly7
Pyrite | Level 9

Thank you very much..it works!

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
  • 9 replies
  • 2111 views
  • 2 likes
  • 3 in conversation