{"id":4850,"date":"2015-11-02T12:44:11","date_gmt":"2015-11-02T17:44:11","guid":{"rendered":"http:\/\/www.thejuliagroup.com\/blog\/?p=4850"},"modified":"2015-11-02T13:41:15","modified_gmt":"2015-11-02T18:41:15","slug":"sas-tip-preventing-disaster-when-variable-lengths-differ","status":"publish","type":"post","link":"https:\/\/www.thejuliagroup.com\/blog\/sas-tip-preventing-disaster-when-variable-lengths-differ\/","title":{"rendered":"SAS Tip: Preventing Disaster When Variable Lengths Differ"},"content":{"rendered":"<p>Over the weekend, <a href=\"http:\/\/www.thejuliagroup.com\/blog\/?p=4847\">I wrote a post showing how SAS can be used to make what appears to be a complex problem quite simple<\/a>.<\/p>\n<div><\/div>\n<div>First of all, am I just being dramatic? Seriously, how can having your variable lengths differ be a disaster?<\/div>\n<div><\/div>\n<div><\/div>\n<p>Simple. You are merging by a variable that is a unique user identifier like username, social security number. Because the two different data sets have different lengths, they do not match. If you are computing the number of unique users you may overestimate by a huge amount. If you want the number of people who are in both data sets, you may vastly underestimate the amount of true matches.<\/p>\n<p>As with anything in programming, there are many ways to do this. My solution is to create a new variable and set it to the identical length and format using the ATTRIB statement. Extra bonus is this will work when you have variables that are not only different lengths but different types, say character in one data set and numeric in the other.<\/p>\n<p>You really only need two statements in your data step, an ATTRIB statement and then an assignment statement that sets the value of the variable you created to whatever the variable is you want to merge.<\/p>\n<p>DATA dsname ;<br \/>\nATTRIB newvar LENGTH = $49 ;<br \/>\nSET mydata2.dsname ;<br \/>\nnewvar = oldvar ;<br \/>\nRepeat this step for the second data set and then merge (or concatenate) to \u00a0your little heart&#8217;s delight.<\/p>\n<p><a href=\"http:\/\/www.thejuliagroup.com\/blog\/wp-content\/uploads\/2015\/07\/guardfacesm.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft wp-image-4700 size-full\" src=\"http:\/\/www.thejuliagroup.com\/blog\/wp-content\/uploads\/2015\/07\/guardfacesm.jpg\" alt=\"Angry guard face\" width=\"142\" height=\"184\" \/><\/a><strong>The voice of experience:\u00a0<\/strong><br \/>\nNotice two things here: \u00a0I created a temporary data set from my permanent one. Although SAS has gotten more forgiving over the years in not writing over your existing data sets when there is an error, it is still better to err on the side of caution and make sure all is wonderful before saving over that existing data, especially if it took you a lot of effort to get the data in that form.<br \/>\nSecond, I created a new variable and kept the old one as is. I don&#8217;t always do this but it is good practice. You may be tempted to just use the first 9 digits because we all know social security numbers are 9 digits and then later you find that it was entered as 123-45-6789 \u00a0and now you only have 123-45-67<\/p>\n<p><strong>&#8212;- Feel smarter after reading this blog?<\/strong><\/p>\n<p><a href=\"http:\/\/www.thejuliagroup.com\/blog\/wp-content\/uploads\/2014\/10\/Fish-Lake-Poster.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-4303\" src=\"http:\/\/www.thejuliagroup.com\/blog\/wp-content\/uploads\/2014\/10\/Fish-Lake-Poster-300x85.jpg\" alt=\"Fish Lake artwork\" width=\"300\" height=\"85\" srcset=\"https:\/\/www.thejuliagroup.com\/blog\/wp-content\/uploads\/2014\/10\/Fish-Lake-Poster-300x85.jpg 300w, https:\/\/www.thejuliagroup.com\/blog\/wp-content\/uploads\/2014\/10\/Fish-Lake-Poster.jpg 947w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<div>\n<p><a href=\"http:\/\/www.7generationgames.com\/buy\/\">Want to feel even smarter? Download and play our games! <\/a>\u00a0You can run around in our virtual world while reviewing your basic math skills. If you are too busy (seriously?) you can still give a game as a gift or donate a game to a classroom or school.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Over the weekend, I wrote a post showing how SAS can be used to make what appears to be a complex problem quite simple. First of all, am I just being dramatic? Seriously, how can having your variable lengths differ be a disaster? Simple. You are merging by a variable that is a unique user&#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-4850","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\/4850","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=4850"}],"version-history":[{"count":6,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts\/4850\/revisions"}],"predecessor-version":[{"id":4857,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts\/4850\/revisions\/4857"}],"wp:attachment":[{"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/media?parent=4850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/categories?post=4850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/tags?post=4850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}