<?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 DO loop in PROC SQL. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13486#M1592</link>
    <description>This is what I would like to do:&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL;&lt;BR /&gt;
DO k=1 TO 50;&lt;BR /&gt;
CREATE TABLE NewTable(k) as &lt;BR /&gt;
SELECT * FROM MyDataSet WHERE MyVar=k;&lt;BR /&gt;
END;&lt;BR /&gt;
QUIT;&lt;BR /&gt;
&lt;BR /&gt;
I think you understand my intention.&lt;BR /&gt;
&lt;BR /&gt;
Isn't DO allowed inside PROC SQL?&lt;BR /&gt;
&lt;BR /&gt;
Another problem is the naming: NewTable(k).&lt;BR /&gt;
&lt;BR /&gt;
How can I achieve what I want with short programming code?&lt;BR /&gt;
&lt;BR /&gt;
In the real case the selection is much more complicated, but it is only the value of one variabel, corresponding to MyVar in the example, that is different in each loop step.&lt;BR /&gt;
&lt;BR /&gt;
Regards&lt;BR /&gt;
Anne</description>
    <pubDate>Tue, 31 Mar 2009 09:18:59 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2009-03-31T09:18:59Z</dc:date>
    <item>
      <title>DO loop in PROC SQL.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13486#M1592</link>
      <description>This is what I would like to do:&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL;&lt;BR /&gt;
DO k=1 TO 50;&lt;BR /&gt;
CREATE TABLE NewTable(k) as &lt;BR /&gt;
SELECT * FROM MyDataSet WHERE MyVar=k;&lt;BR /&gt;
END;&lt;BR /&gt;
QUIT;&lt;BR /&gt;
&lt;BR /&gt;
I think you understand my intention.&lt;BR /&gt;
&lt;BR /&gt;
Isn't DO allowed inside PROC SQL?&lt;BR /&gt;
&lt;BR /&gt;
Another problem is the naming: NewTable(k).&lt;BR /&gt;
&lt;BR /&gt;
How can I achieve what I want with short programming code?&lt;BR /&gt;
&lt;BR /&gt;
In the real case the selection is much more complicated, but it is only the value of one variabel, corresponding to MyVar in the example, that is different in each loop step.&lt;BR /&gt;
&lt;BR /&gt;
Regards&lt;BR /&gt;
Anne</description>
      <pubDate>Tue, 31 Mar 2009 09:18:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13486#M1592</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-03-31T09:18:59Z</dc:date>
    </item>
    <item>
      <title>Re: DO loop in PROC SQL.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13487#M1593</link>
      <description>You can not make a loop inside SQL, but you can make a small macro that will do what you want.&lt;BR /&gt;
 &lt;BR /&gt;
[pre]%macro sqlloop;&lt;BR /&gt;
 PROC SQL;&lt;BR /&gt;
 %DO k=1 %TO 50;&lt;BR /&gt;
 CREATE TABLE NewTable&amp;amp;k. as &lt;BR /&gt;
 SELECT * FROM MyDataSet WHERE MyVar=&amp;amp;k.; &lt;BR /&gt;
 %END;&lt;BR /&gt;
 QUIT;&lt;BR /&gt;
%mend;&lt;BR /&gt;
 &lt;BR /&gt;
%sqlloop;[/pre]&lt;BR /&gt;
&lt;BR /&gt;
If you data are BIG, this will not be an efficient method as it will read the data 50 times - perhaps apply an index on MyDataSet on the variable MyVar to boot performance.</description>
      <pubDate>Tue, 31 Mar 2009 10:40:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13487#M1593</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2009-03-31T10:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: DO loop in PROC SQL.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13488#M1594</link>
      <description>Thank you!&lt;BR /&gt;
