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

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!!!

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

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/ 

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!

View solution in original post

7 REPLIES 7
art297
Opal | Level 21

Post the code you used to extract time.

 

Art, CEO, AnalystFinder.com

 

panda
Quartz | Level 8

Hi - 

I used timepart to extract the time:

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

Thanks!!

ChrisHemedinger
Community Manager

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.

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
panda
Quartz | Level 8

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?

 

ChrisHemedinger
Community Manager

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/ 

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
panda
Quartz | Level 8

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!!

art297
Opal | Level 21

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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