<?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: Loops in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68790#M14910</link>
    <description>But maybe the answer is much simpler. In this case we don't have to use %do but just do;&lt;BR /&gt;
&lt;BR /&gt;
%macro temp1;&lt;BR /&gt;
data temp1;&lt;BR /&gt;
do i= 1 to 5, 7,9;&lt;BR /&gt;
m=i+1;&lt;BR /&gt;
output;&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
%mend temp1;&lt;BR /&gt;
%temp1;&lt;BR /&gt;
&lt;BR /&gt;
It works fine. So I guess the question now is when should we use '%do' and when 'do'. Should be careful about that.</description>
    <pubDate>Mon, 12 Jan 2009 07:25:16 GMT</pubDate>
    <dc:creator>ieva</dc:creator>
    <dc:date>2009-01-12T07:25:16Z</dc:date>
    <item>
      <title>Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68787#M14907</link>
      <description>Hi All,&lt;BR /&gt;
&lt;BR /&gt;
the following code works.&lt;BR /&gt;
  data temp;&lt;BR /&gt;
    do i= 1 to 5 ,7,9; (Non continuous loop)&lt;BR /&gt;
      m=i+1;&lt;BR /&gt;
   end;&lt;BR /&gt;
 run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
if I use the same logic in macros it fails&lt;BR /&gt;
&lt;BR /&gt;
%macro temp1;&lt;BR /&gt;
  data temp;&lt;BR /&gt;
 %do i= 1 %to 5,7,9;&lt;BR /&gt;
   m=&amp;amp;i+1;&lt;BR /&gt;
%end;&lt;BR /&gt;
  run;&lt;BR /&gt;
%mend temp1;&lt;BR /&gt;
%temp1;&lt;BR /&gt;
&lt;BR /&gt;
What could be the reason?&lt;BR /&gt;
&lt;BR /&gt;
Thanks all for your time.&lt;BR /&gt;
SASPHILE!</description>
      <pubDate>Sun, 11 Jan 2009 04:19:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68787#M14907</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2009-01-11T04:19:48Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68788#M14908</link>
      <description>Hi Phille,&lt;BR /&gt;
&lt;BR /&gt;
I read some papers about the %do statement and I didn´t find anything about this way of using %do statement with specific cases. Maybe, %do in macro function don´t have this functionallity. When I tried to run, he don´t recognize the %do parameter.&lt;BR /&gt;
&lt;BR /&gt;
You could resolver this problem using an array to perfom the specific calculation with 5, 7 and 9.&lt;BR /&gt;
&lt;BR /&gt;
Other think, the do statement on the sas code, execute 7 times ( he executes 1, 2, 3, 4, 5, 7, 9) and not 3 times ( using 5, 7, 9), as it appears to be created for.&lt;BR /&gt;
&lt;BR /&gt;
If you find a positiver answer to your problem, please send it here !!!&lt;BR /&gt;
&lt;BR /&gt;
Regards.&lt;BR /&gt;
&lt;BR /&gt;
Kassim &lt;BR /&gt;
Brazil</description>
      <pubDate>Sun, 11 Jan 2009 13:23:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68788#M14908</guid>
      <dc:creator>kassimorra</dc:creator>
      <dc:date>2009-01-11T13:23:51Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68789#M14909</link>
      <description>Try this:&lt;BR /&gt;
&lt;BR /&gt;
%macro temp1;&lt;BR /&gt;
data temp1;&lt;BR /&gt;
%do i= 1 %to 9;&lt;BR /&gt;
m=&amp;amp;i+1;&lt;BR /&gt;
if &amp;amp;i in (1:5, 7, 9) then output;&lt;BR /&gt;
%end;&lt;BR /&gt;
run;&lt;BR /&gt;
%mend temp1;&lt;BR /&gt;
%temp1;&lt;BR /&gt;
&lt;BR /&gt;
Maybe not the most elegant solution, but it works. &lt;BR /&gt;
&lt;BR /&gt;
I don't know why it doesn't work in a regular way.</description>
      <pubDate>Mon, 12 Jan 2009 07:09:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68789#M14909</guid>
      <dc:creator>ieva</dc:creator>
      <dc:date>2009-01-12T07:09:45Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68790#M14910</link>
      <description>But maybe the answer is much simpler. In this case we don't have to use %do but just do;&lt;BR /&gt;