&lt;BR /&gt;
The purpose is to make smaller sets from MyDataSet to get data for different years from which I want to get various statistics.&lt;BR /&gt;
So I don't really think putting an index on MyDataSet would solve this. It's the MyDataSet(k) I want. I call them NewTable(k).&lt;BR /&gt;
&lt;BR /&gt;
I have no experience with macros.&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;Would this be possible:&lt;/B&gt;&lt;BR /&gt;
&lt;BR /&gt;
%macro sqlloop; &lt;BR /&gt;
PROC SQL; &lt;BR /&gt;
%DO k=1 %TO 50;&lt;BR /&gt;
&lt;B&gt;Year=k+1949;&lt;/B&gt;&lt;BR /&gt;
CREATE TABLE NewTable&amp;amp;k. as  SELECT * FROM MyDataSet WHERE &lt;B&gt;Anno=Year;&lt;/B&gt;&lt;BR /&gt;
%END; &lt;BR /&gt;
QUIT;&lt;BR /&gt;
%mend; &lt;BR /&gt;
&lt;BR /&gt;
%sqlloop;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;Would it even be possible delete the do loop from the macro and instead call the macro in a do loop with k as a parameter?&lt;BR /&gt;
&lt;/B&gt;&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;But from where should the calling be done in that case?&lt;/B&gt;  Calling from inside a data step maybe is not a good idea, if even possible.&lt;BR /&gt;
&lt;BR /&gt;
Regards&lt;BR /&gt;
Anne</description>
      <pubDate>Tue, 31 Mar 2009 12:18:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13488#M1594</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-03-31T12:18:42Z</dc:date>
    </item>
    <item>
      <title>Re: DO loop in PROC SQL.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13489#M1595</link>
      <description>&lt;P&gt;Putting an index on MyDataSet would make it much faster to read.&lt;BR /&gt; &lt;BR /&gt; --Would it even be possible delete the do loop from the macro and instead call the macro in a do loop with k as a parameter?&lt;BR /&gt; &lt;BR /&gt; Yes, that would be possible - but have a look at this alternate solution&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sqlloop(start,end); 
   PROC SQL; 
     %DO year=&amp;amp;start. %TO &amp;amp;end.; 
       CREATE TABLE NewTable&amp;amp;year. as 
       SELECT * FROM MyDataSet WHERE anno=&amp;amp;year.; 
     %END; 
   QUIT;
%mend; 

%sqlloop(start=1949, end=1999)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt; This is just to show another approach, where data is only read 2 times (instead of 50 times)&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sqlloop(data,byvar);
   proc sql NOPRINT;
     select distinct &amp;amp;byvar. into :_values SEPARATED by ' _'
     from &amp;amp;data.;
   quit; 
   data _&amp;amp;_values.;
     set &amp;amp;data.;
      select (&amp;amp;byvar);
       %do i=1 %to %sysfunc(count(_&amp;amp;_values.,_));
          %let var = %sysfunc(scan(_&amp;amp;_values.,&amp;amp;i.)); 
          when ("%substr(&amp;amp;var.,2)") output &amp;amp;var.;
       %end;
       end;
   run;
%mend;

%sqlloop(data=sashelp.class, byvar=age)
%sqlloop(data=sashelp.class, byvar=sex)
%sqlloop(data=MyDataSet, byvar=anno)&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13549"&gt;@Cynthia_sas﻿&lt;/a&gt;&amp;nbsp;also adds:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way to code repetitive tasks or create generic code is to use Macro processing. There are several ways to use the SAS Macro facility, from the simple &amp;amp;macvar reference (such as you would use in a title or footnote) to a complex macro program that is defined and then invoked as %macpgm(parm1=val1); (as just one example).&lt;BR /&gt;&lt;BR /&gt; I find that this paper is a good introduction to the macro facility and walks you through much of the basic understanding you will need to approach these tasks:&lt;BR /&gt;&lt;A href="http://www2.sas.com/proceedings/sugi28/056-28.pdf" target="_blank" rel="nofollow noopener noreferrer"&gt;http://www2.sas.com/proceedings/sugi28/056-28.pdf&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt; And then these papers are also aimed at macro beginners:&lt;BR /&gt;&lt;A href="http://www2.sas.com/proceedings/sugi29/243-29.pdf" target="_blank" rel="nofollow noopener noreferrer"&gt;http://www2.sas.com/proceedings/sugi29/243-29.pdf&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://www2.sas.com/proceedings/sugi26/p066-26.pdf" target="_blank" rel="nofollow noopener noreferrer"&gt;http://www2.sas.com/proceedings/sugi26/p066-26.pdf&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://www.nesug.org/Proceedings/nesug03/bt/bt009.pdf" target="_blank" rel="nofollow noopener noreferrer"&gt;http://www.nesug.org/Proceedings/nesug03/bt/bt009.&lt;WBR /&gt;pdf&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://www.ats.ucla.edu/stat/sas/library/nesug99/bt046.pdf" target="_blank" rel="nofollow noopener noreferrer"&gt;http://www.ats.ucla.edu/stat/sas/library/nesug99/b&lt;WBR /&gt;t046.pdf&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://www.gasug.org/papers/DemystifyingMacro_Fecht.pdf" target="_blank" rel="nofollow noopener noreferrer"&gt;http://www.gasug.org/papers/DemystifyingMacro_Fech&lt;WBR /&gt;t.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Sep 2016 19:18:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13489#M1595</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2016-09-22T19:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: DO loop in PROC SQL.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13490#M1596</link>
      <description>Many thanks!&lt;BR /&gt;
