{"id":2407,"date":"2012-05-14T02:54:59","date_gmt":"2012-05-14T07:54:59","guid":{"rendered":"http:\/\/www.thejuliagroup.com\/blog\/?p=2407"},"modified":"2012-05-14T03:01:18","modified_gmt":"2012-05-14T08:01:18","slug":"getting-only-numbers-in-sas-and-javascript","status":"publish","type":"post","link":"https:\/\/www.thejuliagroup.com\/blog\/getting-only-numbers-in-sas-and-javascript\/","title":{"rendered":"Getting only numbers, in SAS and Javascript"},"content":{"rendered":"<p>The advantage of learning a new language is it sometimes makes you re-think the old languages you know. For example, here is a problem that happens often:<\/p>\n<p><strong>Some people are morons.<\/strong><\/p>\n<p>For example, say I were to ask you the following question:<\/p>\n<blockquote><p>&#8220;How old are you?&#8221;<\/p><\/blockquote>\n<p>YOU would probably answer something like, 42 or 21. You didn&#8217;t mistake that for an essay question, now, did you? That, my dear reader, is because YOU are not a moron. However, trust me when I tell you that other people are not as smart as you.<\/p>\n<p>A rather annoying percentage of people enter responses along the lines of:<\/p>\n<p>I am 47 years old.<\/p>\n<p>I just turned 21. Happy Birthday to me.<\/p>\n<p>36 years<\/p>\n<p>87 (yes, eighty-seven)<\/p>\n<p>54 yrs.<\/p>\n<p>and so on &#8230;.<\/p>\n<p>Just using the sub-string function to read in the first two characters won&#8217;t work, obviously.<\/p>\n<p>Well, I was doing something in javascript where I asked the person their age and then stripped off everything but numbers before I tried to use the age they had given me, like so:<br \/>\n<strong>var age = prompt(&#8220;How old are you?&#8221;) ;<\/strong><br \/>\n<strong> var ageyears = age.replace(\/[\\D]\/g, &#8221;);<\/strong><br \/>\nUsually, in my SAS programs, I would either just define age as a numeric variable and all of those who included text had their values set to missing. Or, if I wanted to minimize missing data, I would write a statement \u00a0to just read in the first two characters, or maybe to strip out &#8220;years&#8221; and &#8220;yrs&#8221;. However, in the latest data set I have, it seems to be a sample of people who are creatively annoying, so I had to settle for a lot of missing data or do something else. I got to thinking that there MUST be some function in SAS that does something similar.<\/p>\n<div><\/div>\n<div><\/div>\n<p>Well, wouldn&#8217;t you know &#8230;.<\/p>\n<div>You may be familiar with the COMPRESS function, which is most commonly used for removing trailing blanks. The COMPRESS function can be used to remove any characters from a variable, but it turns out that you can also use it to keep characters, as so:<\/div>\n<div><code><br \/>\n<strong>Age_numonly = compress(age,'0123456789','K');<\/strong><\/code><\/div>\n<div><\/div>\n<div><\/div>\n<p>Having the &#8216;K&#8217; at the end reverses what the COMPRESS function normally does and instead of deleting your numbers it keeps them. I don&#8217;t know how I did not know this. Maybe I knew it at one point and forgot it? Be sure you have the &#8216;K&#8217; in quotes, by the way.<\/p>\n<p>Well, now I have it stored in my blog, which is better than having it in memory, because unlike my memory, this blog gets backed up regularly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The advantage of learning a new language is it sometimes makes you re-think the old languages you know. For example, here is a problem that happens often: Some people are morons. For example, say I were to ask you the following question: &#8220;How old are you?&#8221; YOU would probably answer something like, 42 or 21&#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],"tags":[],"class_list":["post-2407","post","type-post","status-publish","format-standard","hentry","category-software"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts\/2407","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=2407"}],"version-history":[{"count":4,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts\/2407\/revisions"}],"predecessor-version":[{"id":2409,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts\/2407\/revisions\/2409"}],"wp:attachment":[{"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/media?parent=2407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/categories?post=2407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/tags?post=2407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}