&lt;BR /&gt;
%macro temp1;&lt;BR /&gt;
data temp1;&lt;BR /&gt;
do i= 1 to 5, 7,9;&lt;BR /&gt;
m=i+1;&lt;BR /&gt;
output;&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
%mend temp1;&lt;BR /&gt;
%temp1;&lt;BR /&gt;
&lt;BR /&gt;
It works fine. So I guess the question now is when should we use '%do' and when 'do'. Should be careful about that.</description>
      <pubDate>Mon, 12 Jan 2009 07:25:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68790#M14910</guid>
      <dc:creator>ieva</dc:creator>
      <dc:date>2009-01-12T07:25:16Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68791#M14911</link>
      <description>There are two parts to this.&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;
Firstly your non-continuous do loop is not doing what you think it is. The syntax for the do statement is:&lt;BR /&gt;&lt;BR /&gt;
&lt;I&gt;DO index-variable=specification-1 &amp;lt;, . . . specification-n&amp;gt;;&lt;/I&gt;&lt;BR /&gt;&lt;BR /&gt;
where&lt;BR /&gt; &lt;BR /&gt;
&lt;I&gt;specification&lt;/I&gt;&lt;BR /&gt; &lt;BR /&gt;
&amp;nbsp;&amp;nbsp;denotes an expression or a series of expressions in this form &lt;BR /&gt;&lt;BR /&gt;
&lt;I&gt;start &lt;TO stop=""&gt; &lt;BY increment=""&gt; &lt;WHILE&gt;&lt;/WHILE&gt;&lt;/BY&gt;&lt;/TO&gt;&lt;/I&gt; &lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;
You've given, excuse the psuedo-markup, the following:&lt;BR /&gt;&lt;BR /&gt;
&lt;DO index="i"&gt;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;&lt;SPEC id="1" start="1" stop="5"&gt;&lt;/SPEC&gt;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;&lt;SPEC id="2" start="7" stop="."&gt;&lt;/SPEC&gt;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;&lt;SPEC id="2" start="9" stop="."&gt;&lt;/SPEC&gt;&lt;BR /&gt;&lt;BR /&gt;
&lt;/DO&gt;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;
Next, the syntax for %DO (Iterative) does not support multiple specifications and is given as&lt;BR /&gt;&lt;BR /&gt;
&lt;I&gt;%DO macro-variable=start %TO stop &amp;lt;%BY increment&amp;gt;;&lt;/I&gt; &lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;
The equivalent macro code would be&lt;BR /&gt; &lt;BR /&gt;
&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;
%macro temp1;&lt;BR /&gt;&lt;BR /&gt;
data temp;&lt;BR /&gt;&lt;BR /&gt;
%do i= 1 %to 5;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;m=&amp;amp;i+1;&lt;BR /&gt;&lt;BR /&gt;
%end;&lt;BR /&gt;&lt;BR /&gt;
%do i=7 %to 9 %by 2;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;m=&amp;amp;i+1;&lt;BR /&gt;&lt;BR /&gt;
%end;&lt;BR /&gt;&lt;BR /&gt;
run;&lt;BR /&gt;&lt;BR /&gt;
%mend temp1;&lt;BR /&gt;&lt;BR /&gt;
%temp1;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;
which would be resolved to the following executable code:&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;
data temp;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;m=1+1;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;m=2+1;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;m=3+1;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;m=4+1;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;m=5+1;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;m=7+1;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&amp;nbsp;m=9+1;&lt;BR /&gt;&lt;BR /&gt;
run;&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;
If you want to read the manual it is available &lt;A href="http://v8doc.sas.com/sashtml/"&gt;here&lt;/A&gt;.&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;
I'm still mystified by what you are trying to accomplish, but hope this helps,&lt;BR /&gt;&lt;BR /&gt;
&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;
ProcMe&lt;BR /&gt;</description>
      <pubDate>Mon, 12 Jan 2009 12:30:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68791#M14911</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-01-12T12:30:32Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68792#M14912</link>
      <description>Hello Procme,&lt;BR /&gt;
    There is a situation where in I have to loop through many iterations.But the case for the looping is non continous. And also the variable names are to be changed dynamically for every loop increment. Thats the reason I have to use the macro loop counter.&lt;BR /&gt;
