I would like to convert a column of strings that are in YYYY-MM-DD into date format of MM/DD/YYYY.
I would like to use exclusively spss syntax to do this. When I try to use:
STRING date (A10) "YYYY-MM-DD".
STRING converted_date (A10) "MM/DD/YYYY".
* Convert the entire "date" column to MM/DD/YYYY format.
COMPUTE converted_date = DATE.MDY(SUBSTR(date, 6, 2),
SUBSTR(date, 9, 2), SUBSTR(date, 1, 4)).
FORMAT converted_date (ADATE10).
EXECUTE.
The errors that I get relate to variable lists being empty or variables already being defined. I would appreciate any approach to do this!
There are a few problems with your syntax:
Your original date
variable should already exist, you dont need to define it here.
When you define your second variable you add "MM/DD/YYYY" at the end - this is what gives you the error message. If this is meant as a remark for yourself you can use:
STRING converted_date (A10) /* "MM/DD/YYYY".
The command DATE.MDY
creates a numeric variable, so you can't use it to calculate a string variable like you did, and
If you want to create a numeric variable instead, you need to feed DATE.MDY
numbers and not strings.
So here are the two possibilities:
* create a string variable which will contain the date in the new format.
string converted_date_string (a10).
COMPUTE converted_date_string = concat(
char.substr(date, 6, 2), "/",
char.substr(date, 9, 2), "/",
char.substr(date, 1, 4)).
* convert date into a numeric value with MM/DD/YYYY format.
COMPUTE converted_date_numeric = DATE.MDY(
number(char.substr(date, 6, 2),f2),
number(char.substr(date, 9, 2),f2),
number(char.substr(date, 1, 4),f4)
).
FORMAT converted_date_numeric (ADATE10).
Note that a string variable is just a string... If you want to calculate on dates you need to use the second option above - it has to be a numeric variable.