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


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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

7 REPLIES 7
SBRVamsi
Fluorite | Level 6
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
SBRVamsi
Fluorite | Level 6
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.
PGStats
Opal | Level 21

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
Reeza
Super User

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;

 

 

Ksharp
Super User

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;

SBRVamsi
Fluorite | Level 6
Thanks a lot ksharp...
SBRVamsi
Fluorite | Level 6
Thank you Guru

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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