So the case is i will have a contimnous looping from 2 to 13 and then 17,18,21 and 29.&lt;BR /&gt;
I was looking for a workaround  in this case.&lt;BR /&gt;
Thanks for your time.&lt;BR /&gt;
SASPHILE</description>
      <pubDate>Mon, 12 Jan 2009 15:27:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68792#M14912</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2009-01-12T15:27:39Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68793#M14913</link>
      <description>Is this the sort of thing you were after?&lt;BR /&gt;
&lt;BR /&gt;
/*Loop rule set*/&lt;BR /&gt;
data loops;&lt;BR /&gt;
   input from to;&lt;BR /&gt;
   infile cards missover;&lt;BR /&gt;
cards;&lt;BR /&gt;
1 12&lt;BR /&gt;
17 18&lt;BR /&gt;
21 &lt;BR /&gt;
23 &lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
/* Play data, something to process */&lt;BR /&gt;
data have;&lt;BR /&gt;
   do i = 1 to 50;&lt;BR /&gt;
      output;&lt;BR /&gt;
   end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%macro doloops(data=have, out=want, loopset=loops, key=i, action=output);&lt;BR /&gt;
/*Read the loop rule set into memory */&lt;BR /&gt;
data _null_;&lt;BR /&gt;
   set &amp;amp;loopset end=eof;&lt;BR /&gt;
   call symput('loop_from_' || compress(_n_), compress(from));&lt;BR /&gt;
   if to eq . then&lt;BR /&gt;
      call symput('loop_to_' || compress(_n_), compress(from));&lt;BR /&gt;
   else&lt;BR /&gt;
      call symput('loop_to_' || compress(_n_), compress(to));&lt;BR /&gt;
   if eof then &lt;BR /&gt;
      call symput('loop_count', compress(_n_));&lt;BR /&gt;
run;&lt;BR /&gt;
/* Process the specified dataset */&lt;BR /&gt;
data &amp;amp;out(drop=_i); &lt;BR /&gt;
   set &amp;amp;data;&lt;BR /&gt;
   %do _loop = 1 %to &amp;amp;loop_count;&lt;BR /&gt;
      %let _from = &amp;amp;&amp;amp;&amp;amp;loop_from_&amp;amp;_loop;&lt;BR /&gt;
      %let _to = &amp;amp;&amp;amp;&amp;amp;loop_to_&amp;amp;_loop;&lt;BR /&gt;
      do _i = &amp;amp;_from to &amp;amp;_to;&lt;BR /&gt;
         if &amp;amp;key = _i then do;&lt;BR /&gt;
            &amp;amp;action;&lt;BR /&gt;
         end;&lt;BR /&gt;
      end;&lt;BR /&gt;
   %end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%mend doloops;&lt;BR /&gt;
&lt;BR /&gt;
%doloops(data=have, out=want, loopset=loops, key=i, action=output);&lt;BR /&gt;
&lt;BR /&gt;
Gives output for obs 1-12, 17-18, 21, 23.&lt;BR /&gt;
&lt;BR /&gt;
ProcMe</description>
      <pubDate>Mon, 12 Jan 2009 16:18:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68793#M14913</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-01-12T16:18:28Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68794#M14914</link>
      <description>... or &lt;BR /&gt;
