Discussion:
SPSS Recode date into weekdays
(too old to reply)
c***@gmail.com
2013-10-20 07:13:14 UTC
Permalink
Hi

I have a question about how to recode dates into monday to sunday, i want to achieve it using loop commend, but quite confuse on how to achieve that.

data may like this

1/8/2011
.
.
.
2/8/2011
.
.
3/8/2011
.
.
.
.
.

how can i compute a new variable as 1(Monday) 2(Tuesday)....7(Monday)?

Thanks
Art Kendall
2013-10-20 11:57:22 UTC
Permalink
Open a new instance of SPSS.
Copy and paste the syntax below into a syntax window.
Run it.

data list list/mydate(adate10).
begin data
1/1/2013
1/2/2013
1/3/2013
1/1/2014
1/1/2015
end data.
numeric DayOfWeek(f1).
compute DayOfWeek = xdate.wkday(mydate).
value labels DayOfWeek
1 'Sunday'
2 'Monday'
3 'Tuesday'
4 'Wednesday'
5 'Thursday'
6 'Friday'
7 'Saturday'.
list.

Art Kendall
Social Research Consultants
Post by c***@gmail.com
1/8/2011
.
.
.
2/8/2011
.
.
3/8/2011
.
.
Bruce Weaver
2015-01-14 20:39:21 UTC
Permalink
Post by Art Kendall
Open a new instance of SPSS.
Copy and paste the syntax below into a syntax window.
Run it.
data list list/mydate(adate10).
begin data
1/1/2013
1/2/2013
1/3/2013
1/1/2014
1/1/2015
end data.
numeric DayOfWeek(f1).
compute DayOfWeek = xdate.wkday(mydate).
value labels DayOfWeek
1 'Sunday'
2 'Monday'
3 'Tuesday'
4 'Wednesday'
5 'Thursday'
6 'Friday'
7 'Saturday'.
list.
Art Kendall
Social Research Consultants
Note that the OP asked for "1(Monday) 2(Tuesday)", etc. A slight
modification to Art's code will achieve that. E.g.,

data list list/mydate(adate10).
begin data
1/12/2015
1/13/2015
1/14/2015
1/15/2015
1/16/2015
1/17/2015
1/18/2015
1/19/2015
end data.
numeric DayOfWeek(f1).
***************************************** .
compute DayOfWeek = xdate.wkday(mydate)-1.
recode DayOfWeek (0 = 7).
***************************************** .
value labels DayOfWeek
1 'Monday'
2 'Tuesday'
3 'Wednesday'
4 'Thursday'
5 'Friday'
6 'Saturday'
7 'Sunday'.
list.

OUTPUT:
mydate DayOfWeek

01/12/2015 1
01/13/2015 2
01/14/2015 3
01/15/2015 4
01/16/2015 5
01/17/2015 6
01/18/2015 7
01/19/2015 1

