<?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 Loops in SAS in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Loops-in-SAS/m-p/710683#M26797</link>
    <description>&lt;P&gt;I wish to perform the same procedure on a number of different data sets that all differ in name by a single naming element. I would like to run this using a loop, but am not sure how. I'd like to do the following, but do it for each of four data sets: data01, data02, data03, data04 and using a loop to read in each new data set. (This a simplified example of what I actually want to do with the data, but I am really only struggling to figure out how the loop works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc print data=data01; run;&lt;/PRE&gt;</description>
    <pubDate>Mon, 11 Jan 2021 21:29:31 GMT</pubDate>
    <dc:creator>raivester</dc:creator>
    <dc:date>2021-01-11T21:29:31Z</dc:date>
    <item>
      <title>Loops in SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Loops-in-SAS/m-p/710683#M26797</link>
      <description>&lt;P&gt;I wish to perform the same procedure on a number of different data sets that all differ in name by a single naming element. I would like to run this using a loop, but am not sure how. I'd like to do the following, but do it for each of four data sets: data01, data02, data03, data04 and using a loop to read in each new data set. (This a simplified example of what I actually want to do with the data, but I am really only struggling to figure out how the loop works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc print data=data01; run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Jan 2021 21:29:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Loops-in-SAS/m-p/710683#M26797</guid>
      <dc:creator>raivester</dc:creator>
      <dc:date>2021-01-11T21:29:31Z</dc:date>
    </item>
    <item>
      <title>Re: Loops in SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Loops-in-SAS/m-p/710684#M26798</link>
      <description>&lt;P&gt;No loops needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all;
    set data: indsname=indsname;
    dsname=indsname;
run;
proc print data=all;
    by dsname;
    pageby dsname;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Jan 2021 21:39:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Loops-in-SAS/m-p/710684#M26798</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-01-11T21:39:49Z</dc:date>
    </item>
    <item>
      <title>Re: Loops in SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Loops-in-SAS/m-p/710688#M26799</link>
      <description>&lt;P&gt;Often that requires macros but the first step is to have working code that works for a base case. If you're only changing the name of the data set that's actually quite a simple use case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Usually that gets you into macro language though, here's some references for you:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UCLA introductory tutorial on macro variables and macros&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Tutorial on converting a working program to a macro&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&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;BR /&gt;&lt;BR /&gt;Examples of common macro usage - e.g. looping using an index or data set list&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jan 2021 21:47:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Loops-in-SAS/m-p/710688#M26799</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-01-11T21:47:09Z</dc:date>
    </item>
    <item>
      <title>Re: Loops in SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Loops-in-SAS/m-p/710689#M26800</link>
      <description>&lt;P&gt;Here is an approach to developing something like you describe.&lt;/P&gt;
&lt;P&gt;The first data step creates small elements of code at a time and writes them out. The result, barring syntax errors, could be copied into the editor and run.&lt;/P&gt;
&lt;P&gt;The second makes a longer element to write to the results window and could also be copied to the editor to run. One alternate is to write to a specific output file instead of the File Print, such as File "c:\folder\mycodefile.sas" ;&lt;/P&gt;
&lt;P&gt;then use&amp;nbsp; " %include "c:\folder\mycodefile.sas" ; after the data step to call the written code. This has an advantage of creating a code file you can reuse or edit.&lt;/P&gt;
&lt;P&gt;The last example shows use of the Call Execute to place lines directly into the execution queue to run after the data step completes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _null_;
   file print;
   do i=1 to 4;
      dsname = cats("data",put(i,z2.));
      put "proc print data =";
      put dsname;
      put ";";
      put "run;";
   end;
run;

/* slightly fancier*/
data _null_;
   file print;
   length longstring $ 150;
   do i=1 to 4;
      dsname = cats("data",put(i,z2.));
      longstring = catx(' ', "proc print data =", dsname,';');
      put longstring;
      put "run;";
   end;
run;      

/* and to execute */
data _null_;
   file print;
   length longstring $ 150;
   do i=1 to 4;
      dsname = cats("data",put(i,z2.));
      longstring = catx(' ', "proc print data =", dsname,';');
      call execute(longstring);
      Call execute( "run;";);
   end;
run;  
&lt;/PRE&gt;
&lt;P&gt;Variations on this theme.&lt;/P&gt;
&lt;P&gt;Have parts of your needed code like names or options in variables and use the concatenation functions/operators to build of syntax strings.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note, if you aren't aware of it, SAS iterated Do loops in a data step can take character values when listed.&lt;/P&gt;
&lt;P&gt;Do i='first', 'second', 'third';&lt;/P&gt;
&lt;P&gt;in the above code would create data set names: datafirst, datasecond, datathird but the concatenation would use just the variable i instead of put(i, z2.) which was intended to duplicate your shown 01, 02 etc suffixes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would strongly suggest using one of the methods that write to a text program file and examine your generated code prior to executing it until you get some experience with the technique.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jan 2021 21:51:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Loops-in-SAS/m-p/710689#M26800</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-01-11T21:51:16Z</dc:date>
    </item>
  </channel>
</rss>

