flag time as 0 and 1

Accepted Solution Solved
Reply
Contributor
Posts: 30
Accepted Solution

flag time as 0 and 1

Hi -

 

My data set looks like this:

id  time 

1   7:20

1   7:26

2   8:15

2   8:40

I want to create a flag and a dummy variable that can label a certain time:

id  time   flag    dummy

1   7:20   7:20    1

1   7:26   7:20    0

2   8:15   7:20    0

2   8:40   7:20    0

My current code is attached below:

data flag;
  set data;
  flag = '7:20't;
  if time = flag then dummy = 1;
  else dummy = 0;
run;

This does not work (all value in dummy are zeros). I extracted time from datetime. format, the original time is like 17MAR2016 07:20:00, is that the reason? 

Not sure how to solve it.  Any idea?

 

Thanks!!!

 


Accepted Solutions
Solution
‎07-08-2017 07:23 PM
Community Manager
Posts: 2,955

Re: flag time as 0 and 1

That blog post I linked to shows exactly how.  You have to decide whether to truncate (7:20:34 becomes 7:20:00) or round (to 7:21:00).

 

Easy way to round, adapted from that post:

 

data _null_;
	t = '07:20:34't;
	t_m = round(t,60); /* nearest minute */
	put t_m= time5.;
run;

http://blogs.sas.com/content/sgf/2017/02/10/truncating-vs-rounding-sas-time-values/ 

View solution in original post


All Replies
PROC Star
Posts: 7,474

Re: flag time as 0 and 1

Post the code you used to extract time.

 

Art, CEO, AnalystFinder.com

 

Contributor
Posts: 30

Re: flag time as 0 and 1

Hi - 

I used timepart to extract the time:

data want;
   set data;
   time = timepart(time);
   format time hhmm5.;
run;

Thanks!!

Community Manager
Posts: 2,955

Re: flag time as 0 and 1

[ Edited ]

This might be an issue of precision and rounding.  Do you know that your "7:20" value is exactly 7:20, with no seconds?  For a comparison of just hours/minutes, you might want to either truncate or round the values before comparison. 

 

@LeonidBatkhan has a good blog post about rounding/truncating time values here.

Contributor
Posts: 30

Re: flag time as 0 and 1

Posted in reply to ChrisHemedinger

Hi -

The original times have seconds in them, like 07:20:34. Not sure how to round all of the times to minutes (07:20:34 -> 07:20:00).

Any suggestions?

 

Solution
‎07-08-2017 07:23 PM
Community Manager
Posts: 2,955

Re: flag time as 0 and 1

That blog post I linked to shows exactly how.  You have to decide whether to truncate (7:20:34 becomes 7:20:00) or round (to 7:21:00).

 

Easy way to round, adapted from that post:

 

data _null_;
	t = '07:20:34't;
	t_m = round(t,60); /* nearest minute */
	put t_m= time5.;
run;

http://blogs.sas.com/content/sgf/2017/02/10/truncating-vs-rounding-sas-time-values/ 

Contributor
Posts: 30

Re: flag time as 0 and 1

Posted in reply to ChrisHemedinger

That links is very helpful!! 

I think if the time is 7:20:45, it should be round to 7:21; and if the time is 7:20:12, it should be round to 7:20, so round function should 

work perfectly.

 

Thanks again!!

PROC Star
Posts: 7,474

Re: flag time as 0 and 1

Of course, if you didn't want to round you could always use:

data data;
  informat time anydtdtm16.;
  input id  time;
  cards;
1   8jul2017:7:20:30
1   8jul2017:7:26:31
2   8jul2017:8:15:31
2   8jul2017:8:40:31
;

data flag;
  set data;
  flag = '7:20't;
  if hour(timepart(time)) eq hour(flag) and minute(timepart(time)) eq minute(flag) then dummy = 1;
  else dummy = 0;
run;

Art, CEO, AnalystFinder.com

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 173 views
  • 3 likes
  • 3 in conversation