{"id":2725,"date":"2012-11-04T05:17:48","date_gmt":"2012-11-04T10:17:48","guid":{"rendered":"http:\/\/www.thejuliagroup.com\/blog\/?p=2725"},"modified":"2018-02-18T20:53:39","modified_gmt":"2018-02-19T01:53:39","slug":"i-feel-a-macro-coming-on-part-2","status":"publish","type":"post","link":"https:\/\/www.thejuliagroup.com\/blog\/i-feel-a-macro-coming-on-part-2\/","title":{"rendered":"I feel a macro coming on: Part 2 positional, optional &#038; default parameters"},"content":{"rendered":"<p>Yesterday and the day before, I gave an example of using SAS to sort student responses into the class they were in using the DATEPART function, TIMEPART function and a few DO-loops. After making sure my code runs, I decided it was pretty redundant and thus a classic case for a macro.<\/p>\n<p>What I want to do is follow this logic, based on the class schedule in the computer lab<\/p>\n<p>If it is day X, and between times A and B, then it is teacher number one&#8217;s class.<\/p>\n<p>Else, if it is day Y and between times C and D, then it teacher number two&#8217;s class.<\/p>\n<p>The problem is\u00a0 a bit complicated by the fact that it is not the same number of classes each day. Sometime it&#8217;s one, sometimes two and sometimes three. It is not always the same order, that is Mary&#8217;s class does not always follow Samir&#8217;s class. Also, the class lengths are not the same. The middle school classes are longer than elementary school.\u00a0 Here is the code, to be explained below<\/p>\n<p>options mprint ;<br \/>\n%macro sched(the_day,start1,finish1,teacher1,start2=0, finish2=0, teacher2=&#8221; &#8220;, start3=0,finish3=0,\u00a0 teacher3=&#8221; &#8220;);<br \/>\nif date_data = &amp;the_day then do ;<br \/>\nif minutes &gt; &amp;start1 &amp; minutes &lt; &amp;finish1 then tclass = &amp;teacher1 ;<br \/>\nelse if (&amp;start2 &gt; 0) &amp; (minutes &gt; &amp;start2) &amp; minutes &lt; &amp;finish2\u00a0 then tclass = &amp;teacher2 ;<br \/>\nelse if &amp;start3 &gt; 0\u00a0 &amp; (minutes &gt; &amp;start3) &amp; minutes &lt; &amp;finish3 then tclass = &amp;teacher3 ;<br \/>\nend ;<br \/>\n%mend sched ;<\/p>\n<p>Data coded ;<br \/>\nattrib tclass length = $19 ;<br \/>\nset in.studentdata ;<br \/>\nset SASUSER.QUERY_FOR_INPUTFORMDATA ;<br \/>\n%sched(19290,800,850,&#8221;Flintstone&#8221;,start2= 850, finish2=900, teacher2= &#8220;Rubble&#8221;);<br \/>\n%sched(19292,790,840,&#8221;Elmo&#8221;,start2= 840, finish2=900, teacher2= &#8220;Bert&#8221;, start3=940,finish3=990, teacher3= &#8220;Snuffleupagus&#8221;);<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><em>options mprint ;<\/em><\/p>\n<p>&#8212; Prints the code generated by the macro to your log. Invaluable for de-bugging. Delete this statement after your macro is churning away.<br \/>\n<em>%macro sched(the_day,start1,finish1,teacher1,start2=0, finish2=0, teacher2=&#8221; &#8220;, start3=0,finish3=0,\u00a0 teacher3=&#8221; &#8220;);<\/em><\/p>\n<p>Names the macro, gives it four required parameters &#8211; the date, start of FIRST\u00a0 class, time class finishes and teacher name<\/p>\n<p>gives it six optional parameters, the start of the second class, finish of second class, teacher of second class, start of third class, etc.<\/p>\n<p>*** mega-important here, I am also assigning default values for each of these so later in the program it doesn&#8217;t run into a null value and throw an error.<br \/>\n<em>if date_data = &amp;the_day then do ;<\/em><br \/>\n<em> if minutes &gt; &amp;start1\u00a0 &amp;\u00a0 minutes &lt; &amp;finish1 then tclass = &amp;teacher1 ;<\/em><\/p>\n<p>If the record matches the value for day, and the start and finish times match the values given, the record is assigned to teacher1 &#8216; s class.<\/p>\n<p><em>else if (&amp;start2 &gt; 0) &amp; (minutes &gt; &amp;start2) &amp; minutes &lt; &amp;finish2\u00a0 then tclass = &amp;teacher2 ;<\/em><br \/>\n<em>\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 else if &amp;start3 &gt; 0\u00a0 &amp; (minutes &gt; &amp;start3) &amp; minutes &lt; &amp;finish3 then tclass = &amp;teacher3 ;<\/em><\/p>\n<p>For the next two possibilities, IF there is a value supplied for start2 (or start3), then the statement executes and looks for students who were logged on and answering questions during that time, and assigns their records to the appropriate teacher&#8217;s class.<br \/>\n<em>\u00a0\u00a0 \u00a0end ;<\/em><br \/>\n<em> %mend sched ;<\/em><\/p>\n<p>Here we end the DO-loop and then end the macro.<\/p>\n<p>Data coded ;<br \/>\nattrib tclass length = $19 ;<br \/>\nset in.studentdata ;<br \/>\nset SASUSER.QUERY_FOR_INPUTFORMDATA ;<br \/>\n%sched(19290,800,850,&#8221;Flintstone&#8221;,start2= 850, finish2=900, teacher2= &#8220;Rubble&#8221;);<br \/>\n%sched(19292,790,840,&#8221;Elmo&#8221;,start2= 840, finish2=900, teacher2= &#8220;Bert&#8221;, start3=940,finish3=990, teacher3= &#8220;Snuffleupagus&#8221;);<\/p>\n<p>The section above creates a data set, assigns an appropriate length for teacher name and then shows two examples, calling the macro for a day with two class and another day when there were three classes. One immediate change I can see would be useful might be to change the input parameter from a SAS date value to something more comprehensible to humans. Maybe I&#8217;ll do that next week.<\/p>\n<p>Tomorrow I have a slight problem with JavaScript animation to work out.<\/p>\n<p><a href=\"https:\/\/itunes.apple.com\/us\/app\/aztech-meet-the-mayas\/id1266461721?mt=8\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5529\" src=\"http:\/\/www.thejuliagroup.com\/blog\/wp-content\/uploads\/2017\/11\/aztech_jungle.png\" alt=\"girl in jungle\" width=\"450\" height=\"301\" srcset=\"https:\/\/www.thejuliagroup.com\/blog\/wp-content\/uploads\/2017\/11\/aztech_jungle.png 450w, https:\/\/www.thejuliagroup.com\/blog\/wp-content\/uploads\/2017\/11\/aztech_jungle-300x201.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/a><\/p>\n<h3><a href=\"https:\/\/itunes.apple.com\/us\/app\/aztech-meet-the-mayas\/id1266461721?mt=8\">If you want to see what I do with JavaScript, check out our game AzTech: Meet\u00a0 the Maya , for your iPad<\/a><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday and the day before, I gave an example of using SAS to sort student responses into the class they were in using the DATEPART function, TIMEPART function and a few DO-loops. After making sure my code runs, I decided it was pretty redundant and thus a classic case for a macro. What I want&#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-2725","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\/2725","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=2725"}],"version-history":[{"count":6,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts\/2725\/revisions"}],"predecessor-version":[{"id":5587,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts\/2725\/revisions\/5587"}],"wp:attachment":[{"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/media?parent=2725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/categories?post=2725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/tags?post=2725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}