<?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: sas loop over datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492703#M129521</link>
    <description>&lt;P&gt;I am new to sas, so I don't understand your code template.&lt;/P&gt;</description>
    <pubDate>Wed, 05 Sep 2018 15:25:09 GMT</pubDate>
    <dc:creator>joaolopes</dc:creator>
    <dc:date>2018-09-05T15:25:09Z</dc:date>
    <item>
      <title>sas loop over datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492696#M129519</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to import from a database the datasets a, b, c, d, e, f&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can do the following to import it&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%imp(a, a);&lt;BR /&gt;%imp(b, b);&lt;BR /&gt;%imp(c, c);&lt;BR /&gt;%imp(d, d);&lt;BR /&gt;%imp(e, e);&lt;BR /&gt;%imp(f, f);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please can anyone tell me if there is a simple way to create a list and then do a loop over the list?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;something like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let name_list= a b c d e f
%local i next_name;
%do i=1 %to %sysfunc(countw(&amp;amp;name_list));
   %let next_name = %scan(&amp;amp;name_list, &amp;amp;i);
   %imp(&amp;amp;next_name, &amp;amp;next_name);
%end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This command does not work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me with this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Sep 2018 15:12:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492696#M129519</guid>
      <dc:creator>joaolopes</dc:creator>
      <dc:date>2018-09-05T15:12:42Z</dc:date>
    </item>
    <item>
      <title>Re: sas loop over datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492700#M129520</link>
      <description>&lt;P&gt;Use a data step and CALL EXECUTE or DOSUBL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See the last step in this example:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And another that's a fully worked example:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/beb97b1c6d4517dde3b2" target="_blank"&gt;https://gist.github.com/statgeek/beb97b1c6d4517dde3b2&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There's several steps included here primarily to illustrate what's happening. You can simplify this code/reduce the steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/********************************************************************
Example : Call macro using parameters from data set
********************************************************************/

*define macro to be called later;
%macro summary(age=, sex=);

proc print data=sashelp.class;
	where age=&amp;amp;age and sex="&amp;amp;sex";
run;

%mend;

*sort data to ensure correct order for next steps;
proc sort data=sashelp.class out=class;
by age sex;
run;

*create macro string to be called in next step. Note the macro is called at the last of each reord;
data sample;
set class;
by age sex;

if last.sex;
string =
    catt('%summary(age=', age, ',sex=', sex, ');');
put string;
run;

*Call macro - can be done in previous step but broken out here for demonstration purposes;
data _null_;
set sample;
call execute(string);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Sep 2018 15:20:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492700#M129520</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-05T15:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: sas loop over datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492703#M129521</link>
      <description>&lt;P&gt;I am new to sas, so I don't understand your code template.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Sep 2018 15:25:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492703#M129521</guid>
      <dc:creator>joaolopes</dc:creator>
      <dc:date>2018-09-05T15:25:09Z</dc:date>
    </item>
    <item>
      <title>Re: sas loop over datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492705#M129522</link>
      <description>&lt;P&gt;Did you read the comments and instructions? Or run it? You should be able to run it and check each step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code should be pretty similar to what you're doing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a something specific you don't understand?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/153664"&gt;@joaolopes&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am new to sas, so I don't understand your code template.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Sep 2018 15:26:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492705#M129522</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-05T15:26:52Z</dc:date>
    </item>
    <item>
      <title>Re: sas loop over datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492706#M129523</link>
      <description>&lt;P&gt;a b c d e f are datasets that I want to import, not variables&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In your code the example is sex and age which seem to be variables, not datasets.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am probably confused about this&lt;/P&gt;</description>
      <pubDate>Wed, 05 Sep 2018 15:31:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492706#M129523</guid>
      <dc:creator>joaolopes</dc:creator>
      <dc:date>2018-09-05T15:31:43Z</dc:date>
    </item>
    <item>
      <title>Re: sas loop over datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492707#M129524</link>
      <description>&lt;P&gt;For someone who is new to SAS, you have done well to get this far.&amp;nbsp; Two issues ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In calling the macro, you would call it with just a single parameter:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%imp(&amp;amp;next_name)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The semicolon is not needed.&amp;nbsp; Some prefer it as a matter of style.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The major issue is that you can't use %DO just anywhere.&amp;nbsp; It has to appear within a macro definition.&amp;nbsp; So first define the macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro do_all (name_list=);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;%* Your logic goes here ... everything you posted except for the first %LET statement;&lt;/P&gt;
