<?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: DO Loops in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67851#M19415</link>
    <description>Again, use a DATA step approach to generate your SAS code with PUT statements out to a TEMP allocated FILENAME, and then %INCLUDE that &lt;FILEREF&gt; once -- no macro coding at all.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;/FILEREF&gt;</description>
    <pubDate>Sat, 29 Aug 2009 03:02:25 GMT</pubDate>
    <dc:creator>sbb</dc:creator>
    <dc:date>2009-08-29T03:02:25Z</dc:date>
    <item>
      <title>DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67843#M19407</link>
      <description>Hello -- &lt;BR /&gt;
&lt;BR /&gt;
Can anyone confirm how the system should be valuing the "setnm" macro variable below? What I expect to happen is "setnm" = 0208. Instead what appears to be happening is "setnm" = 0308. I'm not sure how this result is occurring from the code below. "Setnm" is a macro variable containing part of the dataset name.&lt;BR /&gt;
&lt;BR /&gt;
Any ideas are appreciated. Thanks&lt;BR /&gt;
&lt;BR /&gt;
	do i = 2 to 2;&lt;BR /&gt;
		i_str = put(i, z2.) || '08';&lt;BR /&gt;
		call symput('setnm', i_str);&lt;BR /&gt;
		yrmonvalue = '2008' || put(i, z2.);&lt;BR /&gt;
		call execute ("data sue.comm;&lt;BR /&gt;
                                                         set alex.comm&amp;amp;setnm;&lt;BR /&gt;
                                                          ...</description>
      <pubDate>Thu, 27 Aug 2009 18:07:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67843#M19407</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-08-27T18:07:46Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67844#M19408</link>
      <description>I've fixed part of my problem. Now the issue I'm facing is that the "setnm" variable doesn't seem to change when "i" increases from 2 to 3 below. &lt;BR /&gt;
&lt;BR /&gt;
I have a "DO" loop embedded in a _null_ data step. Included in the loop is a "call execute." I'm trying to have the "call execute" step run for each value of "i" as "i" increases in the loop.&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
	do i = 2 to 3;&lt;BR /&gt;
		format i z2.;&lt;BR /&gt;
		i_str = put(i, z2.) || '08';&lt;BR /&gt;
		call symput('setnm', i_str);&lt;BR /&gt;
		call execute ("data sue.comm;&lt;BR /&gt;
			        set alex.comm&amp;amp;setnm;&lt;BR /&gt;
                                                      ... etc.</description>
      <pubDate>Thu, 27 Aug 2009 18:19:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67844#M19408</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-08-27T18:19:37Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67845#M19409</link>
      <description>Add something to your DATA step like PUTLOG _ALL_;   to dump SAS variables with each DATA step pass.  The SYMPUT is executing with each DATA step pass, but if you read the CALL EXECUTE discussion, it states that the code is not compiled until after the DATA step is finished.  You will need to generate %LET statements to set the macro variable value, instead of the CALL SYMPUT approach.  Then you will see the "generated" SAS code compiled and executed after completing your first DATA step.&lt;BR /&gt;
&lt;BR /&gt;
Of course, another approach would be to output the SAS code to a "temp" FILENAME allocation using PUT statements, and then %INCLUDE the temp file "fileref".  Less smoke-and-mirrors occurring that way.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 27 Aug 2009 19:10:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67845#M19409</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-08-27T19:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67846#M19410</link>
      <description>Thanks for that feedback Scott. What if I embed the DO loop within a macro? Essentially what I want to do is loop through dataset names and do some actions to each dataset. The dataset names are derivable from the loop counter. &lt;BR /&gt;
&lt;BR /&gt;
So far I've started the following but getting errors. The "&amp;amp;setnm" statement is out of order, the log says. I know I could use a %let statement, but I need to use the loop counter to set the "setnm." Not sure how I could do that with a %let statement since it would have to be outside the loop? Or not?&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%macro sortdata;&lt;BR /&gt;
%do i=2 %to 3;&lt;BR /&gt;
	&amp;amp;setnm = put(i, z2.) || '08' ;&lt;BR /&gt;
	data work.comm;&lt;BR /&gt;
		set alex.comm&amp;amp;setnm;</description>
      <pubDate>Thu, 27 Aug 2009 20:08:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67846#M19410</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-08-27T20:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67847#M19411</link>
      <description>Yes, it is possible with MACRO language - if you are comfortable with the language and can code/support it to suit your needs.  The %LET assignment statement assigns a value which can then be used with ampersand substitution.&lt;BR /&gt;
&lt;BR /&gt;
For the max diagnostics output, add to your SAS program the following line:&lt;BR /&gt;
&lt;BR /&gt;
OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN MLOGIC;&lt;BR /&gt;
&lt;BR /&gt;
Here's a SAS-hosted DOC link on the SAS support  &lt;A href="http://support.sas.com/" target="_blank"&gt;http://support.sas.com/&lt;/A&gt;  website:&lt;BR /&gt;
&lt;BR /&gt;
SAS 9.2 Macro Language: Reference&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/titlepage.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/titlepage.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
Macro Language Elements&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002047059.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002047059.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
My suggestion is that you work with the DATA step approach, as previously commented.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 27 Aug 2009 20:42:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67847#M19411</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-08-27T20:42:50Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67848#M19412</link>
      <description>I think what you are trying to do might need to look more like:&lt;BR /&gt;
&lt;BR /&gt;
%macro sortdata;&lt;BR /&gt;
%do i=2 %to 3;&lt;BR /&gt;
%let setnm = %sysfunc(putn(&amp;amp;i, z2.))08;&lt;BR /&gt;
/* the line above calls put to get the formatted value you want but you need */&lt;BR /&gt;
/* to explicitly state you are putting a number. With the macro language the*/&lt;BR /&gt;
/* concatenation is implicit, not explicit (unless you want to use a function  */&lt;BR /&gt;
/* so no spaces after the first part that resolves as needed using %sysfunc */&lt;BR /&gt;
data work.comm;&lt;BR /&gt;
set alex.comm&amp;amp;setnm;&lt;BR /&gt;
...&lt;BR /&gt;
run;&lt;BR /&gt;
%end; /*i loop*/</description>
      <pubDate>Fri, 28 Aug 2009 21:29:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67848#M19412</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2009-08-28T21:29:58Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67849#M19413</link>
      <description>Thanks much, ballardw, for your tip. I will try that out.&lt;BR /&gt;
&lt;BR /&gt;
One last note on this thread Re: something Scott Barry said: &lt;BR /&gt;
&lt;BR /&gt;
Will the %include statement execute with each loop iteration if I embed %include in a DO loop, which in turn is embedded in a data step? Or will the %include only execute &lt;B&gt;AFTER&lt;/B&gt; the data step's DO loop is finished, even though %include is within the DO loop itself? The latter result is what happened with my "call execute" attempt, and I'm wondering if %include works the same way. I find that behavior misleading because there is nothing in the log that alerts you to the fact.&lt;BR /&gt;
&lt;BR /&gt;
The SAS documentation is so lengthy that sometimes it's hard to find answers to specific processing questions. I did try to do my due diligence!&lt;BR /&gt;
&lt;BR /&gt;
Thanks everyone --</description>
      <pubDate>Fri, 28 Aug 2009 21:48:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67849#M19413</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-08-28T21:48:57Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67850#M19414</link>
      <description>ballardw -- FYI, I still get an error on the "set" statement. &lt;BR /&gt;
&lt;BR /&gt;
Below is my code. How very frustrating! Side note: "alex" is a libname reference.&lt;BR /&gt;
&lt;BR /&gt;
%macro sortdata;&lt;BR /&gt;
%do i=2 %to 3;&lt;BR /&gt;
	%let setnm =  %sysfunc(putn(&amp;amp;i, z2.))08;&lt;BR /&gt;
	data work.comm;&lt;BR /&gt;
		set alex.comm&amp;amp;setnm;</description>
      <pubDate>Fri, 28 Aug 2009 22:08:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67850#M19414</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-08-28T22:08:47Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67851#M19415</link>
      <description>Again, use a DATA step approach to generate your SAS code with PUT statements out to a TEMP allocated FILENAME, and then %INCLUDE that &lt;FILEREF&gt; once -- no macro coding at all.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;/FILEREF&gt;</description>
      <pubDate>Sat, 29 Aug 2009 03:02:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67851#M19415</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-08-29T03:02:25Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67852#M19416</link>
      <description>What is the text of the error message?</description>
      <pubDate>Mon, 31 Aug 2009 23:00:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67852#M19416</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2009-08-31T23:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67853#M19417</link>
      <description>data _null_;&lt;BR /&gt;
do i=2 to 3;&lt;BR /&gt;
setnm = compress ('alex.comm'|| put(i, z2.) || '08') ;&lt;BR /&gt;
call execute('data work.comm;');&lt;BR /&gt;
call execute('set ' || setnm || ';');&lt;BR /&gt;
call execute('run;');&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Of course you are overwriting work.comm each time.  I just rolled out a program with this sort of code using append.</description>
      <pubDate>Mon, 31 Aug 2009 23:30:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67853#M19417</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2009-08-31T23:30:33Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67854#M19418</link>
      <description>Ballardw, error message is as follows: Statement is not valid or it is used out of proper order.</description>
      <pubDate>Tue, 01 Sep 2009 13:15:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67854#M19418</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-09-01T13:15:07Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67855#M19419</link>
      <description>Although not necessarily related to your error message, it doesn't seem likely to me that you'd want to simply replace work.comm on each iteration.&lt;BR /&gt;
&lt;BR /&gt;
Did you actually intend to SET multiple data sets into work.comm?&lt;BR /&gt;
&lt;BR /&gt;
BTW, the last code you pasted wasn't complete (no closing macro %end statement, no %mend statement, etc.)</description>
      <pubDate>Tue, 01 Sep 2009 13:35:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67855#M19419</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-09-01T13:35:51Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67856#M19420</link>
      <description>I was just posting the general concept, not the true code. The code to %mend was not included since the "set" statement was the problem child. Thanks</description>
      <pubDate>Tue, 01 Sep 2009 14:10:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67856#M19420</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-09-01T14:10:21Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67857#M19421</link>
      <description>That's fine, but when I tried to replicate your error message, I did not run into a problem with the SET statement, so there's something "missing".</description>
      <pubDate>Tue, 01 Sep 2009 14:12:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67857#M19421</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-09-01T14:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67858#M19422</link>
      <description>I like Flip's straightforward nonmacro approach, partly because I often use similar code.  Yes, it can be done with a macro, but why make it so difficult?</description>
      <pubDate>Wed, 02 Sep 2009 20:47:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67858#M19422</guid>
      <dc:creator>advoss</dc:creator>
      <dc:date>2009-09-02T20:47:34Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67859#M19423</link>
      <description>A random thought:&lt;BR /&gt;
&lt;BR /&gt;
I have been developing in SAS for 25 years.  Now I did more Oracle forms work around the time that V9 came out so I got a bit rusty.  At any rate, in the past programmers would always try everything before diving into a macro.  I knew programmers with 5 to 10 years of experience who would not put two &amp;amp; together.&lt;BR /&gt;
&lt;BR /&gt;
Now it seems that people are trying a complex macro as their first SAS programming attempt.  It seems like the large number of programming examples available lead people to try to modify things they do not really understand.  Don't get me wrong, I love macro programming.  I had an application in production for over a decade with 45 macro conditional and looping statements in one line of SAS code.  The point is, the simple solluton is often much easier to maintain and more efficient.  &lt;BR /&gt;
If you are new to SAS, try simple first.</description>
      <pubDate>Thu, 03 Sep 2009 12:32:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67859#M19423</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2009-09-03T12:32:18Z</dc:date>
    </item>
    <item>
      <title>Re: DO Loops</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67860#M19424</link>
      <description>Thanks flip and everyone, for your feedback, This discussion group has been most useful. Sometimes learning SAS is overwhelming and hard to grasp until you're faced with a real-world project. Not thru a book.</description>
      <pubDate>Tue, 08 Sep 2009 13:53:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/DO-Loops/m-p/67860#M19424</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-09-08T13:53:15Z</dc:date>
    </item>
  </channel>
</rss>

