calendar week

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

calendar week

drfg2008
Does anyone have a syntax that automatically calculates the calendar week?
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: calendar week

Rick Oliver-3
What does that mean? There is a function that returns the week number: XDATE.WEEK. But the value for any given date can differ between years.



Rick Oliver
Senior Information Developer
IBM Business Analytics (SPSS)
E-mail: [hidden email]




From:        drfg2008 <[hidden email]>
To:        [hidden email]
Date:        05/11/2012 04:15 PM
Subject:        calendar week
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Does anyone have a syntax that automatically calculates the calendar week?

-----
Dr. Frank Gaeth
FU-Berlin

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/calendar-week-tp5705454.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD


Reply | Threaded
Open this post in threaded view
|

Re: calendar week

drfg2008
ups, I had overlooked it. Thank you.
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: calendar week

drfg2008
In reply to this post by drfg2008
Thanks to MZ for the information that a 'calender week' in the US is not equivalent to other countries (D).
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: calendar week

Rick Oliver-3
Calendar weeks in SPSS Statistics assume that the first day of the year is the beginning of the first calendar week. Therefore, the week that contains any particular day of the year can be in a different calendar week in different years, and the week value range is 1-53.

Rick Oliver
Senior Information Developer
IBM Business Analytics (SPSS)
E-mail: [hidden email]
Phone: 312.893.4922 | T/L: 206-4922




From:        drfg2008 <[hidden email]>
To:        [hidden email]
Date:        05/16/2012 01:12 PM
Subject:        Re: calendar week
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Thanks to MZ for the information that a 'calender week' in the US is not
equivalent to other countries (D).

-----
Dr. Frank Gaeth
FU-Berlin

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/calendar-week-tp5705454p5711037.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD


Reply | Threaded
Open this post in threaded view
|

Re: calendar week

Jon K Peck
The definition of the first week does vary around the world, and hence, the week number can vary.  Common schemes are
-starts on day 1 of year
-starts on the date of the first wkday occurring in the year.  First day of week definition varies around the world
-first week with at least 4 days in it starting with the start-of-week date.

There are probably others.

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Rick Oliver/Chicago/IBM@IBMUS
To:        [hidden email]
Date:        05/16/2012 12:29 PM
Subject:        Re: [SPSSX-L] calendar week
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Calendar weeks in SPSS Statistics assume that the first day of the year is the beginning of the first calendar week. Therefore, the week that contains any particular day of the year can be in a different calendar week in different years, and the week value range is 1-53.

Rick Oliver
Senior Information Developer
IBM Business Analytics (SPSS)
E-mail: [hidden email]
Phone: 312.893.4922 | T/L: 206-4922




From:        
drfg2008 <[hidden email]>
To:        
[hidden email]
Date:        
05/16/2012 01:12 PM
Subject:        
Re: calendar week
Sent by:        
"SPSSX(r) Discussion" <[hidden email]>




Thanks to MZ for the information that a 'calender week' in the US is not
equivalent to other countries (D).

-----
Dr. Frank Gaeth
FU-Berlin

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/calendar-week-tp5705454p5711037.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD


Reply | Threaded
Open this post in threaded view
|

Re: calendar week

Robert Jones
I would hope there would also be provision for ISO 8601's definition

Robert
Reply | Threaded
Open this post in threaded view
|

Re: calendar week

Jon K Peck
The ISO week number can be calculated like this using Python programmability and the SPSSINC TRANS extension command.

This short program defines the computation.

begin program.
import datetime
def iso(year, mon, day):
  return datetime.date(year, mon, day).isocalendar()
end program


The SPSSINC TRANS extension command uses that program and iterates over the active dataset doing the calculation for each case.

spssinc trans result=isoyear isoweek, isoday
/formula "iso(year, month, day)".

ISO week numbers are a bit weird in that they can fall back to reference the previous year, so you get the year, week number, and day  back.

In this example, year, month, and day are SPSS variables, and three variables are created: isoyear, isoweek, and isoday
representing year, week number, and day of week number.

Regards,

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Robert Jones <[hidden email]>
To:        [hidden email]
Date:        05/17/2012 08:12 AM
Subject:        Re: [SPSSX-L] calendar week
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




