Hi all, I have data where i need to compute 4 separate groups based on dates and times of when an order was placed. The first 2 groups are easy since they are based just on date of order. compute status = $sysmis. if orderdatetime lt Date.DMY(30,11,2019) status = 0. if orderdatetime ge Date.DMY(01,12,2019) and orderdatetime le Date.DMY(04,06,2020) status = 1. The next 2 groups have the same date range (Date.DMY ge (05,06,2020)) but they needed to be divided into Shift 1 and Shift 2: Shift 1 = 1100 - 2300 Shift 2 = 2301 - 1059 Do I first need to extract out the time from the date so that i have both an orderdate and ordertime? Or is there a way i can do this with the current orderdatetime variable? Thanks much. Carol |
I don't have SPSS available on this computer. If you want 4 groups in the same variable, change 'Shift = 1 +' to 'Status = 3 +'.
* untested *. if (orderdatetime ge Date.DMY(05,06,2020)) Shift = 1 + ((XDATE.TIME(orderdatetime) lt CTIME.HOURS(11)) AND (XDATE.TIME(orderdatetime) gt CTIME.HOURS(23))). execute.
|
That looks close, but as I read it, the logic is off - I read this as
shift= (time lt 11) AND (time gt 23). Which is always false. Testing the 11-23 range, and keeping the assignments of 1 and 2, I get (now using ge and le) shift= 2 - (time ge 11) AND (time le 23). Rich Ulrich |
In reply to this post by parisec
untested
compute MyHour = xdate.hour(MyDateTimeVar). recode MyHour (missing,sysmis =-1) (11 to 23=1)(else=2) into shift. value labels shift . . .
Art Kendall
Social Research Consultants |
In reply to this post by Rich Ulrich
Appreciate all of the feedback!
I extracted out the time from the date to create the ordertime variable. The variable is HH:MM in 24 hour time. This worked. compute testtime = $sysmis. if (ordertime ge TIME.HMS (00,00) and ordertime le TIME.HMS (10,59)) testtime=1 . if (ordertime ge TIME.HMS (11,00)) testtime=2. freq testtime. Thank you all for your time. Carol |
the reason testtime is missing is NOT because the machine is unable to follow your instructions. You know that test time is missing because ordertime was missing.
I suggest you replace compute testtime = $sysmis. with if missing(time.hms) testtime =-1. missing values testtime (-1). value labels testtime -1 'ordertime was missing'.
Art Kendall
Social Research Consultants |
Administrator
|
In reply to this post by parisec
Can you replace this line...
if (ordertime ge TIME.HMS (00,00) and ordertime le TIME.HMS (10,59)) testtime=1 . with this? IF RANGE(ordertime, TIME.HMS(00,00), TIME.HMS (10,59)) testtime=1 .
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
In reply to this post by parisec
Carol,
Okay. But compared to the original problem, you have changed the assignment for 23:01 to 24:00 from shift (testtime) 1 to shift 2. Rich Ulrich |
In reply to this post by Bruce Weaver
Bruce,
Yes. this worked. First time using the RANGE command. Thanks! Carol |
In reply to this post by Rich Ulrich
you are correct Richard.
thanks! |
In reply to this post by parisec
To base this purely on time, you can just use xdate.time in the condition, but you can also use a datetime constant, which would be a date plus the number of seconds since midnight in the xondition.
|
Free forum by Nabble | Edit this page |