<?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>philihp.com &#187; For loops</title>
	<atom:link href="http://www.philihp.com/blog/tag/for-loops/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.philihp.com/blog</link>
	<description>I do things, and then I tell the internet about them.</description>
	<lastBuildDate>Mon, 06 Feb 2012 05:40:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>For loops in SAS</title>
		<link>http://www.philihp.com/blog/2009/for-loops-in-sas/</link>
		<comments>http://www.philihp.com/blog/2009/for-loops-in-sas/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 16:06:22 +0000</pubDate>
		<dc:creator>philihp</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[For loops]]></category>
		<category><![CDATA[List iteration]]></category>
		<category><![CDATA[Metadata]]></category>
		<category><![CDATA[SAS]]></category>

		<guid isPermaLink="false">http://www.philihp.com/blog/?p=490</guid>
		<description><![CDATA[When coding for loops in SAS, one neat thing to remember is that all of the parts of it are optional. start &#60;TO stop&#62; &#60;BY increment&#62; &#60;WHILE(expression) &#124; UNTIL(expression)&#62; http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000201276.htm So in another language, what might be for&#40;int i=0;i &#60; max;i++&#41; &#123; In SAS, you can leave out the &#8220;by 1&#8243;, since it&#8217;s the default [...]]]></description>
			<content:encoded><![CDATA[<p>When coding for loops in SAS, one neat thing to remember is that all of the parts of it are optional.</p>
<blockquote><p>start &lt;TO stop&gt; &lt;BY increment&gt; &lt;WHILE(expression) | UNTIL(expression)&gt;</p>
<p><a href="http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000201276.htm"><cite>http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000201276.htm</cite></a></p></blockquote>
<p>So in another language, what might be</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>i <span style="color: #339933;">&lt;</span> max<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></pre></div></div>

<p>In SAS, you can leave out the &#8220;by 1&#8243;, since it&#8217;s the default increment. So this would be</p>

<div class="wp_syntax"><div class="code"><pre class="sas" style="font-family:monospace;"><span style="color: #0000ff;">do</span> i=<span style="color: #2e8b57; font-weight: bold;">0</span> to <span style="color: #0000ff;">max</span>;</pre></div></div>

<p>What&#8217;s fun is in SAS you can combine a <a href="http://en.wikipedia.org/wiki/Do_while_loop">Repeat-Until/Do-While</a> loop with a for loop. This might be useful if for some reason you don&#8217;t set the var &#8220;max&#8221; until you&#8217;ve iterated through the loop once, such as in this case, where &#8220;<a href="http://support.sas.com/documentation/cdl/en/lrmeta/60739/HTML/default/getnobj.htm">metadata_getnobj</a>&#8221; returns a negative on fail, but otherwise returns the number of objects that match the query.</p>

<div class="wp_syntax"><div class="code"><pre class="sas" style="font-family:monospace;"><span style="color: #0000ff;">do</span> i=<span style="color: #2e8b57; font-weight: bold;">1</span> <span style="color: #0000ff;">by</span> <span style="color: #2e8b57; font-weight: bold;">1</span> <span style="color: #0000ff;">until</span> <span style="color: #66cc66;">&#40;</span>i &gt;= objs<span style="color: #66cc66;">&#41;</span>;
  rc=metadata_getnobj<span style="color: #66cc66;">&#40;</span><span style="color: #a020f0;">&quot;omsobj:SASLibrary?@Libref='MYLIBREF'&quot;</span>,i,uri<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #0000ff;">if</span> rc&gt;=<span style="color: #2e8b57; font-weight: bold;">0</span> <span style="color: #0000ff;">then</span> objs=rc;
&nbsp;
  rc=metadata_getnasn<span style="color: #66cc66;">&#40;</span>uri,<span style="color: #a020f0;">&quot;LibraryConnection&quot;</span>,<span style="color: #2e8b57; font-weight: bold;">1</span>,uri<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #0000ff;">if</span> rc&gt;=<span style="color: #2e8b57; font-weight: bold;">0</span> <span style="color: #0000ff;">then</span> <span style="color: #0000ff;">leave</span>;
<span style="color: #0000ff;">end</span>;</pre></div></div>

<p>This code loops through all the libraries in the metadata with libref=&#8217;MYLIBREF&#8217;, and exits with a URI for the first one it sees with at least one LibraryConnection association. This is useful when you have a remote library pointing to a local library and they both have the same name and you want a URI to the remote one.</p>
<p>Another cool thing with FOR loops in SAS is iteration through lists. In Java, say you have a block like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> x <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;a&quot;</span>,<span style="color: #0000ff;">&quot;b&quot;</span>,<span style="color: #0000ff;">&quot;c&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;x=&quot;</span><span style="color: #339933;">+</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In SAS, this is simply</p>

<div class="wp_syntax"><div class="code"><pre class="sas" style="font-family:monospace;"><span style="color: #0000ff;">do</span> <span style="color: #0000ff;">x</span>=<span style="color: #a020f0;">&quot;a&quot;</span>,<span style="color: #a020f0;">&quot;b&quot;</span>,<span style="color: #a020f0;">&quot;c&quot;</span>;
  <span style="color: #0000ff;">put</span> <span style="color: #0000ff;">x</span>=;
<span style="color: #0000ff;">end</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.philihp.com/blog/2009/for-loops-in-sas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