&lt;BR /&gt;
data &amp;amp;out(drop=_i);&lt;BR /&gt;
   set &amp;amp;data;&lt;BR /&gt;
   do _i = &lt;BR /&gt;
      %do _loop = 1 %to &amp;amp;loop_count;&lt;BR /&gt;
         %let _from = &amp;amp;&amp;amp;&amp;amp;loop_from_&amp;amp;_loop;&lt;BR /&gt;
         %let _to = &amp;amp;&amp;amp;&amp;amp;loop_to_&amp;amp;_loop;&lt;BR /&gt;
         &lt;BR /&gt;
         %if &amp;amp;_loop lt &amp;amp;loop_count %then &amp;amp;_from to &amp;amp;_to, &lt;BR /&gt;
         %else &amp;amp;_from to &amp;amp;_to ;&lt;BR /&gt;
      %end;&lt;BR /&gt;
      ;&lt;BR /&gt;
      if &amp;amp;key = _i then do;&lt;BR /&gt;
         &amp;amp;action;&lt;BR /&gt;
      end;&lt;BR /&gt;
   end;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
ProcMe</description>
      <pubDate>Mon, 12 Jan 2009 16:42:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68794#M14914</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-01-12T16:42:29Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68795#M14915</link>
      <description>Proc me,&lt;BR /&gt;
 Here is the case:&lt;BR /&gt;
I would want to create datasets temp2 to temp14 and then temp16,temp17 and temp41&lt;BR /&gt;
In the following case the loop goes through 2to41 and creates empty datasets like temp18 temp19.......... etc which i dont need.&lt;BR /&gt;
the logic is based on the where condition and _temg001 have values only from 2 to 14 and 16,17 and 41. &lt;BR /&gt;
&lt;BR /&gt;
%macro flatten;&lt;BR /&gt;
  %do i= 2 %to 41;&lt;BR /&gt;
data temp&amp;amp;i;&lt;BR /&gt;
 set allvar.Duphic_20080110(keep= assignment_reassignment_date &lt;BR /&gt;
						          HIC  _TEMG001 month ma_pd_ind);&lt;BR /&gt;
where _TEMG001=&amp;amp;i and ma_pd_ind='PD' and month&amp;lt;10;&lt;BR /&gt;
if &amp;amp;i in (2:14,16,17,41) then output;&lt;BR /&gt;
%end;&lt;BR /&gt;
run;&lt;BR /&gt;
%mend flatten;&lt;BR /&gt;
%flatten;&lt;BR /&gt;
Thanks for your time.&lt;BR /&gt;
SASPHILE</description>
      <pubDate>Mon, 12 Jan 2009 18:53:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68795#M14915</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2009-01-12T18:53:13Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68796#M14916</link>
      <description>An approach I used frequently is to drive a macro execution with a DATA step and CALL EXECUTE.&lt;BR /&gt;
&lt;BR /&gt;
An oversimplified example:&lt;BR /&gt;
&lt;BR /&gt;
%MACRO X(VALUE);&lt;BR /&gt;
  %PUT &amp;gt;INFO&amp;gt;  MACRO X INVOKED WITH VALUE: &amp;amp;VALUE;&lt;BR /&gt;
%MEND X;&lt;BR /&gt;
DATA _NULL_;&lt;BR /&gt;
DO I=1 TO 41;&lt;BR /&gt;
  IF I IN (1,15) OR (18 LE I LE 40) THEN CONTINUE;&lt;BR /&gt;
  CALL EXECUTE('%X(' !! I !! ')');&lt;BR /&gt;
END;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
As has been conveyed, the %IF / %END  is not equal to the DO / END behavior and syntax within the SAS language, and so there are ways to work within the current system.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Mon, 12 Jan 2009 22:02:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68796#M14916</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-01-12T22:02:07Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68797#M14917</link>
      <description>Would this do the trick?&lt;BR /&gt;
&lt;BR /&gt;
data loops;&lt;BR /&gt;
   input from to;&lt;BR /&gt;
   infile cards missover;&lt;BR /&gt;
