I have a data file with numeric date fields that I would like to change to string variables so I can create substrings. For example, a date like 20120825 I would like to break out to three variables: 2012, another variable 08, and another
25. Thanks for any help you can provide. Arthur Kramer, PhD Director of Institutional Research New Jersey City University |
Administrator
|
Hi Art. See if this example helps. It assumes the date strings are all 8 characters in length, as in the example you gave. If that is not so, the syntax will have to be made more general.
* Read in some sample data. new file. dataset close all. data list list / StringVar (A8). begin data 20120825 20120926 20121027 end data. * The following uses some code borrowed from * the Date & Time Wizard. * Pull out YYYY-MM-DD components. * Here, I assume you actually want to store them as separate variables. COMPUTE Year = number(substr(ltrim(StringVar),1,4),f4.0). COMPUTE Month = number(substr(ltrim(StringVar),5,2),f2.0). COMPUTE Day = number(substr(ltrim(StringVar),7,2),f2.0). * I assume you really want a true date variable at the end of the day. COMPUTE DateVar = date.dmy(Day,Month,Year). FORMATS DateVar (DATE11). VARIABLE WIDTH DateVar(11). LIST. * If you don't need the YYYY-MMM-DD components, you * could use this syntax pasted from the Date & Time Wizard * but with things lined up in a way that makes it * easier to follow (when viewed in fixed font, at least). * Return file to original state, then re-compute with scratch variables. DELETE VARIABLES Year to DateVar. * Date and Time Wizard: DateVar. COMPUTE DateVar= date.dmy(number(substr(ltrim(StringVar),7,2),f2.0), number(substr(ltrim(StringVar),5,2),f2.0), number(substr(ltrim(StringVar),1,4),f4.0)). VARIABLE LABELS DateVar ''. VARIABLE LEVEL DateVar (SCALE). FORMATS DateVar (DATE11). VARIABLE WIDTH DateVar(11). LIST.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
In reply to this post by Arthur Kramer
At 05:33 PM 10/18/2013, Arthur Kramer wrote:
>I have a data file with numeric date fields that I would like to >change to string variables so I can create substrings. For example, >a date like 20120825 I would like to break out to three >variables: 2012, another variable 08, and another 25. If your dates are numerical in that form, you have several choices. a.) Strings, as you requested: STRING Str_DATE (A8). COMPUTE Str_Date=STRING(Date,F8). In recent versions of SPSS, you can also use the ALTER TYPE command. b.) You can break out year, month, and day from the numeric form: NUMERIC Year (F4), Month(F2), Day (F2). COMPUTE Day = MOD (Date,100). COMPUTE Month = MOD((Date-Day)/100,100). COMPUTE Year = (Date-100*Month-Day)/1E4. c.) Recommended: convert your dates to SPSS date variables, then use XDATE functions as needed. Proceed as above, preferably using scratch variables for month, day, and year (i.e., #Year, #Month, #Day), and COMPUTE #Day = MOD (Date,100). COMPUTE #Month = MOD((Date-#Day)/100,100). COMPUTE #Year = (Date-100*#Month-#Day)/1E4. NUMERIC SPSSDate (DATE11). COMPUTE SPSSDate =DATE.DMY(#Day,#Month,#Year). =========================================================== APPENDIX 1: Results of all of the above computations: |-----------------------------|---------------------------| |Output Created |18-OCT-2013 18:14:09 | |-----------------------------|---------------------------| Date Str_DATE Year Month Day SPSSDate 17760704 17760704 1776 7 4 04-JUL-1776 19440606 19440606 1944 6 6 06-JUN-1944 20131018 20131018 2013 10 18 18-OCT-2013 Number of cases read: 3 Number of cases listed: 3 ================================= APPENDIX 2: Test data, and all code ================================= NEW FILE. DATA LIST FREE /Date (F8). BEGIN DATA 17760704 19440606 20131018 END DATA. * a.) Strings, as you requested: ... . STRING Str_DATE (A8). COMPUTE Str_Date=STRING(Date,F8). *b.) You can break out year, month, and day from the numeric form: NUMERIC Year (F4), Month(F2), Day (F2). COMPUTE Day = MOD (Date,100). COMPUTE Month = MOD((Date-Day)/100,100). COMPUTE Year = (Date-100*Month-Day)/1E4. *c.) Recommended: convert your dates to SPSS date variables ... . COMPUTE #Day = MOD (Date,100). COMPUTE #Month = MOD((Date-#Day)/100,100). COMPUTE #Year = (Date-100*#Month-#Day)/1E4. NUMERIC SPSSDate (DATE11). COMPUTE SPSSDate =DATE.DMY(#Day,#Month,#Year). LIST. ===================== 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 |
In reply to this post by Bruce Weaver
> On Saturday, October 19, 2013 12:02 AM, Bruce Weaver <[hidden email]> wrote:
> > Hi Art. See if this example helps. It assumes the date strings are all 8 > characters in length, as in the example you gave. If that is not so, the > syntax will have to be made more general. > > * Read in some sample data. > new file. > dataset close all. > data list list / StringVar (A8). > begin data > 20120825 > 20120926 > 20121027 > end data. > > * The following uses some code borrowed from > * the Date & Time Wizard. > > * Pull out YYYY-MM-DD components. > * Here, I assume you actually want to store them as separate variables. > COMPUTE Year = number(substr(ltrim(StringVar),1,4),f4.0). > COMPUTE Month = number(substr(ltrim(StringVar),5,2),f2.0). > COMPUTE Day = number(substr(ltrim(StringVar),7,2),f2.0). > * I assume you really want a true date variable at the end of the day. > COMPUTE DateVar = date.dmy(Day,Month,Year). > FORMATS DateVar (DATE11). > VARIABLE WIDTH DateVar(11). > LIST. > > * If you don't need the YYYY-MMM-DD components, you > * could use this syntax pasted from the Date & Time Wizard > * but with things lined up in a way that makes it > * easier to follow (when viewed in fixed font, at least). Agree, so using scratch variables instead might be a good compromise: COMPUTE #Year = number(substr(ltrim(StringVar),1,4),f4.0). COMPUTE #Month = number(substr(ltrim(StringVar),5,2),f2.0). COMPUTE #Day = number(substr(ltrim(StringVar),7,2),f2.0). COMPUTE DateVar = date.dmy(#Day, #Month, #Year). FORMATS DateVar (SDATE10). /* the lesser known sortable date, ISO, but still SPSS-ian. ===================== 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 |
At 07:40 AM 10/19/2013, Albert-Jan Roskam wrote:
>[Calculation] using scratch variables instead might be a good compromise: > >COMPUTE #Year = number(substr(ltrim(StringVar),1,4),f4.0). >COMPUTE #Month = number(substr(ltrim(StringVar),5,2),f2.0). >COMPUTE #Day = number(substr(ltrim(StringVar),7,2),f2.0). >COMPUTE DateVar = date.dmy(#Day, #Month, #Year). > >FORMATS DateVar (SDATE10). /* the lesser known sortable date, ISO, >but still SPSS-ian. That could give the impression that SPSS-form dates with other formats are *not* sortable. Changing the format from, say, DATE11. to SDATE10., changes how the values are *displayed*, but not how they are represented internally. All SPSS-form dates are sortable. ===================== 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 |
In reply to this post by Albert-Jan Roskam
Thank you, everyone, for the assistance. For now the most expedient is the simple syntax Richard provided. I will try the more sophisticated and elegant syntax at a later date--right now, expedience is the "thing", unfortunately.
Arthur Arthur Kramer, PhD Director of Institutional Research New Jersey City University -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Albert-Jan Roskam Sent: Saturday, October 19, 2013 7:40 AM To: [hidden email] Subject: Re: creating string variable from a numberic variable > On Saturday, October 19, 2013 12:02 AM, Bruce Weaver <[hidden email]> wrote: > > Hi Art. See if this example helps. It assumes the date strings are > > all 8 > characters in length, as in the example you gave. If that is not so, > the syntax will have to be made more general. > > * Read in some sample data. > new file. > dataset close all. > data list list / StringVar (A8). > begin data > 20120825 > 20120926 > 20121027 > end data. > > * The following uses some code borrowed from > * the Date & Time Wizard. > > * Pull out YYYY-MM-DD components. > * Here, I assume you actually want to store them as separate variables. > COMPUTE Year = number(substr(ltrim(StringVar),1,4),f4.0). > COMPUTE Month = number(substr(ltrim(StringVar),5,2),f2.0). > COMPUTE Day = number(substr(ltrim(StringVar),7,2),f2.0). > * I assume you really want a true date variable at the end of the day. > COMPUTE DateVar = date.dmy(Day,Month,Year). > FORMATS DateVar (DATE11). > VARIABLE WIDTH DateVar(11). > LIST. > > * If you don't need the YYYY-MMM-DD components, you > * could use this syntax pasted from the Date & Time Wizard > * but with things lined up in a way that makes it > * easier to follow (when viewed in fixed font, at least). Agree, so using scratch variables instead might be a good compromise: COMPUTE #Year = number(substr(ltrim(StringVar),1,4),f4.0). COMPUTE #Month = number(substr(ltrim(StringVar),5,2),f2.0). COMPUTE #Day = number(substr(ltrim(StringVar),7,2),f2.0). COMPUTE DateVar = date.dmy(#Day, #Month, #Year). FORMATS DateVar (SDATE10). /* the lesser known sortable date, ISO, but still SPSS-ian. ===================== 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 ===================== 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 |
Free forum by Nabble | Edit this page |