{"id":5624,"date":"2018-07-25T10:55:15","date_gmt":"2018-07-25T15:55:15","guid":{"rendered":"http:\/\/www.thejuliagroup.com\/blog\/?p=5624"},"modified":"2018-07-25T10:55:15","modified_gmt":"2018-07-25T15:55:15","slug":"converting-dates-from-character-to-numeric-from-excel-to-sas","status":"publish","type":"post","link":"https:\/\/www.thejuliagroup.com\/blog\/converting-dates-from-character-to-numeric-from-excel-to-sas\/","title":{"rendered":"Converting dates: from character to numeric, from Excel to SAS"},"content":{"rendered":"<p>I&#8217;m back with another SAS Tip of the Day. Like a lot of people, I work with dates very often.<\/p>\n<ul>\n<li>How many days is it from when a client applies to when he or she is determined eligible?<\/li>\n<li>How many days until the average client is employed?<\/li>\n<\/ul>\n<p>You get the idea. Inconveniently, in this particular case, I received the data in an Excel file and when I uploaded the file all of the dates were in character format. Here is a simple fix.<\/p>\n<ol>\n<li>Create an array of the character dates. Takes one statement. Note that you need that $ to indicate character variables.<\/li>\n<li>Create an array of your numeric dates. Takes one statement. Leave OFF the $ to indicate these are NOT character variables.<\/li>\n<li>Use a DO loop to fix any data problems, read into the new numeric variable and subtract 21,916. <a href=\"http:\/\/www2.sas.com\/proceedings\/sugi29\/068-29.pdf\">This is the number of days difference between the reference date for SAS and for Excel. You can read more about that here<\/a>.<\/li>\n<li>Not required but good practice , since I was not going to use the character date values, I dropped those from the data set as well as the j subscript variable.<\/li>\n<\/ol>\n<p><code>data fixdata ;<br \/>\nset fix1;<br \/>\narray chardates {9} $ birthdate date_app date_assess date_eligible date_ipe date_closed\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>date_employ date_int_completed date_last_contact<br \/>\n;<br \/>\narray numdates {9} date_birth app_date assess_date eligible_date ipe_date closed_date employ_date<br \/>\nint_completed_date last_contact_date<br \/>\n;<br \/>\n** Change all date variables to exclude invalid dates ;<br \/>\n** And from Excel to SAS date format ;<\/code><code><br \/>\ndo j = 1 to 9 ;<br \/>\nif chardates{j} = \"0000-00-00\" then numdates{j} = . ;<br \/>\nelse numdates{j} = chardates{j} - 21916 ;<br \/>\nend;<br \/>\ndrop j birthdate date_app date_assess date_eligible date_ipe date_closed date_employ\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 date_int_completed date_last_contact;<\/code><\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>I live in opposite world. I blog on SAS and statistics for fun and make games for a living. <a href=\"http:\/\/www.7generationgames.com\/making-camp-premium\/\">Check out Making Camp Premium. Learn about Ojibwe culture, brush up your math skills, learn more English and have fun. All for under two bucks.<\/a><\/p>\n<p><a href=\"http:\/\/www.thejuliagroup.com\/blog\/wp-content\/uploads\/2018\/07\/making_camp-e1532445025357.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-5620\" src=\"http:\/\/www.thejuliagroup.com\/blog\/wp-content\/uploads\/2018\/07\/making_camp-300x300.png\" alt=\"Making Camp scene with buffalo, deer and rabbit\" width=\"300\" height=\"300\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m back with another SAS Tip of the Day. Like a lot of people, I work with dates very often. How many days is it from when a client applies to when he or she is determined eligible? How many days until the average client is employed? You get the idea. Inconveniently, in this particular&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9,8],"tags":[],"class_list":["post-5624","post","type-post","status-publish","format-standard","hentry","category-software","category-technology"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts\/5624","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/comments?post=5624"}],"version-history":[{"count":2,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts\/5624\/revisions"}],"predecessor-version":[{"id":5626,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts\/5624\/revisions\/5626"}],"wp:attachment":[{"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/media?parent=5624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/categories?post=5624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/tags?post=5624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}