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!!!
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/
Post the code you used to extract time.
Art, CEO, AnalystFinder.com
Hi -
I used timepart to extract the time:
data want;
set data;
time = timepart(time);
format time hhmm5.;
run;
Thanks!!
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.
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?
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/
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!!
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
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.