Hello all,
Another small question - I am working with spss 14 and using the date function. I have a variable imported from Excel that is a date but it is in a numeric format. I want to make it a recognized date variable in spss. For July 25, 2006 the value appears as 20060725. Using the date/time wizard it appears easiest to transform a variable that is a string. I want to do this in the syntax but I can't figure out how. This means that I have to go back to the data window and change it to a string manually. I am sure this is easy to do but I have been through the manual section on formats with no luck... Many thanks Diane UofT. |
Try This:
Compute Var_Year = number(substr(Date_var,1,4)). Compute Var_Month = number(substr(Date_var,5,2)). Compute Var_Day = number(substr(Date_var,7,2)). Compute New_Date = DATE.MDY(Var_Month,Var_Day,Var_Year). FORMATS New_Date (ADATE10). EXECUTE. ---Edward. -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]]On Behalf Of [Ela Bonbevan] Sent: Wednesday, July 12, 2006 10:25 AM To: [hidden email] Subject: Transforming date variables Hello all, Another small question - I am working with spss 14 and using the date function. I have a variable imported from Excel that is a date but it is in a numeric format. I want to make it a recognized date variable in spss. For July 25, 2006 the value appears as 20060725. Using the date/time wizard it appears easiest to transform a variable that is a string. I want to do this in the syntax but I can't figure out how. This means that I have to go back to the data window and change it to a string manually. I am sure this is easy to do but I have been through the manual section on formats with no luck... Many thanks Diane UofT. |
In reply to this post by [Ela Bonbevan]
I think if the date variable is numeric then use this one
Compute Var_Year = number(substr(string(Date_var,f8.0),1,4),f4.0). Compute Var_Month = number(substr(string(Date_var,f8.0),5,2),f2.0). Compute Var_Day = number(substr(string(Date_var,f8.0),7,2),f2.0). exe. Compute New_Date = DATE.MDY(Var_Month,Var_Day,Var_Year). FORMATS New_Date (ADATE10). EXECUTE. -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Edward Boadi Sent: Wednesday, July 12, 2006 10:59 AM To: [hidden email] Subject: Re: Transforming date variables Try This: Compute Var_Year = number(substr(Date_var,1,4)). Compute Var_Month = number(substr(Date_var,5,2)). Compute Var_Day = number(substr(Date_var,7,2)). Compute New_Date = DATE.MDY(Var_Month,Var_Day,Var_Year). FORMATS New_Date (ADATE10). EXECUTE. ---Edward. -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]]On Behalf Of [Ela Bonbevan] Sent: Wednesday, July 12, 2006 10:25 AM To: [hidden email] Subject: Transforming date variables Hello all, Another small question - I am working with spss 14 and using the date function. I have a variable imported from Excel that is a date but it is in a numeric format. I want to make it a recognized date variable in spss. For July 25, 2006 the value appears as 20060725. Using the date/time wizard it appears easiest to transform a variable that is a string. I want to do this in the syntax but I can't figure out how. This means that I have to go back to the data window and change it to a string manually. I am sure this is easy to do but I have been through the manual section on formats with no luck... Many thanks Diane UofT. |
In reply to this post by [Ela Bonbevan]
I might use scratch variables for all those intermediate variables, and/or I might just parse the number using the TRUNC and MOD functions, as in:
compute #day=mod(datevar,100). compute #month=trunc(mod(datevar,10000)/100). compute #year=trunc(datevar/10000). compute newdate=date.mdy(#month, #day, #year). formats newdate (adate10). You could, of course condense this down to one largish Compute statement: compute newdate=date.mdy(trunc(mod(datevar,10000)/100), mod(datevar,100),trunc(datevar/10000)). Both this solution and the substring solution assume that all dates are in the form yyyymmdd with four-digit years, two-digit months, and two-digit days. But...I think SPSS normally reads and correctly converts Excel dates to SPSS date-format variables, so theoretically none of this should be necessary. -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Siraj Ur-rehman Sent: Wednesday, July 12, 2006 12:04 PM To: [hidden email] Subject: Re: Transforming date variables I think if the date variable is numeric then use this one Compute Var_Year = number(substr(string(Date_var,f8.0),1,4),f4.0). Compute Var_Month = number(substr(string(Date_var,f8.0),5,2),f2.0). Compute Var_Day = number(substr(string(Date_var,f8.0),7,2),f2.0). exe. Compute New_Date = DATE.MDY(Var_Month,Var_Day,Var_Year). FORMATS New_Date (ADATE10). EXECUTE. -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Edward Boadi Sent: Wednesday, July 12, 2006 10:59 AM To: [hidden email] Subject: Re: Transforming date variables Try This: Compute Var_Year = number(substr(Date_var,1,4)). Compute Var_Month = number(substr(Date_var,5,2)). Compute Var_Day = number(substr(Date_var,7,2)). Compute New_Date = DATE.MDY(Var_Month,Var_Day,Var_Year). FORMATS New_Date (ADATE10). EXECUTE. ---Edward. -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]]On Behalf Of [Ela Bonbevan] Sent: Wednesday, July 12, 2006 10:25 AM To: [hidden email] Subject: Transforming date variables Hello all, Another small question - I am working with spss 14 and using the date function. I have a variable imported from Excel that is a date but it is in a numeric format. I want to make it a recognized date variable in spss. For July 25, 2006 the value appears as 20060725. Using the date/time wizard it appears easiest to transform a variable that is a string. I want to do this in the syntax but I can't figure out how. This means that I have to go back to the data window and change it to a string manually. I am sure this is easy to do but I have been through the manual section on formats with no luck... Many thanks Diane UofT. |
Free forum by Nabble | Edit this page |