cards;&lt;BR /&gt;
1 12&lt;BR /&gt;
17 18&lt;BR /&gt;
21 &lt;BR /&gt;
23 &lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
   do _TEMG001 = 1 to 50;&lt;BR /&gt;
      output;&lt;BR /&gt;
   end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%macro doloops(data=have, out=want, loopset=loops, key=i);&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
   set &amp;amp;loopset end=eof;&lt;BR /&gt;
   call symput('loop_from_' || compress(_n_), compress(from));&lt;BR /&gt;
   if to eq . then&lt;BR /&gt;
      call symput('loop_to_' || compress(_n_), compress(from));&lt;BR /&gt;
   else&lt;BR /&gt;
      call symput('loop_to_' || compress(_n_), compress(to));&lt;BR /&gt;
   if eof then &lt;BR /&gt;
      call symput('loop_count', compress(_n_));&lt;BR /&gt;
run;&lt;BR /&gt;
data &lt;BR /&gt;
   %do _loop = 1 %to &amp;amp;loop_count;&lt;BR /&gt;
      %do _outds = &amp;amp;&amp;amp;&amp;amp;loop_from_&amp;amp;_loop %to &amp;amp;&amp;amp;&amp;amp;loop_to_&amp;amp;_loop;&lt;BR /&gt;
         &amp;amp;out._&amp;amp;_outds.(drop=_i)&lt;BR /&gt;
      %end;&lt;BR /&gt;
   %end;&lt;BR /&gt;
   ;&lt;BR /&gt;
   set &amp;amp;data;&lt;BR /&gt;
   do _i = &lt;BR /&gt;
      %do _loop = 1 %to &amp;amp;loop_count;&lt;BR /&gt;
         &lt;BR /&gt;
         %if &amp;amp;_loop lt &amp;amp;loop_count %then &amp;amp;&amp;amp;&amp;amp;loop_from_&amp;amp;_loop to &amp;amp;&amp;amp;&amp;amp;loop_to_&amp;amp;_loop, ;&lt;BR /&gt;
         %else &amp;amp;&amp;amp;&amp;amp;loop_from_&amp;amp;_loop to &amp;amp;&amp;amp;&amp;amp;loop_to_&amp;amp;_loop ;&lt;BR /&gt;
&lt;BR /&gt;
      %end;&lt;BR /&gt;
      ;&lt;BR /&gt;
      %do _loop = 1 %to &amp;amp;loop_count;&lt;BR /&gt;
         %do _outds = &amp;amp;&amp;amp;&amp;amp;loop_from_&amp;amp;_loop %to &amp;amp;&amp;amp;&amp;amp;loop_to_&amp;amp;_loop;&lt;BR /&gt;
            if &amp;amp;key = &amp;amp;_outds and _i = &amp;amp;_outds then output &amp;amp;out._&amp;amp;_outds;&lt;BR /&gt;
         %end;&lt;BR /&gt;
      %end;         &lt;BR /&gt;
   end;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%mend doloops;&lt;BR /&gt;
&lt;BR /&gt;
%doloops(data=have, out=want, loopset=loops, key=_TEMG001);&lt;BR /&gt;
&lt;BR /&gt;
ProcMe</description>
      <pubDate>Tue, 13 Jan 2009 09:56:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68797#M14917</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-01-13T09:56:08Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68798#M14918</link>
      <description>... and you can get the loops dataset using something like this:&lt;BR /&gt;
&lt;BR /&gt;
data loop_source;&lt;BR /&gt;
   do i = 1 to 9, 12, 16 to 18, 22;&lt;BR /&gt;
      output;&lt;BR /&gt;
   end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data loops(keep=from to);&lt;BR /&gt;
   set loop_source end=eof;&lt;BR /&gt;
   retain from 0;&lt;BR /&gt;
   retain to 0;&lt;BR /&gt;
   retain last_i 0;&lt;BR /&gt;
   if _n_ = 1 then from = i;&lt;BR /&gt;
   else do;&lt;BR /&gt;
      last_i = lag(i);&lt;BR /&gt;
      if last_i ne . and last_i lt i-1 then do;&lt;BR /&gt;
         to = last_i;&lt;BR /&gt;
         output;&lt;BR /&gt;
         from = i;&lt;BR /&gt;
      end;&lt;BR /&gt;
   end;&lt;BR /&gt;
   if eof then do;&lt;BR /&gt;
      to = i;&lt;BR /&gt;
      output;&lt;BR /&gt;
   end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
