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.
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;
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;
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;
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.