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


I am trying to use sas code to create two extra columns on my dataset as follows:

Her is my sample dataset:

Veh NoPremstartend
15001/1/101/3/10
102/3/101/6/10
102/6/1030/9/10
101/10/1031/12/10
26001/1/091/3/09
27001/1/091/3/09
202/3/0920/9/09
2021/9/0930/9/09

I want to create three extra fields as follows:

prem1start1end1
5001/1/1031/12/10
5001/1/1031/12/10
5001/1/1031/12/10
5001/1/1031/12/10
6001/1/091/3/09
7001/1/0930/09/09
7001/1/0930/09/09
7001/1/0930/09/09

So for each vehicle number where a non-zero premium is followed by a certain number of records with a zero premium I want to retain the premium and start date fields for all the 0 premium records and retrofit the last end date of each sequence of (premium followed by 0 premium for each vehicle)

What I'm trying to do is allocate the premium across all the exposure records in proportion to the length of exposure

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Try this one :

Code: Program.sas

data have;
infile cards truncover expandtabs;
input VehNo Prem (start end) (: ddmmyy10.);
format start end ddmmyy10.;
cards;
1 500 1/1/10 1/3/10
1 0 2/3/10 1/6/10
1 0 2/6/10 30/9/10
1 0 1/10/10 31/12/10
2 600 1/1/09 1/3/09
2 700 1/1/09 1/3/09
2 0 2/3/09 20/9/09
2 0 21/9/09 30/9/09
;
data have;
set have;
by VehNo ;
if first.VehNo or Prem ne 0 then n+1;
run;
data temp;
set have;
by n;
if last.n;
keep n end;
run;
data want;
merge have temp(rename=(end=_end));
by n;
retain _Prem _start;
if first.n then do;_Prem =Prem ;_start=start;end;
format _start _end ddmmyy10.;
drop Prem n start end;
run;

Message was edited by: xia keshan

Message was edited by: xia keshan

View solution in original post

1 REPLY 1
Ksharp
Super User

Try this one :

Code: Program.sas

data have;
infile cards truncover expandtabs;
input VehNo Prem (start end) (: ddmmyy10.);
format start end ddmmyy10.;
cards;
1 500 1/1/10 1/3/10
1 0 2/3/10 1/6/10
1 0 2/6/10 30/9/10
1 0 1/10/10 31/12/10
2 600 1/1/09 1/3/09
2 700 1/1/09 1/3/09
2 0 2/3/09 20/9/09
2 0 21/9/09 30/9/09
;
data have;
set have;
by VehNo ;
if first.VehNo or Prem ne 0 then n+1;
run;
data temp;
set have;
by n;
if last.n;
keep n end;
run;
data want;
merge have temp(rename=(end=_end));
by n;
retain _Prem _start;
if first.n then do;_Prem =Prem ;_start=start;end;
format _start _end ddmmyy10.;
drop Prem n start end;
run;

Message was edited by: xia keshan

Message was edited by: xia keshan

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1 reply
  • 874 views
  • 0 likes
  • 2 in conversation