DATA Step, Macro, Functions and more

Date calculation

Reply
Super Contributor
Posts: 625

Date calculation

I've two variables called Reporting_dt and premium_begin_date. If the year of the premium_begin_date is less than the Reporting_dt year then the begin date is the 1st of January of the reporting year else keep the premium_begin_date.

 

e.g. if Reporting_dt = ‘31/08/2013’ and the premium_begin_date is 01/11/2012 then the begin date = ‘01/01/2013’.

e.g. if Reporting_dt = ‘31/08/2013’ and the premium_begin_date is 01/04/2013 then the begin date = ‘01/04/2013’.

 

Appreciate if someone help me to accomplish this.

PROC Star
Posts: 1,283

Re: Date calculation

do like this

 

data have;
input (Reporting_dt premium_begin_date)(:ddmmyy10.);
format Reporting_dt premium_begin_date ddmmyy10.;
datalines;
31/08/2013 01/11/2012
31/08/2013 01/04/2013
;

data want;
	set have;
	begin_date=premium_begin_date;
	if year(Reporting_dt) > year(premium_begin_date) then begin_date=mdy(1, 1, year(Reporting_dt));
	format begin_date ddmmyy10.;
run;
Super User
Super User
Posts: 9,599

Re: Date calculation

Classic case for using ifn() function here on a binary result:

data want;
  set have;
  begin_date=ifn(year(Reporting_dt) > year(premium_begin_date),mdy(1,1,year(Reporting_dt)),premium_begin_date);
  format begin_date ddmmyy10.;
run;

 

Super User
Posts: 13,523

Re: Date calculation


Babloo wrote:

I've two variables called Reporting_dt and premium_begin_date. If the year of the premium_begin_date is less than the Reporting_dt year then the begin date is the 1st of January of the reporting year else keep the premium_begin_date.

 

e.g. if Reporting_dt = ‘31/08/2013’ and the premium_begin_date is 01/11/2012 then the begin date = ‘01/01/2013’.

e.g. if Reporting_dt = ‘31/08/2013’ and the premium_begin_date is 01/04/2013 then the begin date = ‘01/04/2013’.

 

Appreciate if someone help me to accomplish this.


If you want to use a hard coded date in code the form is 'ddMONyy'd or 'ddMONyyyy'd. Examples: '23Feb98'd or '16May2017'd.

the D after quote tells SAS to attempt to use the value inside the quotes as a Date. The specific format is because there are so many ways to display dates and several of them are not readily identifiable in context such as "010203" which could be 02Jan2003, 01Feb2003, or 03Feb2001.

 

You may find the PDF document on here: https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... helpful for date use.

Ask a Question
Discussion stats
  • 3 replies
  • 166 views
  • 6 likes
  • 4 in conversation