{"id":199,"date":"2013-05-20T07:21:02","date_gmt":"2013-05-20T07:21:02","guid":{"rendered":"http:\/\/localhost:8080\/spss-tricks_00\/?p=199"},"modified":"2016-05-23T15:27:22","modified_gmt":"2016-05-23T15:27:22","slug":"convert-numeric-to-date-variable","status":"publish","type":"post","link":"https:\/\/www.spss-tutorials.com\/convert-numeric-to-date-variable\/","title":{"rendered":"Convert Numeric to Date Variable"},"content":{"rendered":"<!--body-->\n\n<p>For time calculations (such as the number of days between two dates) proper date variables are needed. In some cases, the digits of a numeric variable may represent year, month and day. This tutorial shows how to convert this format into an actual date variable.<\/p>\n\n\n<h2>Convert Numbers to SPSS Date Variables<\/h2>\n\n<p>In some cases, dates are represented as numeric variables whose digits represent a year, month and day (often referred to as YYYY-MM-DD). This format is not suitable for any time based calculations which require an actual <a href=\"https:\/\/www.spss-tutorials.com\/spss-date-variables-tutorial\/\">date variables<\/a>. The solution for this is to convert the original variable into an actual date variable. This is demonstrated by the <a href=\"https:\/\/www.spss-tutorials.com\/spss-syntax\/ \">syntax<\/a> below.<\/p>\n\n\n<h2>SPSS Date Function Syntax Example<\/h2>\n\n<div class='code'><strong>*1. Create test data.<br><\/strong><br>data list free\/date(f8).<br>begin data <br>20111030<br>end data.<br><br><strong>*2. Convert into date format.<br><\/strong><br>compute realdate = date.dmy(mod(date,10**2),tru(mod(date,10**4)\/10**2),tru(date\/10**4)).<br>format realdate(edate10).<br>exe.<\/div><!--class='code'-->\n    \n<h2>SPSS Date Function Explanation<\/h2>\n\n<ul>\n    <li>In SPSS, a date variable holds the number of seconds since (roughly) 1582.<\/li>\n    <li>For any (day, month, year) combination, the function <code>DATE.DMY<\/code> calculates this number.<\/li>\n    <li>It will be displayed as a normal date if its <code>FORMAT<\/code> is set (for example) to <code>EDATE10<\/code>.<\/li>\n    <li>For the example data, the day, month and year correspond to the last, middle and first digits of the number.<\/li>\n    <li>These are extracted by combining <code>TRUNCATE<\/code> and <code>MODULUS<\/code>.<\/li>\n    <li>Like so, new variables holding the day, month and year can be created and these can be used in the <code>DATE.DMY<\/code> function.<\/li>\n    <li>Alternatively, the syntax extracting day, month and year can be placed directly into <code>DATE.DMY<\/code> as is done here.<\/li>\n<\/ul>\n\n\n\n\n","protected":false},"excerpt":{"rendered":"<p>For time calculations (such as the number of days between two dates) proper date variables are needed. In some cases, the digits of a numeric variable may represent year, month and day. This tutorial shows how to convert this format into an actual date variable.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"class_list":["post-199","post","type-post","status-publish","format-standard","hentry","category-spss-date-conversions"],"_links":{"self":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts\/199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/comments?post=199"}],"version-history":[{"count":0,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts\/199\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/media?parent=199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/categories?post=199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/tags?post=199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}