Solved
Contributor
Posts: 56

# time in days until a given date minus weekends

In a data set, each observation has a datetime variable "Trade_date_time", all before 22nd sept 2011.

I want to create a new variable called "time_left", which holds the number of days from each observation untill the 22nd sept 2011.

my code so far...

data my_data;

set my_data;

exp_date= '22-SEP-2011'd;

time_left = exp_date-date;

run;

but of course this is not correct, as it includes weekend days as well,

how can I not include weekend days in my time_left variable.

EDIT: Ok, so now I am using

data my_data;

set my_data;

exp_date= '22-SEP-2011'd;

weeks_left= week(exp_date)-week(date);

time_left = exp_date-date;

run;

which I think works.

Accepted Solutions
Solution
‎03-21-2012 12:59 PM
Posts: 3,167

## time in days until a given date minus weekends

INTCK will take care it automatically:

data my_data;

set my_data;

exp_date= '22-SEP-2011'd;

/*  weeks_left= week(exp_date)-week(date);*/

/*  time_left = exp_date-date;*/

run;

Regards,

Haikuo

All Replies
Super User
Posts: 23,752

## time in days until a given date minus weekends

You could do something like the following to double check but someone might have a more sleek answer using a custom interval and intck.

The following may also need to be adjusted by 1 depending on if you're including the boundary dates or not.

data my_data;

set my_data;

exp_date= '22-SEP-2011'd;

weeks_left= week(exp_date)-week(date);

time_left = exp_date-date;

time_left_check=0;

do i=date to exp_date;

if weekday(i) not in (1, 7) then time_left_check+1;

end;

run;

Regular Contributor
Posts: 184

## time in days until a given date minus weekends

`Reeza wrote:What about holidays?You could do something like the following to double check but someone might have a more sleek answer using a custom interval and intck. The following may also need to be adjusted by 1 depending on if you're including the boundary dates or not. data my_data;  set my_data;   date=datepart(Trade_date_time);  exp_date= '22-SEP-2011'd;  weeks_left= week(exp_date)-week(date);  time_left = exp_date-date;  time_left_adj = exp_date-date-(2*weeks_left);time_left_check=0;do i=date to exp_date;     if weekday(i) not in (1, 7) then time_left_check+1;end;run;`
Solution
‎03-21-2012 12:59 PM
Posts: 3,167

## time in days until a given date minus weekends

INTCK will take care it automatically:

data my_data;

set my_data;

exp_date= '22-SEP-2011'd;

/*  weeks_left= week(exp_date)-week(date);*/

/*  time_left = exp_date-date;*/

run;

Regards,

Haikuo

🔒 This topic is solved and locked.