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/ 

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.

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.

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
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/ 

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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