&lt;BR /&gt;
I still don't understand what you are getting at by proposing an index on MyDataSet. MyDataSet is my &lt;B&gt;original&lt;/B&gt; data set.&lt;BR /&gt;
&lt;BR /&gt;
Your second code alternative I will leave for supplementary studies.&lt;BR /&gt;
&lt;BR /&gt;
Well, then I have my 50 datasets, NewTable(1) - NewTable(50). I want to do the same kind of statistics on each dataset. Of course I don't want to code 50 new data steps: &lt;BR /&gt;
DATA Stat1; SET NewTable(1); /* some code */ run;&lt;BR /&gt;
...&lt;BR /&gt;
&lt;BR /&gt;
DATA Stat50; SET NewTable(50); /* some code */ run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Is it necessary to write a macro for that purpose also?&lt;BR /&gt;
&lt;BR /&gt;
Regards&lt;BR /&gt;
Anne</description>
      <pubDate>Wed, 01 Apr 2009 07:27:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13490#M1596</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-04-01T07:27:07Z</dc:date>
    </item>
    <item>
      <title>Re: DO loop in PROC SQL.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13491#M1597</link>
      <description>Hi:&lt;BR /&gt;
  One way to code repetitive tasks or create generic code is to use Macro processing. There are several ways to use the SAS Macro facility, from the simple &amp;amp;macvar reference (such as you would use in a title or footnote) to a complex macro program that is defined and then invoked as %macpgm(parm1=val1); (as just one example).&lt;BR /&gt;
 &lt;BR /&gt;
  I find that this paper is a good introduction to the macro facility and walks you through much of the basic understanding you will need to approach these tasks:&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/sugi28/056-28.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi28/056-28.pdf&lt;/A&gt;&lt;BR /&gt;
 &lt;BR /&gt;
  And then these papers are also aimed at macro beginners:&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/sugi29/243-29.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi29/243-29.pdf&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/sugi26/p066-26.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi26/p066-26.pdf&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://www.nesug.org/Proceedings/nesug03/bt/bt009.pdf" target="_blank"&gt;http://www.nesug.org/Proceedings/nesug03/bt/bt009.pdf&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://www.ats.ucla.edu/stat/sas/library/nesug99/bt046.pdf" target="_blank"&gt;http://www.ats.ucla.edu/stat/sas/library/nesug99/bt046.pdf&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://www.gasug.org/papers/DemystifyingMacro_Fecht.pdf" target="_blank"&gt;http://www.gasug.org/papers/DemystifyingMacro_Fecht.pdf&lt;/A&gt;&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Wed, 01 Apr 2009 16:43:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13491#M1597</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-04-01T16:43:49Z</dc:date>
    </item>
    <item>
      <title>Re: DO loop in PROC SQL.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13492#M1598</link>
      <description>The Stroupe paper was very pedagogic and instructive.&lt;BR /&gt;
&lt;BR /&gt;
I have only one question:&lt;BR /&gt;
&lt;BR /&gt;
On page 3, in the upper half of the right column, you can read:&lt;BR /&gt;
&lt;BR /&gt;
&lt;I&gt;To invoke the macro definition, use:&lt;BR /&gt;
%MacroName&lt;BR /&gt;
Notice there is &lt;B&gt;no semicolon&lt;/B&gt; at the end of the %VACATION&lt;BR /&gt;
statement. It is wise not to put a semicolon at the end of the&lt;BR /&gt;
macro call in case you end a program statement prematurely.&lt;/I&gt;.&lt;BR /&gt;
&lt;BR /&gt;
Could you elaborate on that, please.&lt;BR /&gt;
&lt;BR /&gt;
Regards&lt;BR /&gt;
Anne</description>
      <pubDate>Thu, 02 Apr 2009 07:29:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13492#M1598</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-04-02T07:29:51Z</dc:date>
    </item>
    <item>
      <title>Re: DO loop in PROC SQL.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13493#M1599</link>
      <description>Hi Anne,&lt;BR /&gt;
