<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: When is referring to a variable before it is defined allowed in a SAS data step? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205943#M38299</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi.&amp;nbsp; The OUTER loop rules.&amp;nbsp; There's one pass through the data step since all the observations in the data set are read within the loop.&amp;nbsp; The data step still returns to the beginning one more time to see if there are any more observations to be read.&amp;nbsp; Since there are none, the data step is finished.&amp;nbsp; You can see that by modifying the code as follows and looking at the LOG (you'll see two AT START and one AT END) ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;data even;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;PUT "AT START";&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;do _n_=1 to howmany;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp; set all nobs=howmany;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp; if ^mod(x,2) then output;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;end;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;PUT "AT END";&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, there's really no reason to do what I did in my example.&amp;nbsp; Without the outer do loop, the data step would work the same way.&amp;nbsp; I only did that to show you that even though the SET statement with NOBS=HOWMANY occurs after the start of the loop that uses the variable HOWMANY, the loop still works (example of what happens prior to execution versus what happens during execution of a data step).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;One situation where you might use actually use a loop within a data step is that it's one way to separate statements that need only to be executed once rather than on each pass through the data step.&amp;nbsp; The execute once statements occur outside the loop while those executed as each observation is read occur within the loop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Another great use of loops to read data is the DOW, especially handy when looking at groups of observations with your data with BY-GROUP processing.&amp;nbsp; If you don't know about the DOW, there's no need to explain it here since there are a number of really good papers to read ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;HOW to DOW&lt;/STRONG&gt;&lt;/EM&gt; (if you have never read a Paul Dorfman paper, it's a treat)&lt;/P&gt;&lt;P&gt;&lt;A href="http://support.sas.com/resources/papers/proceedings12/156-2012.pdf" title="http://support.sas.com/resources/papers/proceedings12/156-2012.pdf"&gt;http://support.sas.com/resources/papers/proceedings12/156-2012.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;an earlier version of some material in the above ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;The DOW-Loop Unrolled&lt;/STRONG&gt;&lt;/EM&gt; (another one by Paul Dorfman, why not read the best)&lt;/P&gt;&lt;P&gt;&lt;A class="active_link" href="http://analytics.ncsu.edu/sesug/2007/SD08.pdf" title="http://analytics.ncsu.edu/sesug/2007/SD08.pdf"&gt;http://analytics.ncsu.edu/sesug/2007/SD08.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The above includes the following which is what I meant by isolating the repeated execution of some statements from others to be executed once ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;The DOW-loop (Whitlock DO-loop) is a nested repetitive DATA step programming&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;structure, intentionally organized in order to allow for programmatically and logically&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;natural isolation of DO-loop instructions related to a certain break-event from actions&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;performed before and after the loop, and without resorting to superfluous conditional&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;statements. &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just search for "SAS DOW" and you'll find more.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 26 Aug 2015 15:33:02 GMT</pubDate>
    <dc:creator>MikeZdeb</dc:creator>
    <dc:date>2015-08-26T15:33:02Z</dc:date>
    <item>
      <title>When is referring to a variable before it is defined allowed in a SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205931#M38287</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have encountered the following situation a handful of times and it has always confused me.&amp;nbsp; As I read through a datastep I notice that a variable is used before it is declared or assigned an initial value. That is, the first mention of a variable as I read the code from top to bottom is in a statement that assumes the variable already has a value. I think I remember reading that the datastep does some pre-processing, perhaps in which all variables are created, before any statements are executed.Would someone please explain when referring to a variable before it is declared is allowed in a datastep and how to correctly think about this situation. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks! &lt;/P&gt;&lt;P&gt;Adam Black&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Aug 2015 14:20:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205931#M38287</guid>
      <dc:creator>Adam_Black</dc:creator>
      <dc:date>2015-08-24T14:20:50Z</dc:date>
    </item>
    <item>
      <title>Re: When is referring to a variable before it is defined allowed in a SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205932#M38288</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;There are a lot more ways to create a variable in SAS than in other programming languages.&amp;nbsp; Perhaps this reference will help&lt;/P&gt;&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000695113.htm" title="http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000695113.htm"&gt;SAS(R) 9.2 Language Reference: Concepts, Second Edition&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Aug 2015 15:51:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205932#M38288</guid>
      <dc:creator>Doc_Duke</dc:creator>
      <dc:date>2015-08-24T15:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: When is referring to a variable before it is defined allowed in a SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205933#M38289</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;As Doc implied, that's seems like an easy question but it really isn't.&amp;nbsp; If you are at the point of " &lt;EM&gt;I think I remember reading that the data step does some pre-processing &amp;lt;more&amp;gt;"&lt;/EM&gt;, I think you should look at ... "&lt;EM&gt;&lt;STRONG&gt;Executable and Declarative Statements&lt;/STRONG&gt;&lt;/EM&gt;" ... &lt;A href="http://support.sas.com/documentation/cdl/en/lestmtsref/63323/HTML/default/viewer.htm#p10bvg3wauedhan1qly0hiokirlv.htm" title="http://support.sas.com/documentation/cdl/en/lestmtsref/63323/HTML/default/viewer.htm#p10bvg3wauedhan1qly0hiokirlv.htm"&gt;SAS(R) 9.3 Statements: Reference ... &lt;/A&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;to get some understanding of what can be done during data step execution versus what occurs prior to execution.&amp;nbsp; There's a classic paper (a bit dated but still worth a read) "&lt;EM&gt;&lt;STRONG&gt;The SAS Supervisor&lt;/STRONG&gt;&lt;/EM&gt;" ...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&lt;A href="http://www.lexjansen.com/nesug/nesug88/sas_supervisor.pdf" title="http://www.lexjansen.com/nesug/nesug88/sas_supervisor.pdf"&gt;http://www.lexjansen.com/nesug/nesug88/sas_supervisor.pdf&lt;/A&gt; ... that has a great explanation as to what happens prior to and at execution of a data step.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also worth a look is "&lt;EM&gt;&lt;STRONG&gt;DATA Step Statements by Category&lt;/STRONG&gt;&lt;/EM&gt;" ... &lt;A href="http://support.sas.com/documentation/cdl/en/lestmtsref/63323/HTML/default/viewer.htm#p158bgegfcdjpmn1axzf8mq2uode.htm" title="http://support.sas.com/documentation/cdl/en/lestmtsref/63323/HTML/default/viewer.htm#p158bgegfcdjpmn1axzf8mq2uode.htm"&gt;SAS(R) 9.3 Statements: Reference&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Aug 2015 16:43:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205933#M38289</guid>
      <dc:creator>MikeZdeb</dc:creator>
      <dc:date>2015-08-24T16:43:35Z</dc:date>
    </item>
    <item>
      <title>Re: When is referring to a variable before it is defined allowed in a SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205934#M38290</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Are you sure the variable wasn't coming from an existing data set such as a SET, MERGE or UPDATE statement?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Provide some examples of the code in question from the Data statement to the questionable line(s) of code.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Aug 2015 16:52:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205934#M38290</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2015-08-24T16:52:56Z</dc:date>
    </item>
    <item>
      <title>Re: When is referring to a variable before it is defined allowed in a SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205935#M38291</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here is an example where a variable is referred to before it is given a value (it is never given a value as evidence by "un-init" message).&lt;/P&gt;&lt;P&gt;If is implicitly declared by the SAS data step compiler.&amp;nbsp; Is this the kind of thing you are talking about?&amp;nbsp; If you could give example that would be most helpful.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;DIV style="font-family: Courier New; font-size: 11pt;"&gt;&lt;STRONG style="color: #000080; background-color: #ffffff;"&gt;data&lt;/STRONG&gt; &lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;_null_&lt;/SPAN&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; a = &lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;1&lt;/STRONG&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;put&lt;/SPAN&gt; &lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;_all_&lt;/SPAN&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;if&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; a = b &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;then&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; c=&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;2&lt;/STRONG&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;else&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; c=a;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp; t = vtype(b);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;put&lt;/SPAN&gt; &lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;_all_&lt;/SPAN&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;STRONG style="color: #000080; background-color: #ffffff;"&gt;run&lt;/STRONG&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;23&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data _null_;&lt;BR /&gt;&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;24&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a = &lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;1&lt;/STRONG&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;25&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; put _all_;&lt;BR /&gt;&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;26&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if a = b then c=&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;2&lt;/STRONG&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;27&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else c=a;&lt;BR /&gt;&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;28&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t = vtype(b);&lt;BR /&gt;&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;29&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; put _all_;&lt;BR /&gt;&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;30&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;NOTE: &lt;/SPAN&gt;&lt;SPAN style="color: #ff0000; background-color: #ffffff;"&gt;Variable&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; b is &lt;/SPAN&gt;&lt;SPAN style="color: #008080; background-color: #ffffff;"&gt;uninitialized.&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;a=&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;1&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; b=&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;.&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; c=&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;.&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; t=&amp;nbsp; _ERROR_=&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;0&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; _N_=&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;1&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;a=&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;1&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; b=&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;.&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; c=&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;1&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; t=N _ERROR_=&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;0&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; _N_=&lt;/SPAN&gt;&lt;STRONG style="color: #008080; background-color: #ffffff;"&gt;1&lt;/STRONG&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Aug 2015 18:52:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205935#M38291</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2015-08-24T18:52:49Z</dc:date>
    </item>
    <item>
      <title>Re: When is referring to a variable before it is defined allowed in a SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205936#M38292</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you for the references! I have found "The SAS Supervisor..." paper particularly helpful. I was aware of the different ways to declare variables in SAS but did not understand how to think about undeclared variables as in the following example.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data output;&lt;/P&gt;&lt;P&gt;if new_var = . then put "new_var exists but was never declared";&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It sounds like in this example the SAS supervisor creates new_var and initializes it to missing at compile time. Then the if statement is performed during execution. This seems like dangerous behavior to me. I could imagine that a typo in a variable name would be a difficult error to find since it would not create an warning. Instead SAS automatically defines a new variable.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for the help.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Aug 2015 20:29:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205936#M38292</guid>
      <dc:creator>Adam_Black</dc:creator>
      <dc:date>2015-08-24T20:29:21Z</dc:date>
    </item>
    <item>
      <title>Re: When is referring to a variable before it is defined allowed in a SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205937#M38293</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you all for your help. Here are a couple examples of what I am talking about. The simplest example is the following.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data output;&lt;/P&gt;&lt;P&gt;if new_var = . then put "new_var exists and was never declared";&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A more complicated example comes from a problem I was trying to solve involving a sohisticated merge. Imagine we have a dataset with babies and the days they were born. We also have a dataset with doctors containing flags for the days they worked at the hospital. I wanted to create a dataset that would list all the possible baby-doctor combinations such that the doctor might have delivered the baby. ie. The doctor worked on the baby's birthday. Below is the solution which I adapted from code someone posted online in response to this question.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data babies;&lt;/P&gt;&lt;P&gt;input baby_name $ birth_day birth_day_name $;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;Jake&amp;nbsp; 1 day1&lt;/P&gt;&lt;P&gt;Sonny 4 day4&lt;/P&gt;&lt;P&gt;North 5 day5&lt;/P&gt;&lt;P&gt;Apple 6 day6&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data doctors;&lt;/P&gt;&lt;P&gt;input&amp;nbsp; DrLastname $ day1 day2 day3 day4 day5 day6;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;Jones 1 0 0 1 1 1 &lt;/P&gt;&lt;P&gt;Lewis 1 1 1 0 0 1 &lt;/P&gt;&lt;P&gt;Smith 0 1 1 1 0 1 &lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data babies_doctors_array;&lt;/P&gt;&lt;P&gt;array drnames[3] $10 _temporary_;&lt;/P&gt;&lt;P&gt;array drdays[3,6] _temporary_;&lt;/P&gt;&lt;P&gt;/* load doctors dataset into temp arrays */&lt;/P&gt;&lt;P&gt;if _n_=1 then do i = 1 to nobs_doctors;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set doctors point=i nobs=nobs_doctors;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; array days day1-day6;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drnames&lt;I&gt;=DrLastname;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do j = 1 to dim(days);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drdays[i,j]=days&lt;J&gt;;&lt;/J&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* go through babies to find doctors that worked on thei birthday*/&lt;/P&gt;&lt;P&gt;set babies;&lt;/P&gt;&lt;P&gt;do k = 1 to nobs_doctors;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if drdays[k,birth_day]=1 then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; babys_doctor = drnames&lt;K&gt;;&lt;/K&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;keep baby_name birth_day babys_doctor;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc print data=babies_doctors_array; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The variable nobs_doctors is used in the do loop before the set statement in which it is declared. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The most recent case of this I've encountered that prompted me to start this discussion looks like it is a coding error to me.&amp;nbsp; Here is a really stripped down version of the code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data raw;&lt;/P&gt;&lt;P&gt;format dos date9.;&lt;/P&gt;&lt;P&gt;input id dos mmddyy. comp1 comp2 comp3;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 121299 1 0 0&lt;/P&gt;&lt;P&gt;1 121299 0 1 0&lt;/P&gt;&lt;P&gt;1 101103 0 1 0&lt;/P&gt;&lt;P&gt;2 030400 1 1 0&lt;/P&gt;&lt;P&gt;2 030400 0 0 0&lt;/P&gt;&lt;P&gt;2 040400 0 0 1&lt;/P&gt;&lt;P&gt;3 041190 0 1 0&lt;/P&gt;&lt;P&gt;4 092090 0 0 1&lt;/P&gt;&lt;P&gt;4 051589 0 1 0&lt;/P&gt;&lt;P&gt;5 040300 0 0 0&lt;/P&gt;&lt;P&gt;5 071710 1 0 0&lt;/P&gt;&lt;P&gt;5 070899 0 1 0&lt;/P&gt;&lt;P&gt;6 030299 0 1 0&lt;/P&gt;&lt;P&gt;7 121200 1 0 0&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;proc print data=raw;run;&lt;/P&gt;&lt;P&gt;proc sort data=raw;&lt;/P&gt;&lt;P&gt;by id dos;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data fin;&lt;/P&gt;&lt;P&gt;set raw;&lt;/P&gt;&lt;P&gt;by id dos;&lt;/P&gt;&lt;P&gt;/* not sure about using compsum before it is defined */&lt;/P&gt;&lt;P&gt;if compsum = 0 then no_comps = 1;&amp;nbsp; &lt;/P&gt;&lt;P&gt;compsum = sum(comp1, comp2, comp3);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This just looks like a mistake to me and illustrates why I think this behavior is dangerous. It makes this kind of coding error hard to catch.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks again for all your help.&lt;/P&gt;&lt;P&gt;-Adam&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Aug 2015 21:37:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205937#M38293</guid>
      <dc:creator>Adam_Black</dc:creator>
      <dc:date>2015-08-24T21:37:16Z</dc:date>
    </item>
    <item>
      <title>Re: When is referring to a variable before it is defined allowed in a SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205938#M38294</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi ... one comment on your statement&amp;nbsp; "&lt;EM&gt;&lt;STRONG&gt;The variable nobs_doctors is used in the do loop before the set statement in which it is declared.&lt;/STRONG&gt;&lt;/EM&gt;"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is related to COMPILE versus EXECUTION and this works fine ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;data all;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;input x @@;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;datalines;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;1 2 3 4 5 6 7 8 9 10&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;data even;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;do _n_=1 to howmany;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp; set all nobs=howmany;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp; if ^mod(x,2) then output;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;end;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;since a value is assigned to the variable HOWMANY prior to data step execution.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Aug 2015 16:39:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205938#M38294</guid>
      <dc:creator>MikeZdeb</dc:creator>
      <dc:date>2015-08-25T16:39:28Z</dc:date>
    </item>
    <item>
      <title>Re: When is referring to a variable before it is defined allowed in a SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205939#M38295</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;And about this ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;/* not sure about using compsum before it is defined */&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;if compsum = 0 then no_comps = 1; &lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;compsum = sum(comp1, comp2, comp3);&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;The statement with the SUM function is just an assignment statement (and not at all DANGEROUS) ... as explained in on-line HELP ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;Syntax&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;variable=expression;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;Arguments&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;variable&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;names a new or existing variable.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;Range variable can be a variable name, array reference, or SUBSTR function. &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;Tip Variables that are created by the Assignment statement are not automatically retained. &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;expression&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;is any SAS expression.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;Tip expression can contain the variable that is used on the left side of the equal sign. When a variable appears on both sides of a statement, the original value on the right side is used to evaluate the expression, and the result is stored in the variable on the left side of the equal sign. For more information, see Expressions in SAS Language Reference: Concepts.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Prior to the assignment statement, the value of COMPSUM is MISSING, not 0 so NO_COMPS is never assigned a value.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This works fine ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;data fin;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;set raw;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;compsum = sum(of comp:);&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Values on the RIGHT of the = are known (variables or constants).&amp;nbsp; Values on the LEFT of the = can be new or already known variables.&amp;nbsp; If you are still squeamish about declaring variables, you could always assign an initial value in a RETAIN statement (DECLARATIVE, happens once, not EXECUTABLE) ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;retain comp_sum 0;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A RETAIN statement has other consequences, but in your case you are assigning a new value with every pass through the data step and the retaining of the variable value is of no consequence.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Aug 2015 16:56:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205939#M38295</guid>
      <dc:creator>MikeZdeb</dc:creator>
      <dc:date>2015-08-25T16:56:37Z</dc:date>
    </item>
    <item>
      <title>Re: When is referring to a variable before it is defined allowed in a SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205940#M38296</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It might be worth mentioning that the variable HOWMANY is defined/declared by its first use and in this case gets the proper data type numeric.&amp;nbsp; In this example I is implicitly declared character and SAS doesn't want that.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As to @adam_black second example I understand how that can viewed as an error but I don't know if SAS can detect that unless you could turn off implicit declaration (may be possible I don't know).&amp;nbsp; I know you can turn the UN-INIT note to an error with NOTE2ERR option.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;DIV style="font-family: Courier New; font-size: 11pt;"&gt;&lt;SPAN style="color: #000080; background-color: #ffffff;"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;_null_&lt;/SPAN&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; i = first(&lt;/SPAN&gt;&lt;SPAN style="color: #800080; background-color: #ffffff;"&gt;'ABC'&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;);&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;if&lt;/SPAN&gt; &lt;SPAN style="color: #008080; background-color: #ffffff;"&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;then&lt;/SPAN&gt; &lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;set&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; sashelp.class point=i nobs=nobs;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;put&lt;/SPAN&gt; &lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;_all_&lt;/SPAN&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #0000ff; background-color: #ffffff;"&gt;stop&lt;/SPAN&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="color: #000080; background-color: #ffffff;"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/SPAN&gt;; &lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #008080; background-color: #ffffff;"&gt;&lt;STRONG&gt;33&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data _null_;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #008080; background-color: #ffffff;"&gt;&lt;STRONG&gt;34&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = first(&lt;/SPAN&gt;&lt;SPAN style="color: #800080; background-color: #ffffff;"&gt;'ABC'&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;);&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #008080; background-color: #ffffff;"&gt;&lt;STRONG&gt;35&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if &lt;/SPAN&gt;&lt;SPAN style="color: #008080; background-color: #ffffff;"&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; then set sashelp.class point=i nobs=nobs;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;ERROR: &lt;/SPAN&gt;&lt;SPAN style="color: #ff0000; background-color: #ffffff;"&gt;Variable&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt; i has already been defined as &lt;/SPAN&gt;&lt;SPAN style="color: #008080; background-color: #ffffff;"&gt;character.&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #008080; background-color: #ffffff;"&gt;&lt;STRONG&gt;36&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; put _all_;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #008080; background-color: #ffffff;"&gt;&lt;STRONG&gt;37&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stop;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #008080; background-color: #ffffff;"&gt;&lt;STRONG&gt;38&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="color: #000000; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Aug 2015 17:03:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205940#M38296</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2015-08-25T17:03:11Z</dc:date>
    </item>
    <item>
      <title>Re: When is referring to a variable before it is defined allowed in a SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205941#M38297</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 13px; background-color: #ffffff;"&gt;In response to "Values on the RIGHT of the = are known (variables or constants).&amp;nbsp; Values on the LEFT of the = can be new or already known variables."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The behavior that I think is odd is that SAS allows new variables on the RIGHT side of the =.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data out;&lt;/P&gt;&lt;P&gt;new_var1 = new_var2;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;NOTE: Variable new_var2 is uninitialized.&lt;/P&gt;&lt;P&gt;NOTE: The data set WORK.OUT has 1 observations and 2 variables.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SAS does print a note telling me that new_var is uninitialized but allows it nevertheless. This note could get lost in the log a large program making a variable name typo a hard error to find.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Aug 2015 18:04:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205941#M38297</guid>
      <dc:creator>Adam_Black</dc:creator>
      <dc:date>2015-08-25T18:04:59Z</dc:date>
    </item>
    <item>
      <title>Re: When is referring to a variable before it is defined allowed in a SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205942#M38298</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;In the following code...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-style: inherit; font-family: inherit;"&gt;data all;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-style: inherit; font-family: inherit;"&gt;input x @@;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-style: inherit; font-family: inherit;"&gt;datalines;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-style: inherit; font-family: inherit;"&gt;1 2 3 4 5 6 7 8 9 10&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-style: inherit; font-family: inherit;"&gt;;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-style: inherit; font-family: inherit;"&gt;data even;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-style: inherit; font-family: inherit;"&gt;do _n_=1 to howmany;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-style: inherit; font-family: inherit;"&gt;&amp;nbsp; set all nobs=howmany;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-style: inherit; font-family: inherit;"&gt;&amp;nbsp; if ^mod(x,2) then output;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-style: inherit; font-family: inherit;"&gt;end;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-family: 'courier new', courier;"&gt;&lt;STRONG style="font-style: inherit; font-family: inherit;"&gt;run;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;I would love it if you could explain the control flow of the second data step.Namely, is there an implied loop created by the set statement or is the implied loop overridden by the outer do loop? Thanks again for your help! &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Aug 2015 18:10:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205942#M38298</guid>
      <dc:creator>Adam_Black</dc:creator>
      <dc:date>2015-08-25T18:10:33Z</dc:date>
    </item>
    <item>
      <title>Re: When is referring to a variable before it is defined allowed in a SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205943#M38299</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi.&amp;nbsp; The OUTER loop rules.&amp;nbsp; There's one pass through the data step since all the observations in the data set are read within the loop.&amp;nbsp; The data step still returns to the beginning one more time to see if there are any more observations to be read.&amp;nbsp; Since there are none, the data step is finished.&amp;nbsp; You can see that by modifying the code as follows and looking at the LOG (you'll see two AT START and one AT END) ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;data even;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;PUT "AT START";&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;do _n_=1 to howmany;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp; set all nobs=howmany;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp; if ^mod(x,2) then output;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;end;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;PUT "AT END";&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, there's really no reason to do what I did in my example.&amp;nbsp; Without the outer do loop, the data step would work the same way.&amp;nbsp; I only did that to show you that even though the SET statement with NOBS=HOWMANY occurs after the start of the loop that uses the variable HOWMANY, the loop still works (example of what happens prior to execution versus what happens during execution of a data step).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;One situation where you might use actually use a loop within a data step is that it's one way to separate statements that need only to be executed once rather than on each pass through the data step.&amp;nbsp; The execute once statements occur outside the loop while those executed as each observation is read occur within the loop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Another great use of loops to read data is the DOW, especially handy when looking at groups of observations with your data with BY-GROUP processing.&amp;nbsp; If you don't know about the DOW, there's no need to explain it here since there are a number of really good papers to read ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;HOW to DOW&lt;/STRONG&gt;&lt;/EM&gt; (if you have never read a Paul Dorfman paper, it's a treat)&lt;/P&gt;&lt;P&gt;&lt;A href="http://support.sas.com/resources/papers/proceedings12/156-2012.pdf" title="http://support.sas.com/resources/papers/proceedings12/156-2012.pdf"&gt;http://support.sas.com/resources/papers/proceedings12/156-2012.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;an earlier version of some material in the above ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;The DOW-Loop Unrolled&lt;/STRONG&gt;&lt;/EM&gt; (another one by Paul Dorfman, why not read the best)&lt;/P&gt;&lt;P&gt;&lt;A class="active_link" href="http://analytics.ncsu.edu/sesug/2007/SD08.pdf" title="http://analytics.ncsu.edu/sesug/2007/SD08.pdf"&gt;http://analytics.ncsu.edu/sesug/2007/SD08.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The above includes the following which is what I meant by isolating the repeated execution of some statements from others to be executed once ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;The DOW-loop (Whitlock DO-loop) is a nested repetitive DATA step programming&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;structure, intentionally organized in order to allow for programmatically and logically&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;natural isolation of DO-loop instructions related to a certain break-event from actions&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;performed before and after the loop, and without resorting to superfluous conditional&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;statements. &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just search for "SAS DOW" and you'll find more.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Aug 2015 15:33:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205943#M38299</guid>
      <dc:creator>MikeZdeb</dc:creator>
      <dc:date>2015-08-26T15:33:02Z</dc:date>
    </item>
    <item>
      <title>Re: When is referring to a variable before it is defined allowed in a SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205944#M38300</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Part of the convenience of SAS data step coding.&lt;/P&gt;&lt;P&gt;Here is a contrived example that takes advantage of that.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data new ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; lag_age = age ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set sashelp.class ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; put _n_ age lag_age ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Aug 2015 16:35:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-is-referring-to-a-variable-before-it-is-defined-allowed-in/m-p/205944#M38300</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2015-08-26T16:35:24Z</dc:date>
    </item>
  </channel>
</rss>

