SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
jjjch
Obsidian | Level 7

Every year, there is a day that has 23 hours, and a day has 25 hours.

 

So I am expecting to get the results like below:

 

hour=24 hourshortday=23 hourlongday=25

 

But actually, all hours are 24:

 

hour=24 hourshortday=24 hourlongday=24

 

How can I get the results I expected?

 

data test;
hour=intck('hour','28MAR2019:00:00:00'dt,'29MAR2019:00:00:00'dt);
hourshortday=intck('hour','10MAR2019:00:00:00'dt,'11MAR2019:00:00:00'dt);
hourlongday=intck('hour','4NOV2018:00:00:00'dt,'5NOV2018:00:00:00'dt);
put (hour hourshortday hourlongday) (=);
run;

 

4 REPLIES 4
ballardw
Super User

@jjjch wrote:

Every year, there is a day that has 23 hours, and a day has 25 hours.

In what universe is your planet Earth?

 

You may have a data system that has some notion that abuses date/time values. I have seen that working with data loggers that had hours 0 through 24 for every day (25 time points).

 

I suspect that you will have to provide examples from your actual data file that exhibits the behavior you show, including the dates with differing numbers of hours.

 

SAS uses the rules for leap years where accumulated differences are applied by adding (or not) a leap day for date calculations and does not try to keep track of actual daily sidereal time.

jjjch
Obsidian | Level 7

Sorry, I should be more specific: I am living in Texas and we (and many other states in the US) have daylight saving time.

jjjch
Obsidian | Level 7

ballardw, thank you for your help. I ended up with writing a function like below.

/*define the function*/
libname myfunc "...";
proc fcmp outlib=myfunc.util.datetime;
	function hoursInADay(myDate);
		if missing(myDate) then return(.);
		jg_t_year = year(myDate);
		jg_t_shortday = nwkdom(2, 1, 3, jg_t_year);
		jg_t_longday = nwkdom(1, 1, 11, jg_t_year);
		if myDate=jg_t_shortday then jg_num_of_hours=23;
		else if myDate=jg_t_longday then jg_num_of_hours=25;
		else jg_num_of_hours=24;
		return(jg_num_of_hours);
	endsub;
run;


/*test the function*/
libname myfunc "...";
options cmplib=myfunc.util;

data _NULL_;
	array myDates{3} _temporary_ ('28MAR2019'd, '10MAR2019'd, '4NOV2018'd);
	do i=1 to dim(myDates);	
		hours = hoursInADay(myDates{i});
		put myDates{i}=yymmdd10.;
		put hours=/;
	end;
run;

And the results are what I expected:

myDates[1]=2019-03-28
hours=24

 

myDates[2]=2019-03-10
hours=23

 

myDates[3]=2018-11-04
hours=25

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1434 views
  • 0 likes
  • 2 in conversation