ProcMe</description>
      <pubDate>Tue, 13 Jan 2009 10:21:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68798#M14918</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-01-13T10:21:26Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68799#M14919</link>
      <description>Just two more variants how to do it:&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
/* variant 1: %do %while with %scan */&lt;BR /&gt;
%macro CreateTempDSVers1(ind=);&lt;BR /&gt;
  %let i=1;&lt;BR /&gt;
  %do %while(%scan(&amp;amp;ind,&amp;amp;i) ne );&lt;BR /&gt;
    data TempDS%scan(&amp;amp;ind,&amp;amp;i);&lt;BR /&gt;
      var='hello world';&lt;BR /&gt;
    run;&lt;BR /&gt;
    %let i=%eval(&amp;amp;i+1);&lt;BR /&gt;
  %end;&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
%CreateTempDSVers1(ind=2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 41)&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
/* variant 2: Call Execute */&lt;BR /&gt;
%macro CreateTempDSVers2(ind);&lt;BR /&gt;
  data TempDS&amp;amp;ind;&lt;BR /&gt;
    var='hello world';&lt;BR /&gt;
  run;&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
  do i=2 to 14,16,17,41;&lt;BR /&gt;
    call execute(cats('%CreateTempDSVers2(',i,')'));&lt;BR /&gt;
  end;&lt;BR /&gt;
run;</description>
      <pubDate>Tue, 13 Jan 2009 12:27:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68799#M14919</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2009-01-13T12:27:53Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68800#M14920</link>
      <description>The following code allows you to specify the ranges you require in the call to %Flatten. Note the use of the %quote function in the macro call that masks the ',' symbols so that the macro knows that there is only one parameter being passed.&lt;BR /&gt;
