<?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>t+1 &#187; SAS</title>
	<atom:link href="http://blog.tplus1.com/index.php/category/sas/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tplus1.com</link>
	<description>Programming, gardening, economics, life in Cleveland Heights</description>
	<lastBuildDate>Sat, 07 Jan 2012 21:12:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to use indexes with SAS datasets</title>
		<link>http://blog.tplus1.com/index.php/2008/01/22/how-to-use-indexes-with-sas-datasets/</link>
		<comments>http://blog.tplus1.com/index.php/2008/01/22/how-to-use-indexes-with-sas-datasets/#comments</comments>
		<pubDate>Tue, 22 Jan 2008 20:15:50 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SAS]]></category>

		<guid isPermaLink="false">http://blog.tplus1.com/index.php/2008/01/22/how-to-use-indexes-with-sas-datasets/</guid>
		<description><![CDATA[I just came across some old SAS tutorials that I wrote a few years ago, so I&#8217;ll be posting them here when I&#8217;ve got nothing else to say.
Indexes are an alternative to sorting your dataset or building a format. They speed up any where or by processing.  
Creating indexes
You can create an index in [...]]]></description>
			<content:encoded><![CDATA[<p><i>I just came across some old SAS tutorials that I wrote a few years ago, so I&#8217;ll be posting them here when I&#8217;ve got nothing else to say</i>.</p>
<p>Indexes are an alternative to sorting your dataset or building a format. They speed up any <code>where</code> or <code>by</code> processing.  </p>
<h2><a name="creatingindexes">Creating indexes</a></h2>
<p>You can create an index in a data step like this:<br />
<pre><code>
data clm2 (index=(mbrno empssn mi=(mbrno iyymm) ei=(empssn iyymm)));
&nbsp;&nbsp;&nbsp;&nbsp;set clm1;
run;
</code></pre><br />
The mi and ei are compound indexes, which behave as if you sorted your dataset mbrno iyymm or by empssn iyymm.</p>
<p>You can use proc datasets to add an index to a dataset that already exists:<br />
<pre><code>
proc datasets library=saslib;
&nbsp;&nbsp;&nbsp;&nbsp;modify clm2;
&nbsp;&nbsp;&nbsp;&nbsp;index create mbrno empssn mi=(mbrno iyymm) ei=(empssn iyymm);
</code></pre></p>
<h2><a href="usingindexes">Using indexes</a></h2>
<p>Indexes allow you to merge datasets that aren&#8217;t sorted.  In the above example, now you can use clm2 just like it was sorted by any of the indexed vars:<br />
<pre><code>
data clm_plus_elig;
&nbsp;&nbsp;&nbsp;&nbsp;merge clm2 mem;
&nbsp;&nbsp;&nbsp;&nbsp;by empssn iyymm;
run;
</code></pre></p>
<p>This is another example of how to do a lookup.<br />
<pre><code>
data d1;
&nbsp;&nbsp;&nbsp;&nbsp;infile datalines;
&nbsp;&nbsp;&nbsp;&nbsp;input col1 8.;
datalines;
&nbsp;&nbsp;&nbsp;&nbsp;101
&nbsp;&nbsp;&nbsp;&nbsp;106
&nbsp;&nbsp;&nbsp;&nbsp;102
&nbsp;&nbsp;&nbsp;&nbsp;102
&nbsp;&nbsp;&nbsp;&nbsp;103
&nbsp;&nbsp;&nbsp;&nbsp;103
&nbsp;&nbsp;&nbsp;&nbsp;104
&nbsp;&nbsp;&nbsp;&nbsp;105
;

data d2 (index=(col1));
&nbsp;&nbsp;&nbsp;&nbsp;infile datalines;
&nbsp;&nbsp;&nbsp;&nbsp;input col1 col2 $8.;
datalines;
&nbsp;&nbsp;&nbsp;&nbsp;104 ddd
&nbsp;&nbsp;&nbsp;&nbsp;102 bbb
&nbsp;&nbsp;&nbsp;&nbsp;103 ccc
&nbsp;&nbsp;&nbsp;&nbsp;101 aaa
;

data d3;
&nbsp;&nbsp;&nbsp;&nbsp;set d1;
&nbsp;&nbsp;&nbsp;&nbsp;set d2 key=col1 / unique;

&nbsp;&nbsp;&nbsp;&nbsp;/* This block handles bad lookups. */
&nbsp;&nbsp;&nbsp;&nbsp;if _IORC_ eq %sysrc(_DSENOM) then do;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;col2 = &quot;xxx&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;end;

run;

proc print heading=h data=d3;
run;
</code></pre><br />
And this is the output:<br />
<pre><code>
this is the d3 output dataset.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 14:43 Friday, March 11, 2005&nbsp;&nbsp; 1

Obs&nbsp;&nbsp;&nbsp;&nbsp;col1&nbsp;&nbsp;&nbsp;&nbsp;col2

 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;101&nbsp;&nbsp;&nbsp;&nbsp;aaa
 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;106&nbsp;&nbsp;&nbsp;&nbsp;xxx
 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;102&nbsp;&nbsp;&nbsp;&nbsp;bbb
 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;102&nbsp;&nbsp;&nbsp;&nbsp;bbb
 5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;103&nbsp;&nbsp;&nbsp;&nbsp;ccc
 6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;103&nbsp;&nbsp;&nbsp;&nbsp;ccc
 7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;104&nbsp;&nbsp;&nbsp;&nbsp;ddd
 8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;105&nbsp;&nbsp;&nbsp;&nbsp;xxx
</code></pre></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tplus1.com/index.php/2008/01/22/how-to-use-indexes-with-sas-datasets/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