I would hope there would also be provision for ISO 8601's definition

Robert

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/calendar-week-tp5705454p5711299.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD


Reply | Threaded
Open this post in threaded view
|

Re: calendar week

Robert Jones
Thanks
Reply | Threaded
Open this post in threaded view
|

Re: calendar week

nessie


Dear Jon, or any other wise person

Can you help me to make this or another script (ISO-week number) work with a variable on the form DATETIME17 (hh-mmm-yyyy hh:mm) or DATE11 (dd-mmm-yyyy)? I am sure it would be a valuable script for many European users.


I have tried to understand SPSSINC TRANS, but haven't quite got the hang of it.
Can I write the Python script directly in Syntax? Does it work like a macro?

Best regards
Lars N.
Reply | Threaded
Open this post in threaded view
|

Re: calendar week

Art Kendall
google "nabble spssx"
type "isoweek" in the edit box
key <enter>
click <calendar week>

Art Kendall
Social Research Consultants
On 4/16/2014 3:02 AM, nessie [via SPSSX Discussion] wrote:


Dear Jon, or any other wise person

Can you help me to make this or another script (ISO-week number) work with a variable on the form DATETIME17 (hh-mmm-yyyy hh:mm) or DATE11 (dd-mmm-yyyy)? I am sure it would be a valuable script for many European users.


I have tried to understand SPSSINC TRANS, but haven't quite got the hang of it.
Can I write the Python script directly in Syntax? Does it work like a macro?

Best regards
Lars N.


If you reply to this email, your message will be added to the discussion below:
http://spssx-discussion.1045642.n5.nabble.com/calendar-week-tp5705454p5725470.html
To start a new topic under SPSSX Discussion, email [hidden email]
To unsubscribe from SPSSX Discussion, click here.
NAML

Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: calendar week

Andy W
In reply to this post by nessie
Jon's original solution can be amended to work with this by extracting the relevant portions from the DateTime variable in SPSS and then submitting those to the python program. Here I amend Jon's original slightly to coerce the variables to integers within the python function.

*************************.
DATA LIST FREE / Id.
BEGIN DATA
0
1
2
END DATA.
COMPUTE MyDate = $TIME - (Id*60*60*24).
FORMATS MyDate (DATETIME17).

*Simple way with current program, just create your own seperate time variables.
COMPUTE year = XDATE.YEAR(MyDate).
COMPUTE month = XDATE.MONTH(MyDate).
COMPUTE day = XDATE.MDAY(MyDate).
FORMATS year (F4.0) month day (F2.0).
EXECUTE.

begin program.
import datetime
def iso(year, mon, day):
  return datetime.date(int(year), int(mon), int(day)).isocalendar()
end program.

spssinc trans result=isoyear isoweek isoday TYPE = 0 0 0
/formula "iso(year, month, day)".
*************************.

I thought that to avoid making the new variables you could change the python formula to simply take one date-time variable, but this ends up being alittle more complicated. It appears SPSSINC does not automatically turn SPSS datetime variables into python datetime objects (just passes the seconds from the epoch). If you don't want to bother making the new variables, it will take alittle more work to coerce the variable to a python datetime object (see https://docs.python.org/2/library/datetime.html#datetime.datetime.fromtimestamp - it looks like those dates start from 1/1/1970)

*************************.
*Updating Python program to take a datetime variables.
BEGIN PROGRAM.
import datetime
def isoDate(MyDate):
  return datetime.MyDate.isocalendar()
END PROGRAM.

*This does not work.
SPSSINC TRANS RESULT=isoyear2 isoweek2 isoday2 TYPE = 0 0 0
  /FORMULA "isoDate(MyDate)".

*What will SPSS return?.
BEGIN PROGRAM.
def testDate(MyDate):
  return MyDate
END PROGRAM.
SPSSINC TRANS RESULT=Test TYPE = 0
  /FORMULA "testDate(MyDate)".