&lt;BR /&gt;
%macro flatten(inn);&lt;BR /&gt;
%LOCAL lista list i ;&lt;BR /&gt;
%LET i = 1;&lt;BR /&gt;
%DO %UNTIL(%SCAN(&amp;amp;inn,&amp;amp;i,%STR(,)) EQ );&lt;BR /&gt;
%LET lista = %SCAN(&amp;amp;inn,&amp;amp;i,%STR(,));&lt;BR /&gt;
%IF %index(&amp;amp;lista,%STR(:)) %THEN&lt;BR /&gt;
%DO x = %SCAN(&amp;amp;lista,1,':') %TO %SCAN(&amp;amp;lista,2,':');&lt;BR /&gt;
%LET list = &amp;amp;list&amp;amp;x%STR( );&lt;BR /&gt;
%END;&lt;BR /&gt;
%ELSE %DO;&lt;BR /&gt;
%LET list = &amp;amp;list&amp;amp;lista%STR( );&lt;BR /&gt;
%END;&lt;BR /&gt;
%let i =%eval(&amp;amp;i+1);&lt;BR /&gt;
%END;&lt;BR /&gt;
&lt;BR /&gt;
data &lt;BR /&gt;
%LET i = 1;&lt;BR /&gt;
%DO %UNTIL(%SCAN(&amp;amp;list,&amp;amp;i,%STR( )) EQ ) ;&lt;BR /&gt;
%unquote(temp%SCAN(&amp;amp;list,&amp;amp;i,%STR( ))(keep=m%SCAN(&amp;amp;list,&amp;amp;i,%STR( ))))&lt;BR /&gt;
%let i =%eval(&amp;amp;i + 1);&lt;BR /&gt;
%END;&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
%LET i = 1;&lt;BR /&gt;
%DO %UNTIL(%SCAN(&amp;amp;list,&amp;amp;i,%STR( )) EQ );&lt;BR /&gt;
m%SCAN(&amp;amp;list,&amp;amp;i,%STR( )) = %SCAN(&amp;amp;list,&amp;amp;i,%STR( )) + 1;&lt;BR /&gt;
%let i =%eval(&amp;amp;i+1);&lt;BR /&gt;
%END;&lt;BR /&gt;
&lt;BR /&gt;
%LET i = 1;&lt;BR /&gt;
%DO %UNTIL(%SCAN(&amp;amp;list,&amp;amp;i,%STR( )) EQ );&lt;BR /&gt;
output temp%SCAN(&amp;amp;list,&amp;amp;i,%STR( ));&lt;BR /&gt;
%let i =%eval(&amp;amp;i+1);&lt;BR /&gt;
%END;&lt;BR /&gt;
run; &lt;BR /&gt;
&lt;BR /&gt;
%mend flatten;&lt;BR /&gt;
options nomlogic mprint nosymbolgen;&lt;BR /&gt;
%flatten(%quote(1:7,14:16,18,30))&lt;BR /&gt;
&lt;BR /&gt;
The following log is produced by the macrto call:&lt;BR /&gt;
&lt;BR /&gt;
MPRINT(FLATTEN): data temp1(keep=m1) temp2(keep=m2) temp3(keep=m3) temp4(keep=m4) temp5(keep=m5)&lt;BR /&gt;
temp6(keep=m6) temp7(keep=m7) temp14(keep=m14) temp15(keep=m15) temp16(keep=m16) temp18(keep=m18)&lt;BR /&gt;
temp30(keep=m30) ;&lt;BR /&gt;
MPRINT(FLATTEN): m1 = 1 + 1;&lt;BR /&gt;
MPRINT(FLATTEN): m2 = 2 + 1;&lt;BR /&gt;
MPRINT(FLATTEN): m3 = 3 + 1;&lt;BR /&gt;
MPRINT(FLATTEN): m4 = 4 + 1;&lt;BR /&gt;
MPRINT(FLATTEN): m5 = 5 + 1;&lt;BR /&gt;
MPRINT(FLATTEN): m6 = 6 + 1;&lt;BR /&gt;
MPRINT(FLATTEN): m7 = 7 + 1;&lt;BR /&gt;
MPRINT(FLATTEN): m14 = 14 + 1;&lt;BR /&gt;
MPRINT(FLATTEN): m15 = 15 + 1;&lt;BR /&gt;
MPRINT(FLATTEN): m16 = 16 + 1;&lt;BR /&gt;
MPRINT(FLATTEN): m18 = 18 + 1;&lt;BR /&gt;
MPRINT(FLATTEN): m30 = 30 + 1;&lt;BR /&gt;
MPRINT(FLATTEN): output temp1;&lt;BR /&gt;
MPRINT(FLATTEN): output temp2;&lt;BR /&gt;
MPRINT(FLATTEN): output temp3;&lt;BR /&gt;
MPRINT(FLATTEN): output temp4;&lt;BR /&gt;
MPRINT(FLATTEN): output temp5;&lt;BR /&gt;
MPRINT(FLATTEN): output temp6;&lt;BR /&gt;
MPRINT(FLATTEN): output temp7;&lt;BR /&gt;
MPRINT(FLATTEN): output temp14;&lt;BR /&gt;
MPRINT(FLATTEN): output temp15;&lt;BR /&gt;
MPRINT(FLATTEN): output temp16;&lt;BR /&gt;
MPRINT(FLATTEN): output temp18;&lt;BR /&gt;
MPRINT(FLATTEN): output temp30;&lt;BR /&gt;
MPRINT(FLATTEN): run;&lt;BR /&gt;
&lt;BR /&gt;
NOTE: The data set WORK.TEMP1 has 1 observations and 1 variables.&lt;BR /&gt;
NOTE: The data set WORK.TEMP2 has 1 observations and 1 variables.&lt;BR /&gt;
NOTE: The data set WORK.TEMP3 has 1 observations and 1 variables.&lt;BR /&gt;
NOTE: The data set WORK.TEMP4 has 1 observations and 1 variables.&lt;BR /&gt;
NOTE: The data set WORK.TEMP5 has 1 observations and 1 variables.&lt;BR /&gt;
NOTE: The data set WORK.TEMP6 has 1 observations and 1 variables.&lt;BR /&gt;
NOTE: The data set WORK.TEMP7 has 1 observations and 1 variables.&lt;BR /&gt;
NOTE: The data set WORK.TEMP14 has 1 observations and 1 variables.&lt;BR /&gt;
NOTE: The data set WORK.TEMP15 has 1 observations and 1 variables.&lt;BR /&gt;
NOTE: The data set WORK.TEMP16 has 1 observations and 1 variables.&lt;BR /&gt;
NOTE: The data set WORK.TEMP18 has 1 observations and 1 variables.&lt;BR /&gt;
NOTE: The data set WORK.TEMP30 has 1 observations and 1 variables.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
real time 0.25 seconds&lt;BR /&gt;
cpu time 0.09 seconds&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
You can see that I've output each of the differently summed/named variables to different named datasets. This may not be quite what you want but you can cherry pick out the bits that you require.&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
&lt;BR /&gt;
BPD</description>
      <pubDate>Tue, 13 Jan 2009 16:42:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68800#M14920</guid>
      <dc:creator>BPD</dc:creator>
      <dc:date>2009-01-13T16:42:43Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68801#M14921</link>
      <description>Thanks for the help.&lt;BR /&gt;
