<?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 loop over multiple tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673375#M202569</link>
    <description>&lt;P&gt;Thank you Kurt!&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The first part of the code works fine. The last part of the code doesn't work for me. Not quite sure if i'm understanding it correct, but I want to generate 5 new datasets with only pnr, x and y saved as UDDA2002, UDDA2003, UDDA2004, UDDA2005 and UDDA2006&amp;nbsp;on a new location.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two libnames:&lt;/P&gt;&lt;P&gt;origin and rawdata. Origin is where I gets the data from and rawdata is where I want to locate the new extracted UDDA data of pnr, x and y.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have&amp;nbsp;tried this:&lt;/P&gt;&lt;P&gt;data rawdata.want&lt;/P&gt;&lt;P&gt;set origin.&amp;amp;datasets.;&lt;/P&gt;&lt;P&gt;keep pnr x y;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then the log tells me that work.UDDA2002 etc. does not exist.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards Frank&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 30 Jul 2020 11:09:12 GMT</pubDate>
    <dc:creator>Kjeldsen89</dc:creator>
    <dc:date>2020-07-30T11:09:12Z</dc:date>
    <item>
      <title>Do loop over multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673372#M202566</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I'm quite new to the SAS platform.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to make a data extract from some tables with identical names, but with different years as prefix, ex&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;UDDA2002&lt;/P&gt;&lt;P&gt;UDDA2003&lt;/P&gt;&lt;P&gt;UDDA2004&lt;BR /&gt;UDDA2005&lt;/P&gt;&lt;P&gt;UDDA2006&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have to extract the exact same variables from the different tables, but instead of doing it manually I was wondering if I could do this in a do-loop instead?&lt;/P&gt;&lt;P&gt;lets say I want to extract the variables pnr, x and y from the tables and save them as UDDA2004, UDDA2005 etc. on a different location than my import location.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards Frank.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jul 2020 10:07:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673372#M202566</guid>
      <dc:creator>Kjeldsen89</dc:creator>
      <dc:date>2020-07-30T10:07:44Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop over multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673373#M202567</link>
      <description>&lt;P&gt;First, create a string with dataset names that you can use in a &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=p00hxg3x8lwivcn1f0e9axziw57y.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;SET Statement&lt;/A&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
length string $32767;
do year = 2002 to 2006;
  string = catx(' ',string,cats('UDDA',year));
end;
call symputx('datasets',string,'g');
run;

data want;
set &amp;amp;datasets.;
keep pnr x y;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also see the following documentation:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=p1cydk5fq0u4bfn1xfbjt7w1c7lu.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;DO Statement: Iterative&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n1e21rr6al5m2nn19r1fat5qxwrt.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;CATS Function&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n0p7wxtk0hvn83n1pveisbcp2ae9.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;CATX Function&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n1nexcs36ctqk5n11uao7k9myz7y.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;CALL SYMPUTX Routine&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=n1nnrzzsw6rzrjn1p2jfky6pdv23.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;KEEP Statement&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jul 2020 10:31:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673373#M202567</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-30T10:31:20Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop over multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673375#M202569</link>
      <description>&lt;P&gt;Thank you Kurt!&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The first part of the code works fine. The last part of the code doesn't work for me. Not quite sure if i'm understanding it correct, but I want to generate 5 new datasets with only pnr, x and y saved as UDDA2002, UDDA2003, UDDA2004, UDDA2005 and UDDA2006&amp;nbsp;on a new location.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two libnames:&lt;/P&gt;&lt;P&gt;origin and rawdata. Origin is where I gets the data from and rawdata is where I want to locate the new extracted UDDA data of pnr, x and y.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have&amp;nbsp;tried this:&lt;/P&gt;&lt;P&gt;data rawdata.want&lt;/P&gt;&lt;P&gt;set origin.&amp;amp;datasets.;&lt;/P&gt;&lt;P&gt;keep pnr x y;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then the log tells me that work.UDDA2002 etc. does not exist.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards Frank&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jul 2020 11:09:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673375#M202569</guid>
      <dc:creator>Kjeldsen89</dc:creator>
      <dc:date>2020-07-30T11:09:12Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop over multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673376#M202570</link>
      <description>&lt;P&gt;The fact that you have those datasets in libraries outside of WORK is crucial information and should have been included in your question.&lt;/P&gt;
&lt;P&gt;Since each entry in the SET statement needs to completely describe (including the libname) the dataset in question, the library needs to be included when the string is built:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
length string $32767;
do year = 2002 to 2006;
  string = catx(' ',string,cats('origin.UDDA',year));
end;
call symputx('datasets',string,'g');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jul 2020 11:15:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673376#M202570</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-30T11:15:30Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop over multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673379#M202572</link>
      <description>&lt;P&gt;My fault! I will remember that next time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seemes like it have appended the UDDA2002-UDDA2006 instead of saving them as 6 new seperate datasets. Is there any way I can fix this in the data step? maybe use the string &amp;amp;datasets l just defined?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards Frank&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jul 2020 11:23:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673379#M202572</guid>
      <dc:creator>Kjeldsen89</dc:creator>
      <dc:date>2020-07-30T11:23:57Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop over multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673380#M202573</link>
      <description>&lt;P&gt;That's where I misread your question (better: not read your question completely), my bad.&lt;/P&gt;
&lt;P&gt;So we need to run the same data step repeatedly, so we will use a macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro extract(start,end);
%do year = &amp;amp;start. %to &amp;amp;end.;

data rawdata.udda&amp;amp;year.;
set&amp;nbsp;origin.udda&amp;amp;year.&amp;nbsp;(keep=pnr&amp;nbsp;x&amp;nbsp;y);
run;

%end;
%mend;
%extract(2002,2006)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jul 2020 11:31:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673380#M202573</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-30T11:31:23Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop over multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673382#M202574</link>
      <description>&lt;P&gt;PERFECT!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jul 2020 11:45:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-over-multiple-tables/m-p/673382#M202574</guid>
      <dc:creator>Kjeldsen89</dc:creator>
      <dc:date>2020-07-30T11:45:54Z</dc:date>
    </item>
  </channel>
</rss>

