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

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;

   date=datepart(Trade_date_time);

  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;

   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);

run;

which I think works.

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

INTCK will take care it automatically:

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 = intck('weekday',date,exp_date);

run;

Regards,

Haikuo

View solution in original post

3 REPLIES 3
Reeza
Super User

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;

Howles
Quartz | Level 8

See http://www.sascommunity.org/wiki/Generating_Holiday_Lists

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;

Haikuo
Onyx | Level 15

INTCK will take care it automatically:

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 = intck('weekday',date,exp_date);

run;

Regards,

Haikuo

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
  • 3 replies
  • 727 views
  • 3 likes
  • 4 in conversation