Number of cases read: 8 Number of cases listed: 8
--
Bruce Weaver
***@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/Home
"When all else fails, RTFM."
s***@gmail.com
2018-10-03 16:40:36 UTC
Permalink
Post by Bruce Weaver
Post by Art Kendall
Open a new instance of SPSS.
Copy and paste the syntax below into a syntax window.
Run it.
data list list/mydate(adate10).
begin data
1/1/2013
1/2/2013
1/3/2013
1/1/2014
1/1/2015
end data.
numeric DayOfWeek(f1).
compute DayOfWeek = xdate.wkday(mydate).
value labels DayOfWeek
1 'Sunday'
2 'Monday'
3 'Tuesday'
4 'Wednesday'
5 'Thursday'
6 'Friday'
7 'Saturday'.
list.
Art Kendall
Social Research Consultants
Note that the OP asked for "1(Monday) 2(Tuesday)", etc. A slight
modification to Art's code will achieve that. E.g.,
data list list/mydate(adate10).
begin data
1/12/2015
1/13/2015
1/14/2015
1/15/2015
1/16/2015
1/17/2015
1/18/2015
1/19/2015
end data.
numeric DayOfWeek(f1).
***************************************** .
compute DayOfWeek = xdate.wkday(mydate)-1.
recode DayOfWeek (0 = 7).
***************************************** .
value labels DayOfWeek
1 'Monday'
2 'Tuesday'
3 'Wednesday'
4 'Thursday'
5 'Friday'
6 'Saturday'
7 'Sunday'.
list.
mydate DayOfWeek
01/12/2015 1
01/13/2015 2
01/14/2015 3
01/15/2015 4
01/16/2015 5
01/17/2015 6
01/18/2015 7
01/19/2015 1
Number of cases read: 8 Number of cases listed: 8
--
Bruce Weaver
http://sites.google.com/a/lakeheadu.ca/bweaver/Home
"When all else fails, RTFM."
Can you help me know any syntax in SPSS, that convert Date into Weekday.
E.g if date is appearing as 02 Oct 2018 than i want to change into Tuesday
o***@gmail.com
2015-01-14 17:42:38 UTC
Permalink
create a dummy variable for day of week in SPSS
COMPUTE wkday=XDATE.WKDAY(date).
execute.
compute Mon=0.
compute Tue=0.
compute Wed=0.
compute Thu=0.
compute Fri=0.
compute Sat=0.
compute Sun=0.
if wkday =1 Sun=1.
if wkday =2 Mon=1.
if wkday =3 Tue=1.
if wkday =4 Wed=1.
if wkday =5 Thu=1.
if wkday =6 Fri=1.
if wkday =7 Sat=1.
execute.
s***@gmail.com
2018-10-03 16:40:13 UTC
Permalink
Post by o***@gmail.com
create a dummy variable for day of week in SPSS
COMPUTE wkday=XDATE.WKDAY(date).
execute.
compute Mon=0.
compute Tue=0.
compute Wed=0.
compute Thu=0.
compute Fri=0.
compute Sat=0.
compute Sun=0.
if wkday =1 Sun=1.
if wkday =2 Mon=1.
if wkday =3 Tue=1.
if wkday =4 Wed=1.
if wkday =5 Thu=1.
if wkday =6 Fri=1.
if wkday =7 Sat=1.
execute.
Can you help me know any syntax in SPSS, that convert Date into Weekday.
E.g if date is appearing as 02 Oct 2018 than i want to change into Tuesday
Ki
2018-10-03 18:18:24 UTC
Permalink
Post by s***@gmail.com
Post by o***@gmail.com
create a dummy variable for day of week in SPSS
COMPUTE wkday=XDATE.WKDAY(date).
execute.
compute Mon=0.
compute Tue=0.
compute Wed=0.
compute Thu=0.
compute Fri=0.
compute Sat=0.
compute Sun=0.
if wkday =1 Sun=1.
if wkday =2 Mon=1.
if wkday =3 Tue=1.
if wkday =4 Wed=1.
if wkday =5 Thu=1.
if wkday =6 Fri=1.
if wkday =7 Sat=1.
execute.
Can you help me know any syntax in SPSS, that convert Date into Weekday.
E.g if date is appearing as 02 Oct 2018 than i want to change into Tuesday
Something like this:

*Compute here the weekday.

compute Day_of_week = xdate.WKDAY(day_of_call_R).

*1 is Sunday, 2 is Monday, etc.
*day_of_call_R should be in one of the date format.

execute.
David Marso
2018-10-04 20:28:56 UTC
Permalink
PREVIOUSLY ANSWERED IN 2015:
"create a dummy variable for day of week in SPSS
COMPUTE wkday=XDATE.WKDAY(date).
execute.
compute Mon=0.
compute Tue=0.
compute Wed=0.
compute Thu=0.
compute Fri=0.
compute Sat=0.
compute Sun=0.
if wkday =1 Sun=1.
if wkday =2 Mon=1.
if wkday =3 Tue=1.
if wkday =4 Wed=1.
if wkday =5 Thu=1.
if wkday =6 Fri=1.
if wkday =7 Sat=1.
execute. "

A better way IMNSHO.
NUMERIC Mon Tue Wed Thu Fri Sat Sun(F1).
RECODE Mon TO Sun(ELSE=0).
VECTOR DoW=Mon TO Sun.
COMPUTE DoW(XDATE.WKDAY(date) )=1.
VALUE LABELS....
VARIABLE LABELS....

Loading...