<?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; mean</title>
	<atom:link href="http://scott.sherrillmix.com/blog/tag/mean/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>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-5">
<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-6">
<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-7">
<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-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;">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>

