<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dammit Jim! &#187; SAS</title>
	<atom:link href="http://scott.sherrillmix.com/blog/tag/sas/feed/" rel="self" type="application/rss+xml" />
	<link>http://scott.sherrillmix.com/blog</link>
	<description>I'm a biologist not a...</description>
	<lastBuildDate>Mon, 06 Feb 2012 05:19:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Getting Help with SAS</title>
		<link>http://scott.sherrillmix.com/blog/programmer/getting-help-with-sas/</link>
		<comments>http://scott.sherrillmix.com/blog/programmer/getting-help-with-sas/#comments</comments>
		<pubDate>Thu, 15 Nov 2007 06:01:35 +0000</pubDate>
		<dc:creator>ScottS-M</dc:creator>
				<category><![CDATA[Programmer]]></category>
		<category><![CDATA[SAS]]></category>
		<category><![CDATA[Statistician]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[group]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[questions]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://scott.sherrillmix.com/blog/programmer/getting-help-with-sas/</guid>
		<description><![CDATA[There was some discussion in one of my SAS posts about where to find SAS help and communities. It seemed like a pretty useful topic so I thought I&#8217;d expand it a bit and make a post out of it. First, let me say I&#8217;m not the most knowledgeable since I&#8217;m more of a find-wall-bang-head [...]]]></description>
			<content:encoded><![CDATA[<img src="/res/images/sas_source.png" alt="SAS source code" class="right"/>

<p>There was some <a href="http://scott.sherrillmix.com/blog/programmer/sas-lag-problems/#comment-14004">discussion</a> in one of my SAS posts about where to find SAS help and communities. It seemed like a pretty useful topic so I thought I&#8217;d expand it a bit and make a post out of it. First, let me say I&#8217;m not the most knowledgeable since I&#8217;m more of a find-wall-bang-head type of programmer but I did my best to dig up some possible answers. If anyone has any other suggestions, feel free to leave them in the comments.</p> 

<ul>
<li>To start with, there&#8217;s always the official online <a href="http://support.sas.com/documentation/onlinedoc/91pdf/index.html">documentation</a> although this tends to be more for polishing something you already know how to do than starting cold.</li>
<li>Speaking of official, there&#8217;s also the official <a href="http://support.sas.com/forums/index.jspa">SAS forums</a>. I didn&#8217;t know about these until I started looking around for this post so I can&#8217;t say much about them but the topics they have available seem rather specific and I can&#8217;t figure out where one would go to post a basic question.</li>
<li><ins datetime="2007-11-17T18:23:59+00:00"><i>Edit:</i>There&#8217;s also the <a href="http://support.sas.com/resources/">SAS Knowledge Base</a> that has a lot of good papers and notes detailing SAS features complete with sample code and explanations. It&#8217;s really useful if you&#8217;re a learn by example type. (Thanks to <a href="http://blogs.sas.com/sascom">Alison</a> for pointing this one out).</ins></li>
<li><a href="http://scott.sherrillmix.com/blog/programmer/sas-lag-problems/#comment-15278">Kelly Levoyer</a> of SAS points out <a href="http://www.sascommunity.org/wiki/Main_Page">SAScommunity.org</a> which seems like it is a little sparse but does have a surprisingly long list of <a href="http://www.sascommunity.org/wiki/Category:Bloggers_Corner">SAS-related blogs</a>.</li>
<li>The SAS company also appears to have jumped on the <a href="http://blogs.sas.com/">blogging band wagon</a> although really only <a href="http://blogs.sas.com/sasdummy/">SAS Dummy</a> looks helpful for learning SAS at the moment.</li>
<li>The only place that seem to be available for asking general question is the <a href="http://www.listserv.uga.edu/archives/sas-l.html">SAS-L email list</a> (which I just found out is the same as the <a href="http://groups.google.com/group/comp.soft-sys.sas/about">comp.soft-sys.sas</a> Usenet group). There&#8217;s a nice paper on <a href="http://www2.sas.com/proceedings/sugi29/247-29.pdf">SAS-L etiquette</a> (mostly do your homework first) (found via the sascommunity site).</li>
</ul>

<p>Offline, there are also <a href="http://support.sas.com/usergroups/">SAS user groups</a>. I often get emails from our local one but I&#8217;ve never actually gone. The SAS company also has trainers that travel and teach quick classes. Our university stats department brought in one to teach a couple short two-day classes about statistical functions and macros. The classes were pretty good although I&#8217;m not sure how much it cost or how frequent they are. It might be worth checking on if you&#8217;re near a university.</p>

<p>Finally, you can also read my poor attempts at explaining <a href="http://scott.sherrillmix.com/blog/programmer/sas-macros/">SAS macro variables</a> and <a href="http://scott.sherrillmix.com/blog/programmer/sas-macros-letting-sas-do-the-typing/">SAS macros</a>. Also, if you have any specific questions you can try asking in the comments here and if it&#8217;s not too time consuming I&#8217;ll try to lend a hand.</p>]]></content:encoded>
			<wfw:commentRss>http://scott.sherrillmix.com/blog/programmer/getting-help-with-sas/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SAS Macros: Letting SAS Do the Typing</title>
		<link>http://scott.sherrillmix.com/blog/programmer/sas-macros-letting-sas-do-the-typing/</link>
		<comments>http://scott.sherrillmix.com/blog/programmer/sas-macros-letting-sas-do-the-typing/#comments</comments>
		<pubDate>Sun, 04 Nov 2007 08:09:31 +0000</pubDate>
		<dc:creator>ScottS-M</dc:creator>
				<category><![CDATA[Programmer]]></category>
		<category><![CDATA[SAS]]></category>
		<category><![CDATA[Statistician]]></category>
		<category><![CDATA[ampersand]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[do loop]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[std]]></category>
		<category><![CDATA[syput]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[variable]]></category>

		<guid isPermaLink="false">http://scott.sherrillmix.com/blog/programmer/sas-macros-letting-sas-do-the-typing/</guid>
		<description><![CDATA[I've been meaning to write up a bit on using macros in SAS to complement my previous post on macro variables for quite a while. Luckily Norwegian guy reminded me about the pain of starting programming in SAS and provided me some motivation. So here's my take on using macros in programming. So what is [...]]]></description>
			<content:encoded><![CDATA[<p>I've been meaning to write up a bit on using macros in SAS to complement my previous post on <a href="http://scott.sherrillmix.com/blog/programmer/sas-macros/">macro variables</a> for quite a while. Luckily <a href="http://scott.sherrillmix.com/blog/programmer/sas-lag-problems/#comment-14002">Norwegian guy</a> reminded me about the pain of starting programming in SAS and provided me some motivation. So here's my take on using macros in programming.</p>

<p>So what is a macro? Macros are a part of SAS that look through your code before the normal part of SAS sees it and writes out your code for you based on a special syntax. If you've ever found yourself copying and pasting code then you've probably been in a situation well suited for macros. They're also great if you need to perform different functions under different conditions. Once I learned macros, SAS seemed a lot more like a usable (although weird) programming language and tasks seemed to get a lot easier (except actually picking the statistical techniques to use).</p>

<p>Probably the easiest way to see what macros do is an example. So say we once again have a data set of tree heights</p>

<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-8">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">data</span> trees;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">input</span> name:$<span style="color: #2e8b57; font-weight: bold;color:#800000;">8</span>. height;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cards;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Maple <span style="color: #2e8b57; font-weight: bold;color:#800000;">123</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Maple <span style="color: #2e8b57; font-weight: bold;color:#800000;">78</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Maple <span style="color: #2e8b57; font-weight: bold;color:#800000;">90</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Elm <span style="color: #2e8b57; font-weight: bold;color:#800000;">155</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Elm <span style="color: #2e8b57; font-weight: bold;color:#800000;">65</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Elm <span style="color: #2e8b57; font-weight: bold;color:#800000;">90</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Elm <span style="color: #2e8b57; font-weight: bold;color:#800000;">120</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Birch <span style="color: #2e8b57; font-weight: bold;color:#800000;">100</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Birch <span style="color: #2e8b57; font-weight: bold;color:#800000;">30</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Maple <span style="color: #2e8b57; font-weight: bold;color:#800000;">111</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>; </div></li></ol></div>
</div></div><br />

<p>I already talked about how to find and use the mean and standard deviation for the <a href="http://scott.sherrillmix.com/blog/programmer/sas-macros/"> whole data set</a>. Now what if we wanted to standardize each species by its own seperate mean and deviation? We could cut and paste but once we get a few more species or want to change something later this really becomes a hassle. So this is where macros come in.</p>

<p>The first thing to do is to calculate the mean and standard deviations for each species. We can use <code>proc means</code> again to do this. Since we won't be using the output I'll add the <code>noprint</code> option and since we only want the means for the individual species and not the whole dataset I'll add the <code>nway</code> option. The <code>class name;</code> statement tells SAS to find the statistics seperately for each species and the <code>output</code> line tells SAS to save the mean and deviation in a dataset called <code>meansd</code>.</p>
<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-9">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">proc means</span> <span style="color: #000080; font-weight: bold;">data</span>=trees nway noprint;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class name;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">var</span> height;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">output</span> out=meansd <span style="color: #0000ff;">mean</span>=meanheight <span style="color: #0000ff;">std</span>=sdheight;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>; </div></li></ol></div>
</div></div><br />

<p>Now we just need to get the values from the <code>meansd</code> dataset into macro variables. We'll use the _NULL_ dataset and <code>call symput</code> again to create macro variables. This time we need to create seperate macro variables for each species. Luckily SAS automatically numbers each observation in a dataset in a column called <code>_N_</code>. Since each line of the dataset corresponds to a tree species, we can easily use this identifier to create the macro variables by using <code>call symput(&#039;mean&#039;||left(_N_), meanheight);</code>. The <code>left()</code> and <code>()trim</code> functions (numeric variables have extra spaces to the left and string variables have spaces to the right) removes any unnecessary spaces and the <code>||</code> concatenates (connects) the text "mean" with the line number to give give <code>mean1</code>, <code>mean2</code>, etc.. I'll do the same thing for standard deviation and tree name. Once the macro variables are created, there is still one problem remaining. We don't know how many species there were or how many macro variables were created. Luckily SAS will make another column that indicates the last line of the dataset when it sees <code>end=newcolumnname</code> following a set statement. Then we just need to check if SAS is on the last line and if so save the line number (<code>_N_</code>) to know the number of species of trees.</p>  

<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-10">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">data</span> <span style="color: #0000ff;">_NULL_</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">set</span> meansd <span style="color: #0000ff;">end</span>=last;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">call</span> symput<span style="color: #66cc66;">&#40;</span><span style="color: #a020f0;">'mean'</span>||left<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">_N_</span><span style="color: #66cc66;">&#41;</span>,meanheight<span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">call</span> symput<span style="color: #66cc66;">&#40;</span><span style="color: #a020f0;">'sd'</span>||left<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">_N_</span><span style="color: #66cc66;">&#41;</span>,sdheight<span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">call</span> symput<span style="color: #66cc66;">&#40;</span><span style="color: #a020f0;">'name'</span>||left<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">_N_</span><span style="color: #66cc66;">&#41;</span>,<span style="color: #0000ff;">trim</span><span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">if</span> last <span style="color: #0000ff;">then</span> <span style="color: #0000ff;">call</span> symput<span style="color: #66cc66;">&#40;</span><span style="color: #a020f0;">'numspecies'</span>,<span style="color: #0000ff;">_N_</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>; </div></li></ol></div>
</div></div><br />

<p>If you ever want to check what macro variables you have in your program, you can use <code>%PUT _USER_;</code> to print them all to the log file. Or if you want to see every macro variable available  (SAS has quite a few automatic ones like operating system and date) use <code>%PUT _ALL_;</code>. Inserting <code>%PUT _USER_;</code> here produces:</p>
<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-11">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">GLOBAL NUMSPECIES&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">3</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">GLOBAL NAME1 Birch</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">GLOBAL NAME2 Elm</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">GLOBAL NAME3 Maple</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">GLOBAL MEAN1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">65</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">GLOBAL MEAN2&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">107</span>.<span style="color: #2e8b57; font-weight: bold;color:#800000;">5</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">GLOBAL MEAN3&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">100</span>.<span style="color: #2e8b57; font-weight: bold;color:#800000;">5</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">GLOBAL SD1 <span style="color: #2e8b57; font-weight: bold;color:#800000;">49</span>.<span style="color: #2e8b57; font-weight: bold;color:#800000;">497474683</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">GLOBAL SD2 <span style="color: #2e8b57; font-weight: bold;color:#800000;">38</span>.<span style="color: #2e8b57; font-weight: bold;color:#800000;">837267326</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">GLOBAL SD3 <span style="color: #2e8b57; font-weight: bold;color:#800000;">20</span>.<span style="color: #2e8b57; font-weight: bold;color:#800000;">273134933</span> </div></li></ol></div>
</div></div><br />
<p>Now we've set a lot of macro variables but we still haven't created a real macro. In SAS, macros are started with <code>%MACRO macroname;</code> and finished with <code>%MEND;</code> (short for M[acro]END). <code>%</code>'s are used to indicate commands that the SAS macro facility will read and remove before normal SAS sees the code. Anything not with a % will be printed out by the macro facility. Macros don't spit out their code for SAS until they're are called using <code>%macroname</code>.</p>

<p>So I'll call my macro <code>treestandardizer</code> but you can call it whatever you want. I'm going to use a pretty simple and specific macro but if you were going to use this often and for different datasets you would want to program it better. The first thing to do is create the <code>final</code> dataset and set it to the <code>trees</code> dataset. Since we need to loop through each species of tree, we'll need a <code>%DO</code> loop. Everything between <code>%DO</code> and <code>%END</code> will be repeated while <code>i</code> increments from 1 to the number of tree species.  If you want to combine text and a macro variable to reference another macro variable, you use the double ampersand <code>&amp;&amp;<!--formatted--></code> in SAS. For example, we want to get the mean for species 1 by looking in the macro variable <code>&amp;mean1<!--formatted--></code> so we use <code>&amp;&amp;mean&amp;i<!--formatted--></code>. I <em>think</em> the macro processing part of SAS ends up running through the code twice, the first time finding the <code>&amp;&amp;<!--formatted--></code> and replacing it with <code>&amp;<!--formatted--></code> and the <code>&amp;i<!--formatted--></code> and replacing it with <code>1</code> to leave <code>&amp;mean1<!--formatted--></code> and the second time finding <code>&amp;mean1<!--formatted--></code> and pasting in the appropriate value (65). So we'll have the do loop write out a series of <code>if</code> statements to check what the name of the tree is and use the appropriate mean and deviation. Note that when using a string macro variable like <code>&amp;nameX<!--formatted--></code>, you need to surround it with double quotes (the macro processor doesn't look inside single quotes) so SAS doesn't think it is a variable name. </p>

<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-12">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">%MACRO</span> treestandardizer;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">data</span> final;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">set</span> trees;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">%DO</span> i = <span style="color: #2e8b57; font-weight: bold;color:#800000;">1</span> <span style="color: #0000ff;">%TO</span> <span style="color: #0000ff; font-weight: bold;">&amp;numspecies</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">if</span> name=<span style="color: #a020f0;">"&amp;&amp;name&amp;i"</span> <span style="color: #0000ff;">then</span> stheight=<span style="color: #66cc66;">&#40;</span>height-&amp;<span style="color: #0000ff; font-weight: bold;">&amp;mean</span><span style="color: #0000ff; font-weight: bold;">&amp;i</span><span style="color: #66cc66;">&#41;</span>/&amp;<span style="color: #0000ff; font-weight: bold;">&amp;sd</span><span style="color: #0000ff; font-weight: bold;">&amp;i</span>; </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">%END</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">%MEND</span>; </div></li></ol></div>
</div></div><br />

<p>The previous code prepared the macro but nothing actually happens until we call it using <code>%treestandardizer</code>. Unlike almost everything else in SAS this line doesn't have to end in a semicolon (although it's pretty unlikely to hurt if you forget and add one). So to call the macro:</p>
<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-13">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">%treestandardizer </div></li></ol></div>
</div></div><br />

<p>If you want to see what happens when you call a macro, you can have SAS print the code generated by the macro to the log file with the option <code>option mprint;</code> (make sure to set it before actually calling the macro). In this case, it gives:</p>
<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-14">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MPRINT<span style="color: #66cc66;">&#40;</span>TREESTANDARDIZER<span style="color: #66cc66;">&#41;</span>:&nbsp; &nbsp;<span style="color: #000080; font-weight: bold;">data</span> final;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MPRINT<span style="color: #66cc66;">&#40;</span>TREESTANDARDIZER<span style="color: #66cc66;">&#41;</span>:&nbsp; &nbsp;<span style="color: #0000ff;">set</span> trees;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MPRINT<span style="color: #66cc66;">&#40;</span>TREESTANDARDIZER<span style="color: #66cc66;">&#41;</span>:&nbsp; &nbsp;<span style="color: #0000ff;">if</span> name=<span style="color: #a020f0;">"Birch"</span> <span style="color: #0000ff;">then</span> stheight=<span style="color: #66cc66;">&#40;</span>height- <span style="color: #2e8b57; font-weight: bold;color:#800000;">65</span><span style="color: #66cc66;">&#41;</span>/<span style="color: #2e8b57; font-weight: bold;color:#800000;">49</span>.<span style="color: #2e8b57; font-weight: bold;color:#800000;">497474683</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MPRINT<span style="color: #66cc66;">&#40;</span>TREESTANDARDIZER<span style="color: #66cc66;">&#41;</span>:&nbsp; &nbsp;<span style="color: #0000ff;">if</span> name=<span style="color: #a020f0;">"Elm"</span> <span style="color: #0000ff;">then</span> stheight=<span style="color: #66cc66;">&#40;</span>height- <span style="color: #2e8b57; font-weight: bold;color:#800000;">107</span>.<span style="color: #2e8b57; font-weight: bold;color:#800000;">5</span><span style="color: #66cc66;">&#41;</span>/<span style="color: #2e8b57; font-weight: bold;color:#800000;">38</span>.<span style="color: #2e8b57; font-weight: bold;color:#800000;">837267326</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MPRINT<span style="color: #66cc66;">&#40;</span>TREESTANDARDIZER<span style="color: #66cc66;">&#41;</span>:&nbsp; &nbsp;<span style="color: #0000ff;">if</span> name=<span style="color: #a020f0;">"Maple"</span> <span style="color: #0000ff;">then</span> stheight=<span style="color: #66cc66;">&#40;</span>height- <span style="color: #2e8b57; font-weight: bold;color:#800000;">100</span>.<span style="color: #2e8b57; font-weight: bold;color:#800000;">5</span><span style="color: #66cc66;">&#41;</span>/<span style="color: #2e8b57; font-weight: bold;color:#800000;">20</span>.<span style="color: #2e8b57; font-weight: bold;color:#800000;">273134933</span>; </div></li></ol></div>
</div></div><br />

<p>So it worked and we now have the standardized heights in the <code>stheight</code> column of the <code>final</code> dataset. This particular example could be done a few different ways (the easiest and probably better way being to merge the <code>meancv</code> dataset with the <code>trees</code>) but I hope it gives a decent introduction to SAS macros. If you have any specific questions or something wasn't clear, feel free to ask in a comment.</p> 

<p>Here is the <a href="/res/SAS_macro_example.sas">SAS source code</a> if you don't feel like copying and pasting.</p>

]]></content:encoded>
			<wfw:commentRss>http://scott.sherrillmix.com/blog/programmer/sas-macros-letting-sas-do-the-typing/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>SAS lag problems</title>
		<link>http://scott.sherrillmix.com/blog/programmer/sas-lag-problems/</link>
		<comments>http://scott.sherrillmix.com/blog/programmer/sas-lag-problems/#comments</comments>
		<pubDate>Wed, 27 Sep 2006 23:07:23 +0000</pubDate>
		<dc:creator>ScottS-M</dc:creator>
				<category><![CDATA[Programmer]]></category>
		<category><![CDATA[SAS]]></category>
		<category><![CDATA[Statistician]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[caution]]></category>
		<category><![CDATA[conditional]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[difference]]></category>
		<category><![CDATA[if statement]]></category>
		<category><![CDATA[lag]]></category>
		<category><![CDATA[problem]]></category>

		<guid isPermaLink="false">http://scott.sherrillmix.com/blog/programmer/sas-lag-problems/</guid>
		<description><![CDATA[I just found an interesting (that's interesting in the 'I just spent an hour debugging that?' sense) characteristic in SAS. If you have a variable, x, and are using the lag of x, do NOT put the lag(x) inside a conditional statement. This can apparently cause some pretty strange results. It is probably easier to [...]]]></description>
			<content:encoded><![CDATA[<p>I just found an interesting (that's interesting in the 'I just spent an hour debugging <em>that</em>?' sense) characteristic in SAS. If you have a variable, <code>x</code>, and are using the <code>lag</code> of <code>x</code>, do NOT put the <code>lag(x)</code> inside a conditional statement. This can apparently cause some pretty strange results. It is probably easier to see through code than to explain:</p>

<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-18">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">data</span> test;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">input</span> id$<span style="color: #2e8b57; font-weight: bold;color:#800000;">1</span> <span style="color: #0000ff;">x</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cards;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; a <span style="color: #2e8b57; font-weight: bold;color:#800000;">1</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; a <span style="color: #2e8b57; font-weight: bold;color:#800000;">2</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; a <span style="color: #2e8b57; font-weight: bold;color:#800000;">3</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; b <span style="color: #2e8b57; font-weight: bold;color:#800000;">12</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; b <span style="color: #2e8b57; font-weight: bold;color:#800000;">13</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; b <span style="color: #2e8b57; font-weight: bold;color:#800000;">15</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">data</span> test;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">set</span> test;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; lagid=<span style="color: #0000ff;">lag</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #0000ff;">if</span> lagid=id <span style="color: #0000ff;">then</span> <span style="color: #0000ff;">do</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lagx=<span style="color: #0000ff;">lag</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">x</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; duration=x-lagx;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #0000ff;">end</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">proc print</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>; </div></li></ol></div>
</div></div><br />

<p>And here are the results:</p>

<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-19">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Obs&nbsp; &nbsp; id&nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">x</span>&nbsp; &nbsp; lagid&nbsp; &nbsp; lagx&nbsp; &nbsp; duration</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #2e8b57; font-weight: bold;color:#800000;">1</span>&nbsp; &nbsp; &nbsp;a&nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">1</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #2e8b57; font-weight: bold;color:#800000;">2</span>&nbsp; &nbsp; &nbsp;a&nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">2</span>&nbsp; &nbsp; &nbsp; a&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #2e8b57; font-weight: bold;color:#800000;">3</span>&nbsp; &nbsp; &nbsp;a&nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">3</span>&nbsp; &nbsp; &nbsp; a&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">2</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">1</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #2e8b57; font-weight: bold;color:#800000;">4</span>&nbsp; &nbsp; &nbsp;b&nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">12</span>&nbsp; &nbsp; &nbsp; a&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #2e8b57; font-weight: bold;color:#800000;">5</span>&nbsp; &nbsp; &nbsp;b&nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">13</span>&nbsp; &nbsp; &nbsp; b&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">3</span>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">10</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #2e8b57; font-weight: bold;color:#800000;">6</span>&nbsp; &nbsp; &nbsp;b&nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">15</span>&nbsp; &nbsp; &nbsp; b&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">13</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">2</span> </div></li></ol></div>
</div></div><br />

<p>There are obviously some strange things going on here. I have no idea what is going on in the 2nd row of the results where <code>lagx</code> is missing when it should be 1. And in the 5th row, <code>lagx</code> is the 3 from 2 rows above when it should be 12. Luckily, the solution is easy. Just execute the lag every time:</p>

<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-20">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">data</span> test;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">set</span> test;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; lagid=<span style="color: #0000ff;">lag</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; lagx=<span style="color: #0000ff;">lag</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">x</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #0000ff;">if</span> lagid=id <span style="color: #0000ff;">then</span> <span style="color: #0000ff;">do</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; duration=x-lagx;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #0000ff;">end</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>; </div></li></ol></div>
</div></div><br />

<p>Anyway, it took me two years to run into this so I guess this isn't a common mistake but if you're ever having trouble with a <code>lag</code>, check to make sure it is executed for every line of the data.</p>]]></content:encoded>
			<wfw:commentRss>http://scott.sherrillmix.com/blog/programmer/sas-lag-problems/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>SAS Macro Variables: How to Take a Mean in SAS</title>
		<link>http://scott.sherrillmix.com/blog/programmer/sas-macros/</link>
		<comments>http://scott.sherrillmix.com/blog/programmer/sas-macros/#comments</comments>
		<pubDate>Sat, 16 Sep 2006 02:03:52 +0000</pubDate>
		<dc:creator>ScottS-M</dc:creator>
				<category><![CDATA[Programmer]]></category>
		<category><![CDATA[SAS]]></category>
		<category><![CDATA[Statistician]]></category>
		<category><![CDATA[column]]></category>
		<category><![CDATA[decimal]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[mean]]></category>
		<category><![CDATA[symput]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[variable]]></category>

		<guid isPermaLink="false">http://scott.sherrillmix.com/blog/programmer/sas-macros/</guid>
		<description><![CDATA[I just thought I'd do a quick post about SAS macro variables. They're a pretty important part of SAS but when I had just started people told me not to worry about them and to just enter values by hand. After I finally got tired of constantly looking up results and entering them into programs [...]]]></description>
			<content:encoded><![CDATA[<p>I just thought I'd do a quick post about SAS macro variables. They're a pretty important part of SAS but when I had just started people told me not to worry about them and to just enter values  by hand. After I finally got tired of constantly looking up results and entering them into programs over and over again, I decided I had better figure out how real programmers did it. As a side note, I've had a couple people tell me SAS was dying out but I see it appears to be in the <a href="http://www.tiobe.com/tpci.htm">top 20 of programming languages</a> so I guess it's going strong.</p>

<p>So SAS is very good at working on rows of data but not quite so good at working with columns. For example if you have a column of tree heights and want to standardize by the mean and standard deviation, there is no easy way to do this without using macros or manually getting the mean and entering it into your program (this works the first couple times but it gets old quickly trust me).</p>

<p>Macros seem to me to be sort of a patch that sits on top of the main SAS program to allow this sort of thing. Macros work by scanning through your program for special commands and replacing things before sending it onto the real SAS program. In effect, the macro processor writes your program for you.</p>

<p>So back to our example of trees. Here is some data:</p>
<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-25">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">data</span> trees;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">input</span> name $<span style="color: #2e8b57; font-weight: bold;color:#800000;">1</span>-<span style="color: #2e8b57; font-weight: bold;color:#800000;">8</span> height <span style="color: #2e8b57; font-weight: bold;color:#800000;">9</span>-<span style="color: #2e8b57; font-weight: bold;color:#800000;">11</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cards;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Maple&nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">123</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Oak&nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">78</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Birch&nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">90</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Elm&nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">155</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Poplar&nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">65</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>; </div></li></ol></div>
</div></div><br />
<p>I never actually use the <code>cards</code> statements in real code but it's handy for portable examples. Also the spaces between the two columns are significant since we told SAS to look the 1st through 8th column (<code>$1-8</code>) for tree name</p>
<p>So we now need to find the mean and s.d. of the heights:</p>
<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-26">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">proc means</span> <span style="color: #000080; font-weight: bold;">data</span>=trees <span style="color: #0000ff;">mean</span> <span style="color: #0000ff;">std</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">var</span> height;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">output</span> out=meansd <span style="color: #0000ff;">mean</span>=meanheight <span style="color: #0000ff;">std</span>=sdheight;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>; </div></li></ol></div>
</div></div><br />
<p>Now our mean is stored in the <code>meanheight</code> and <code>sdheight</code> columns of the <code>meantrees</code> dataset. So to get it into the macro part of SAS we need to use <code>call symput(anyname,column)</code>. You can only use <code>call symput</code> in data statements. This is a little silly since we don't actually want to do anything with the data but I don't make the rules. Luckily, SAS does provide the <code>_NULL_</code> dataset which just dumps whatever you put in it after you finish the data step. SAS variables often have extra spaces attached so it's probably smart to stick a <code>trim(left(column))</code> in the <code>call symput</code>.</p>
<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-27">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">data</span> <span style="color: #0000ff;">_null_</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">set</span> meansd;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">call</span> symput<span style="color: #66cc66;">&#40;</span><span style="color: #a020f0;">'treemean'</span>,<span style="color: #0000ff;">trim</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">left</span><span style="color: #66cc66;">&#40;</span>meanheight<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">call</span> symput<span style="color: #66cc66;">&#40;</span><span style="color: #a020f0;">'treesd'</span>,<span style="color: #0000ff;">trim</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">left</span><span style="color: #66cc66;">&#40;</span>sdheight<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>; </div></li></ol></div>
</div></div><br />
<p>So after <code>call symput</code> we can access the stored values with <code>&amp;treemean<!--formatted--></code> and <code>&amp;treesd<!--formatted--></code>. They're called macro variables by the way. If you ever need to check the contents of your macro variables you can use <code>%PUT _users_;</code> to put them all into the log file. So now we can make a column of our standardized tree heights:</p>
<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-28">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">data</span> trees;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">set</span> trees;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">standardized=<span style="color: #66cc66;">&#40;</span>height-<span style="color: #0000ff; font-weight: bold;">&amp;treemean</span><span style="color: #66cc66;">&#41;</span>/<span style="color: #0000ff; font-weight: bold;">&amp;treesd</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>; </div></li></ol></div>
</div></div><br />
<p>Now the <code>trees</code> dataset contains the standardized tree heights. This is a lot easier than looking in the results and entering it manually every time something changes.</p>]]></content:encoded>
			<wfw:commentRss>http://scott.sherrillmix.com/blog/programmer/sas-macros/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

