10-22-2015 07:26 AM
I have been trying so hard to do what I know I've done but cannot.
I need to create new variables from my current time variables: EndTime-StartTime=New Time.
They are numeric and in the 00:00 format. I've been incredibly stressed which I think is hindering my progress.
I've created the NewTime in my data step but I feel that I am missing formatting.
Please any advice is needed as of course, it needs to be done today.
10-22-2015 07:45 AM
I didn't get what you mean exactly but if i'm correct you are trying to use some
datetimes stored in a table as data and transform into columns.
Or if you are trying to use, you need to set this date into character to uses as
a column the syntax is like this: "19mar2015"n;
Hope this helps
10-22-2015 07:52 AM
10-22-2015 07:53 AM
So, you have something like:
data temp; endtime="14:10"t; starttime="08:13"t; new_time=endtime-starttime; format endtime starttime new_time time5.; run;
If its just formatting you are after, use the format command. If there is more, then post test data in form of datatstep, and required output to describe the problem.
10-22-2015 08:09 AM
10-22-2015 08:20 AM
Yes, it does. So your endtime and starttime are character variables according to that data. To do computations on them they need to be numeric, hence we use input() function, however as you have values which are not times = "U" then we need to put that in a conditional so that only when not U then do we do the calculation, otherwise you will get warnings about invalid data.
data have; endtime="15:26"; starttime="14:15"; output; endtime="13:16"; starttime="12:10"; output; endtime="11:42"; starttime="U"; output; run; data want; set have; if endtime ne "U" and starttime ne "U" then newtime=input(endtime,time5.) - input(starttime,time5.); format newtime tod5.; run;
In this example, I only calculate newtime if both are valid times (to avoid warnings), i convert each to numeric version, subtract them. ONce that is done, I then apply a time format to the newtime variable. Remember dates are recorded as number of days since a certain date, and times are number of seconds. So to use these numerically they need to be converted from the character ones. It is however far better to store dates and times in your datasets as specific date or time variables, consider the have dataset:
data have; endtime="15:26"; starttime="14:15"; output; endtime="13:16"; starttime="12:10"; output; endtime="11:42"; starttime="U"; output; run; data want (keep=etime stime); set have; if endtime ne "U" then etime=input(endtime,time5.); if starttime ne "U" then stime=input(starttime,time5.); format etime stime tod5.; run;
In the want dataset, the variables for the two items are numeric values which can be "seen" as time values, and used in the way that numeric variables can be.
10-22-2015 08:25 AM
10-22-2015 08:28 AM
Tod5. is an inbuilt SAS format:
It does not matter how many observations there are, a datastep is like a loop, doing the code within for each observation:
data want; set have; if endtime ne "U" and starttime ne "U" then newtime=input(endtime,time5.) - input(starttime,time5.); format newtime tod5.; run;
So in the above the if endtime ne "U" ... is applied to every observation.
10-22-2015 08:56 AM
I hope this helps. Jim
data one; drop u;
format endtime startime duration time6.;
informat endtime startime time6.;
input endtime : u$6.;
if u='U' then startime=.;
if startime ne . then do;
if duration lt 1 then duration=duration+(3600*24);
; proc print; run;
10-29-2015 10:21 PM
10-30-2015 04:55 AM
You could do it with proc format, e.g.
"00:00"t - "01:00"t = "0-1hr"
NOt sure if that is the correct syntax, I personally don't use formats. I would do it in a select clause myself:
data want; set have; length diff $20; select; when("00:00"t < your_time <= "01:00"t) diff="0-1hrs"; when("01:00"t < your_time <= "02:00"t) diff="1-2hrs"; ... otherwise diff="Unknown"; end; run;