{"id":904,"date":"2010-12-08T01:20:29","date_gmt":"2010-12-08T06:20:29","guid":{"rendered":"http:\/\/www.thejuliagroup.com\/blog\/?p=904"},"modified":"2010-12-08T01:24:59","modified_gmt":"2010-12-08T06:24:59","slug":"nesting-local-and-global-variables-in-sas-macros","status":"publish","type":"post","link":"https:\/\/www.thejuliagroup.com\/blog\/nesting-local-and-global-variables-in-sas-macros\/","title":{"rendered":"Nesting, Local and Global Variables in SAS Macros"},"content":{"rendered":"<p>People who are following the reverb10 blogs would no doubt be disappointed by today&#8217;s post because it has absolutely nothing to do with finding my compass in life, unless life&#8217;s compass is possibly nested in a SAS macro. Since I started this blog to write down things I was sure I would want to remember later, today&#8217;s post fits my life (or, at least blog) goal exactly.<\/p>\n<p>Today, I decided to RTFM.<\/p>\n<p>Various statistical applications differ in their style for manuals. Stata is okay to use for getting statistical  results but the documentation tends to be very terse.<\/p>\n<blockquote><p>&#8220;These  are numbers. Take the integral of the factorial of the determinant of  the matrix which results from the inverse of the product of covariance  matrix and the Y vector from the equation you will be solving this time  next year. Then the universe explodes and you have your answer.&#8221;<\/p><\/blockquote>\n<p>SAS  goes in the other direction, pretty much starting each chapter with the  discovery of the number two by a Neanderthal named Og, proceeding  through proofs going back to Euclid and up to the latest Joint  Statistical Meetings.<\/p>\n<p>Most of the SPSS documentation I have seen was  really basic.<\/p>\n<blockquote><p>One is a number. Two is a bigger number. Statistics uses numbers like one and two. These are what statisticians refer to as &#8216;numbers&#8217;.<\/p><\/blockquote>\n<p>I seem to write a lot about SAS and I was planning on broadening my horizons, but I happened to be interested in macros today &#8230; I&#8217;ve used SAS macros as needed for years, even took a course a decade or so ago, read articles to solve problems as I came across them, but never read the manual from beginning to end, so I figured I missed a few things here and there. So, this afternoon, I sat down and started at page 1. I ran across this bit about nesting, global and local macros and I had to think for a minute until it was obvious.<\/p>\n<p>The points being made were that:<\/p>\n<p>1. There are global macro variables and local macro variables. Global variables are available at any time during your program. Local variables are only available during the macro in which they are created BUT &#8230;<\/p>\n<p>2. If a variable by a given name is already created, and then you use that same variable name within a macro, it will not create a new one, it will replace the value in the existing variable.<\/p>\n<p>Here is what it said in the manual<\/p>\n<p>&#8220;The same rule applies regardless of how many levels of nesting exist. Consider the following example:<br \/>\n%let new=inventry;<br \/>\n<strong>%macro conditn;<\/strong><br \/>\n%let old=sales;<br \/>\n%let cond=cases&gt;0;<br \/>\n<strong>%mend conditn;<\/strong><br \/>\n<strong>%macro name3;<\/strong><br \/>\n%let new=report;<br \/>\n%let old=warehse;<br \/>\n%conditn<br \/>\ndata &amp;new;<br \/>\nset &amp;old;<br \/>\nif &amp;cond;<br \/>\nrun;<br \/>\n<strong>%mend name3;<\/strong><br \/>\n%name3<br \/>\nThe macro processor generates these statements:<br \/>\ndata report;<br \/>\nset sales;<br \/>\nif &amp;cond;<br \/>\nrun;\u00a0 &#8221;<\/p>\n<p>My first thought was &#8211; huh? Then I realized<\/p>\n<ul>\n<li>OK, <em>&amp;new<\/em> is already created as a global macro variable before the macro, %name3, even exists.<\/li>\n<li>When %name3 has a %LET statement using a variable named <em>&amp;new<\/em>, it simply changes the name of the existing macro variable.<\/li>\n<li>This is the part that threw me at first &#8212; the macro variable <em>&amp;old<\/em> is created within the macro %name3.\u00a0 So it is local to that macro. We are still in %name3, so we have the variable <em>&amp;old<\/em> available to us.<\/li>\n<li>The macro %conditn executes. It CHANGES the value of the variable <em>&amp;old<\/em>. It also creates an <em>&amp;cond<\/em> variable which evaporates as soon as the macro ends because it is a strictly local variable to %conditn.<\/li>\n<li>When %name3 executes we have access to variable <em>&amp;new<\/em> as it is a global variable. It has the value to which it was changed in this macro. We have access to the variable <em>&amp;old <\/em>as it is a variable local to the macro that is executing right now, name3. It has the value that it was changed to by macro %conditn, which executed before our DATA step. Not being able to resolve the reference to &amp;cond it simply produces text that says &#8220;&amp;cond&#8221; .<\/li>\n<\/ul>\n<p>What have we learned here?<\/p>\n<p>The first, most obvious piece of advice is not to re-use the same macro variable names within a program unless you really do want the global or nested macro variable to be changing the value. I mean, it&#8217;s not like we&#8217;re short on potential variable names here. They don&#8217;t even have to be actual words. You can name your macro variable <em>ushnakatz <\/em> if you want.<\/p>\n<p>The second is to be aware of how SAS processes macro variables. I can see uses where I might want to create a variable and then change it within a macro so that I can have one macro that generates reports on sales, another on inventory, etc. and uses all the same procedures otherwise. Something like that %conditn macro, but working.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>People who are following the reverb10 blogs would no doubt be disappointed by today&#8217;s post because it has absolutely nothing to do with finding my compass in life, unless life&#8217;s compass is possibly nested in a SAS macro. Since I started this blog to write down things I was sure I would want to remember&#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-904","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\/904","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=904"}],"version-history":[{"count":3,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts\/904\/revisions"}],"predecessor-version":[{"id":907,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/posts\/904\/revisions\/907"}],"wp:attachment":[{"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/media?parent=904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/categories?post=904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thejuliagroup.com\/blog\/wp-json\/wp\/v2\/tags?post=904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}