&lt;BR /&gt;
You may have created a piece of code which simply writes one word, for example a variable name, which you place in the middle of a SAS code statement, putting a colon after the macro call would end that statement. For example:&lt;BR /&gt;
&lt;BR /&gt;
%macro what_variable(a_or_b=);&lt;BR /&gt;
%if &amp;amp;a_or_b=a %then %do;&lt;BR /&gt;
variable_b&lt;BR /&gt;
%end;&lt;BR /&gt;
%else %do;&lt;BR /&gt;
variable_a&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend what_variable;&lt;BR /&gt;
&lt;BR /&gt;
Then using it you might have:&lt;BR /&gt;
&lt;BR /&gt;
data output(keep=%what_variable(a_or_b=b) &lt;BR /&gt;
                                     variable 1 variable2);&lt;BR /&gt;
set input;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
WHich would resolve to&lt;BR /&gt;
&lt;BR /&gt;
data output(keep=variable_b variable 1 variable2);&lt;BR /&gt;
set input;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
If you put a semi-colon after the macro call....&lt;BR /&gt;
&lt;BR /&gt;
data output(keep=%what_variable(a_or_b=b);&lt;BR /&gt;
                                    variable 1 variable2);&lt;BR /&gt;
set input;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Which would resolve to&lt;BR /&gt;
&lt;BR /&gt;
data output(keep=variable_b; variable 1 variable2);&lt;BR /&gt;
set input;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Which would give you an error.&lt;BR /&gt;
&lt;BR /&gt;
That is an extreme example but I think that's what they are getting at.&lt;BR /&gt;
&lt;BR /&gt;
Personally if I know the macro is going to complete the statement it's self then I put a semi colon in simply because if I don't it mucks up the colours in the editor.&lt;BR /&gt;
&lt;BR /&gt;
You hvae to know what the macro is doing though.</description>
      <pubDate>Thu, 02 Apr 2009 09:52:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13493#M1599</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-04-02T09:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: DO loop in PROC SQL.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13494#M1600</link>
      <description>Hi, Anne:&lt;BR /&gt;
  Generally, I agree with the statement in the paper, if I know that my macro is going to only generate part of a statement and I also know that my closing semi-colon is going to be correctly provided elsewhere. However, if I know that my macro is going to generate whole statements and complete steps, then I always end my macro call with a semi-colon.&lt;BR /&gt;
&lt;BR /&gt;
  One reason I got into this habit is that when I was coding programs for SAS/IntrNet and for stored processes, most macro calls needed to have a delimiter after them and without going into too much detail, if a macro did NOT have any parameters, the only way to provide a delimiter was to do:&lt;BR /&gt;
%macroname()  OR %macroname;&lt;BR /&gt;
 &lt;BR /&gt;
  I believe in thoroughly testing and debugging my starter, non-macroized, SAS program first, so I know whether it's appropirate to put a semicolon at the end of the macro call or not.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Thu, 02 Apr 2009 14:21:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/13494#M1600</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-04-02T14:21:27Z</dc:date>
    </item>
    <item>
      <title>Re: DO loop in PROC SQL.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/325356#M72358</link>
      <description>Hi,&lt;BR /&gt;The first piece of code is pretty straight forward since it loops from 1949 to 1999, each year. However, i do not know how the second proposition relates to that.&lt;BR /&gt;&lt;BR /&gt;I am wondering how we use the idea of the first piece of code, but instead of printing separate tables for each year, generate only one table containing all the years in one column and the whatever we SELECT to do in a second column. Thanks!</description>
      <pubDate>Tue, 17 Jan 2017 17:18:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-loop-in-PROC-SQL/m-p/325356#M72358</guid>
      <dc:creator>miguelito</dc:creator>
      <dc:date>2017-01-17T17:18:34Z</dc:date>
    </item>
  </channel>
</rss>

