Search code examples
spss

Converting string variable that contains date into a date variable in SPSS


My goal is to convert string variable "birthday" that contains date in a date variable. When I run my syntax I get this "the string to be converted via the number function is zero length". I assume that something is wrong with missing values.

My dataset looks like this:

 birthday
 2004-07-01 00:00:00
 2005-09-01 00:00:00
 2006-03-01 00:00:00
 2007-07-01 00:00:00

Syntax that I has written:

RECODE birthday (MISSING='99').
EXECUTE.

COMPUTE geboortedatum=number(birthday, YMDHMS19).
VARIABLE LABELS birthday ''.
VARIABLE LEVEL  birthday (SCALE).
FORMATS birthday (MOYR8).
VARIABLE WIDTH  birthday(8).
EXECUTE.

Solution

  • The message you're getting refers to cases where the birthday is empty.
    Note that since this is a string variable and not a number, although the date is missing there, spss won't recognize it as missing data unless you define it so. So your recode command RECODE birthday (MISSING='99'). wont have any effect, you should change it to:

    RECODE birthday (''='99').
    

    Now either way, empty or '99', the date function will not be able to treat the value and so you will get messages about it (and the value in the new column will be missing).
    If you want to avoid getting the messages, instead of the recode command you can do this:

    if birthday<>"" geboortedatum=number(birthday, YMDHMS19).