*SPSS does not assume the variable is datetime.
BEGIN PROGRAM.
import spss
import datetime
MyVars = [1]
dataCursor=spss.Cursor(MyVars,cvtDates=MyVars)
#dataCursor=spss.Cursor(MyVars)
MyData=dataCursor.fetchone()
dataCursor.close()
print MyData[0].isocalendar()
END PROGRAM.
*see https://docs.python.org/2/library/datetime.html#datetime.datetime.fromtimestamp.
*To convert the timestamp.
*************************.

Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: calendar week

Jon K Peck
SPSSINC TRANS does not convert SPSS dates into Python, because you can do this in the plugin program if you need it.  Here is an example (and notice that there is no need for the cursed EXECUTE.)

DATA LIST FREE / Id.
BEGIN DATA
0
1
2
END DATA.
COMPUTE MyDate = $TIME - (Id*60*60*24).
FORMATS MyDate (DATETIME17).

begin program.
import datetime
from spssdata import CvtSpssDatetime
def iso(spssdate):
  return CvtSpssDatetime(spssdate).isocalendar()
end program.

spssinc trans result=isoyear isoweek isoday TYPE = 0
/formula "iso(MyDate)".


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Andy W <[hidden email]>
To:        [hidden email],
Date:        04/16/2014 07:02 AM
Subject:        Re: [SPSSX-L] calendar week
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Jon's original solution can be amended to work with this by extracting the
relevant portions from the DateTime variable in SPSS and then submitting
those to the python program. Here I amend Jon's original slightly to coerce
the variables to integers within the python function.

*************************.
DATA LIST FREE / Id.
BEGIN DATA
0
1
2
END DATA.
COMPUTE MyDate = $TIME - (Id*60*60*24).
FORMATS MyDate (DATETIME17).

*Simple way with current program, just create your own seperate time
variables.
COMPUTE year = XDATE.YEAR(MyDate).
COMPUTE month = XDATE.MONTH(MyDate).
COMPUTE day = XDATE.MDAY(MyDate).
FORMATS year (F4.0) month day (F2.0).
EXECUTE.

begin program.
import datetime
def iso(year, mon, day):
 return datetime.date(int(year), int(mon), int(day)).isocalendar()
end program.

spssinc trans result=isoyear isoweek isoday TYPE = 0 0 0
/formula "iso(year, month, day)".
*************************.

I thought that to avoid making the new variables you could change the python
formula to simply take one date-time variable, but this ends up being
alittle more complicated. It appears SPSSINC does not automatically turn
SPSS datetime variables into python datetime objects (just passes the
seconds from the epoch). If you don't want to bother making the new
variables, it will take alittle more work to coerce the variable to a python
datetime object (see
https://docs.python.org/2/library/datetime.html#datetime.datetime.fromtimestamp
- it looks like those dates start from 1/1/1970)

*************************.
*Updating Python program to take a datetime variables.
BEGIN PROGRAM.
import datetime
def isoDate(MyDate):
 return datetime.MyDate.isocalendar()
END PROGRAM.

*This does not work.
SPSSINC TRANS RESULT=isoyear2 isoweek2 isoday2 TYPE = 0 0 0
 /FORMULA "isoDate(MyDate)".

*What will SPSS return?.
BEGIN PROGRAM.
def testDate(MyDate):
 return MyDate
END PROGRAM.
SPSSINC TRANS RESULT=Test TYPE = 0
 /FORMULA "testDate(MyDate)".

*SPSS does not assume the variable is datetime.
BEGIN PROGRAM.
import spss
import datetime
MyVars = [1]
dataCursor=spss.Cursor(MyVars,cvtDates=MyVars)
#dataCursor=spss.Cursor(MyVars)
MyData=dataCursor.fetchone()
dataCursor.close()
print MyData[0].isocalendar()
END PROGRAM.
*see
https://docs.python.org/2/library/datetime.html#datetime.datetime.fromtimestamp.
*To convert the timestamp.
*************************.





-----
Andy W
[hidden email]
http://andrewpwheeler.wordpress.com/
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/calendar-week-tp5705454p5725480.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD


Reply | Threaded
Open this post in threaded view
|

Re: calendar week

nessie
Thank's a lot Andy and Jon, this worked like a charm and made my life a hole lot easier. I am sure there are many users out there that will benefit from these formulas.

Best regards Lars N.