{"id":2083,"date":"2012-01-27T17:02:45","date_gmt":"2012-01-27T22:02:45","guid":{"rendered":"http:\/\/www.thejuliagroup.com\/blog\/?p=2083"},"modified":"2012-01-27T17:02:45","modified_gmt":"2012-01-27T22:02:45","slug":"random-basic-sas-tips-on-ranuni-sampling-with-replacement-junk","status":"publish","type":"post","link":"https:\/\/www.thejuliagroup.com\/blog\/random-basic-sas-tips-on-ranuni-sampling-with-replacement-junk\/","title":{"rendered":"Random basic SAS tips on ranuni, sampling with replacement &#038; junk"},"content":{"rendered":"<p><strong>1. Use the data sets in the sashelp directory for dummy data.<\/strong><\/p>\n<p><strong>2. The RANUNI function is worth remembering<\/strong><\/p>\n<p>Today I needed to check something. \u00a0Specifically, I was using the ranuni function to generate random numbers for sampling with replacement. I wanted the data set to be sorted randomly, select the first match, then re-sort the data and re-sample. (Yes, obviously, I was doing propensity score matching.)<\/p>\n<p>When you use a 0 for the seed, e.g.,<\/p>\n<p>randnum = ranuni(0) ;<\/p>\n<p>SAS actually uses the time of day. I was not sure to how many seconds, micro-seconds, nano-seconds or whatever it rounds the time. Even if I did, I&#8217;m not sure that would have helped me because I wanted to know is the rounding factor small enough that if my program is running with a small data set there is already a new seed by the next step. I *thought* so but you know, there is a reason we do testing. \u00a0It&#8217;s because things don&#8217;t always come out the way you think.<\/p>\n<p>I was going to create a dummy data set and then I realized, hey! There are all kinds of data sets already in the sashelp directory. You may never have looked at them, or noticed, but they \u00a0are there. So, I did this :<\/p>\n<p><code>Data yes ;<br \/>\nset sashelp.air ;<br \/>\nrandnum = ranuni(0) ;<br \/>\nrun ;<br \/>\nproc means data = yes ;<br \/>\nvar randnum ;<\/code><\/p>\n<p><code><br \/>\nData yes ;<br \/>\nset yes ;<br \/>\nrandnum = ranuni(0) ;<br \/>\nproc means data = yes ;<br \/>\nvar randnum ;<br \/>\nrun ;<\/code><\/p>\n<p>I did the PROC MEANS because I was too lazy to open the two data sets and look at the numbers. Yes, it worked. I expected it would.<\/p>\n<p><strong>3. Avoid the unconditional ELSE statement.<\/strong><\/p>\n<p>This is a habit I got into years ago. I&#8217;m not one for giving other people rules for how to code because I think most of those rules given out as gospel are just one of many acceptable ways of doing things. This one, however, is worth remembering. Say you have two possible conditions, experimental and control. You would *think* it would make sense to type<\/p>\n<p><code>If group = 1 then output experiment ;<\/code><\/p>\n<p><code>else \u00a0output control ;<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>It <em>would<\/em> make sense to you if you have never met any actual people. There are data entry errors, where someone types 11 or the letter &#8220;l&#8221; instead of a 1. People type &#8220;experiment&#8221; instead of 1. They leave that field blank because they don&#8217;t know if this person was in the experimental or control group. All of those people end up in the control group. The technical term statisticians use for this state of affairs is &#8220;bad&#8221;.<\/p>\n<p>Instead, at a \u00a0minimum, do this.<\/p>\n<p><code>If group = 1 then output experiment ;<\/code><br \/>\n<code><br \/>\nelse if group = 0 then output control ;<\/code><\/p>\n<p>I worked with someone who had a good habit of always creating one more data set than he needed, he named it junk. Then, \u00a0at the end of every IF statement that sent data to different groups was this<\/p>\n<p><code>If group = 1 then output experiment ;<\/code><\/p>\n<p><code>else if group = 0 then output control ;<\/p>\n<p>else output junk ;<\/code><\/p>\n<p>Not only did your 1s only go to the experimental group and your 0s only go to the control group but you also had a dataset that collected the junk where you could look at who these people were and try to figure out what their problem was. Personally, I don&#8217;t do that as a routine, but it is a good habit.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Use the data sets in the sashelp directory for dummy data. 2. The RANUNI function is worth remembering Today I needed to check something. \u00a0Specifically, I was using the ranuni function to generate random numbers for sampling with replacement. I wanted the data set to be sorted randomly, select the first match, then re-sort&#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-2083","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\/2083","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=2083"}],"version-history":[{"count":1,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts\/2083\/revisions"}],"predecessor-version":[{"id":2084,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts\/2083\/revisions\/2084"}],"wp:attachment":[{"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/media?parent=2083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/categories?post=2083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/tags?post=2083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}