But by invoking macros for looping causes other advanced functionality it will be 1 observation behind.</description>
      <pubDate>Tue, 13 Jan 2009 21:25:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68801#M14921</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2009-01-13T21:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68802#M14922</link>
      <description>Does this the job for you?&lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
  do i= 2 to 14,16,17,41;&lt;BR /&gt;
     _TEMG001=i; ma_pd_ind='PD'; month=7;output;&lt;BR /&gt;
     _TEMG001=i; ma_pd_ind='xx'; month=7;output;&lt;BR /&gt;
     _TEMG001=i; ma_pd_ind='PD'; month=10;output;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  select distinct _TEMG001, cats('work.temp',_TEMG001), cats('when(',_TEMG001,') output temp',_TEMG001,';')&lt;BR /&gt;
      into :IndList separated by ' ', :OutDS separated by ' ', :SelectDs separated by ' '&lt;BR /&gt;
    from have&lt;BR /&gt;
    order by _TEMG001;&lt;BR /&gt;
quit;&lt;BR /&gt;
%put OutDs   =%bquote(&amp;amp;OutDs);&lt;BR /&gt;
%put SelectDs=%bquote(&amp;amp;SelectDs);&lt;BR /&gt;
&lt;BR /&gt;
data &amp;amp;OutDS;&lt;BR /&gt;
  set have;&lt;BR /&gt;
  where ma_pd_ind='PD' and month&amp;lt;10;&lt;BR /&gt;
  select (_TEMG001);&lt;BR /&gt;
    &amp;amp;SelectDs&lt;BR /&gt;
    otherwise;&lt;BR /&gt;
  end; &lt;BR /&gt;
run;</description>
      <pubDate>Wed, 14 Jan 2009 09:28:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68802#M14922</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2009-01-14T09:28:55Z</dc:date>
    </item>
    <item>
      <title>Loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68803#M14923</link>
      <description>Thanks a ton to you all for giving me the necessary inputs for my question.&lt;BR /&gt;
 I couldnt incorporate any of the inputs as I was pressed for time to give my deliverable,I did learn new things looking at your coding styles.&lt;BR /&gt;
As part of the solution what I did was:&lt;BR /&gt;
&lt;BR /&gt;
%macro temp(n1= ,n=2);&lt;BR /&gt;
 %do i= &amp;amp;n1 %to &amp;amp;n2;  &lt;BR /&gt;
SAS code&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
%mend temp;&lt;BR /&gt;
%temp(n1=2, n2=16);&lt;BR /&gt;
%temp(n1=17, n2=17);&lt;BR /&gt;
%temp(n1=19, n2=19);&lt;BR /&gt;
%temp(n1=23, n2=27);</description>
      <pubDate>Wed, 14 Jan 2009 16:50:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loops/m-p/68803#M14923</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2009-01-14T16:50:12Z</dc:date>
    </item>
  </channel>
</rss>

