DATA Step, Macro, Functions and more

Create flag for policy renewal within 365 days

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Create flag for policy renewal within 365 days

[ Edited ]


The data looks like as below
I need a new flag like this as Renewal

My logic is

Policy_no =lag(policy_no) and start_date=lag(start_date)>=365 then Renewal='R1'

But my question if he renews it again in the next year it should be R2, and so on for huge data

So I need my Renewal flag like this ...as below

Policy_no. Start_date. Renewal
10100. 03Jan2014. N
10100. 07Mar2015. R1
10100. 10Sep2016. R2
21001. 15Mar2014. N
21001. 15Mar2014. N
21001. 20Oct2015. R1

I need a renewal like this and my logic is like above. Please help me on this asap.

If need further information please reply me.


Accepted Solutions
Solution
‎10-15-2016 01:02 PM
Super User
Posts: 19,862

Re: Base Programming

[ Edited ]

You're on the right path. 

 

Rather than use lag to check for the same policy number use BY group processing. 

LAG doesn't operate conditionally so calculate the value ahead of time and store that in a variable to use in your calculation. 

 

What happens if renewal is <365 days? That case isn't covered in your logic. 

 

Heres a sample(untested) that should get you started. 

 

By policy_no;

 

Retain index;

 

prev_start=lag(start_date;

 

if first.policy_no then do;

    Prev_start=.;

    Index=0;

    Renewal = 'N';

end;

Else if stsrt_date - prev_start >= 365 then do;

Renewal =catt('R', index);

end;

else <logic?>;

index+1;

 

 

View solution in original post


All Replies
Occasional Contributor
Posts: 12

Re: Base Programming

Sorry in he logic it should be
Policy_no =lag(policy_no) and
start_date-lag(start_date)>=365 then Renewal='R1'

It's minus symbol after start_date
Occasional Contributor
Posts: 12

Base Programming

The data looks like as below
I need a new flag like this as Renewal

My logic is

Policy_no =lag(policy_no) and
start_date-lag(start_date)>=365 then Renewal='R1'

But my question if he renews it again in the next year it should be R2, and so on for huge data

So I need my Renewal flag like this ...as below

Policy_no. Start_date. Renewal
10100. 03Jan2014. N
10100. 07Mar2015. R1
10100. 10Sep2016. R2
21001. 15Mar2014. N
21001. 15Mar2014. N
21001. 20Oct2015. R1

I need a renewal like this and my logic is like above. Please help me on this asap.

If need further information please reply me.
Respected Advisor
Posts: 4,932

Re: Base Programming

Use a do until() and logic

 

data want;
do until(last.policy_no);
    set have; by policy_no;
    renewal = "N  ";
    if missing(last_date) then last_date = start_date;
    else if intck("DAY", last_date, start_date) >= 365 then do;
        r = sum(r, 1);
        renewal = cats("R", r);
        last_date = start_date;
        end;
    output;
    end;
drop last_date r;
run;
PG
Solution
‎10-15-2016 01:02 PM
Super User
Posts: 19,862

Re: Base Programming

[ Edited ]

You're on the right path. 

 

Rather than use lag to check for the same policy number use BY group processing. 

LAG doesn't operate conditionally so calculate the value ahead of time and store that in a variable to use in your calculation. 

 

What happens if renewal is <365 days? That case isn't covered in your logic. 

 

Heres a sample(untested) that should get you started. 

 

By policy_no;

 

Retain index;

 

prev_start=lag(start_date;

 

if first.policy_no then do;

    Prev_start=.;

    Index=0;

    Renewal = 'N';

end;

Else if stsrt_date - prev_start >= 365 then do;

Renewal =catt('R', index);

end;

else <logic?>;

index+1;

 

 

Super User
Posts: 10,044

Re: Create flag for policy renewal within 365 days


data have;
infile cards dlm='. ';
input Policy_no Start_date : date9. Renewal $;
format start_date date9.;
cards;
10100. 03Jan2014. N
10100. 07Mar2015. R1
10100. 10Sep2016. R2
21001. 15Mar2014. N
21001. 15Mar2014. N
21001. 20Oct2015. R1
;
run;
data want;
 set have;
 by policy_no;
 length new $ 20;
 new='N';
 if first.policy_no then count=0;
 if dif(start_date) ge 365 and not first.policy_no then do;
  count+1;new=cats('R',count);
 end;
 drop count;
run;

Occasional Contributor
Posts: 12

Re: Create flag for policy renewal within 365 days

Thanks a lot ksharp...
Occasional Contributor
Posts: 12

Re: Create flag for policy renewal within 365 days

Thank you Guru
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 470 views
  • 3 likes
  • 4 in conversation