&lt;P&gt;%mend do_all;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then call the macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do_all (name_list=a b c d e f)&lt;/P&gt;</description>
      <pubDate>Wed, 05 Sep 2018 15:31:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492707#M129524</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-09-05T15:31:43Z</dc:date>
    </item>
    <item>
      <title>Re: sas loop over datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492709#M129525</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Step1: Make a data set of all the variables you need to call your macro&lt;/P&gt;
&lt;P&gt;Step 2: Make a string that looks like your macro call, %imp(a, a)&lt;/P&gt;
&lt;P&gt;Step 3: Use CALL EXECUTE to run the string.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data list_datasets;
&lt;FONT size="6"&gt;&lt;STRONG&gt;&lt;FONT color="#800080"&gt;*step 1;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;
input var1 $ var2 $;

&lt;FONT size="6" color="#800000"&gt;&lt;STRONG&gt;*step 2;&lt;/STRONG&gt;&lt;/FONT&gt;
*create a string that looks like your macro call;
str = catt('%imp(', var1, ',', var2, ');');
cards;
a a
b b
c c
d d
e e
f f
;
run;



data run_macro;
set list_datasets;
&lt;FONT size="6" color="#800000"&gt;*step 3;&lt;/FONT&gt;
*will run macro for every str line in previous data set;
call execute(string);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should run any of the demo's I posted to see how that works fully, but the solution to the question you asked is above.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, why have two parameters that are the same? That seems redundant at best.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/153664"&gt;@joaolopes&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to import from a database the datasets a, b, c, d, e, f&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can do the following to import it&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%imp(a, a);&lt;BR /&gt;%imp(b, b);&lt;BR /&gt;%imp(c, c);&lt;BR /&gt;%imp(d, d);&lt;BR /&gt;%imp(e, e);&lt;BR /&gt;%imp(f, f);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please can anyone tell me if there is a simple way to create a list and then do a loop over the list?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;something like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let name_list= a b c d e f
%local i next_name;
%do i=1 %to %sysfunc(countw(&amp;amp;name_list));
   %let next_name = %scan(&amp;amp;name_list, &amp;amp;i);
   %imp(&amp;amp;next_name, &amp;amp;next_name);
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This command does not work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me with this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Sep 2018 15:39:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492709#M129525</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-05T15:39:14Z</dc:date>
    </item>
    <item>
      <title>Re: sas loop over datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492711#M129526</link>
      <description>&lt;P&gt;This worked! Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My final code is the following&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro imp(outdset,intable);
	proc sql;
		connect to odbc (dsn='trialdata');
		create table sas_data.&amp;amp;outdset as select * from connection to odbc(select * from &amp;amp;intable);
	quit;
%mend imp;

%macro do_all (name_list=);
%local i next_name;
%do i=1 %to %sysfunc(countw(&amp;amp;name_list));
   %let next_name = %scan(&amp;amp;name_list, &amp;amp;i);
   %imp(&amp;amp;next_name, &amp;amp;next_name);
%end;
%mend do_all;
%do_all (name_list=a b c d e f);

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Sep 2018 15:45:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492711#M129526</guid>
      <dc:creator>joaolopes</dc:creator>
      <dc:date>2018-09-05T15:45:11Z</dc:date>
    </item>
    <item>
      <title>Re: sas loop over datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492712#M129527</link>
      <description>&lt;P&gt;Thanks for your help&lt;/P&gt;</description>
      <pubDate>Wed, 05 Sep 2018 15:41:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-loop-over-datasets/m-p/492712#M129527</guid>
      <dc:creator>joaolopes</dc:creator>
      <dc:date>2018-09-05T15:41:22Z</dc:date>
    </item>
  </channel>
</rss>

