<?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: How to make a loop over a column of a SAS table in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-make-a-loop-over-a-column-of-a-SAS-table/m-p/751323#M29671</link>
    <description>&lt;P&gt;PROC CONTENTS in not a "function".&amp;nbsp; It is a procedure.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to use data to generate code there are many ways.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use CALL EXECUTE().&amp;nbsp; Basically generate a string (or series of strings) and use CALL EXECUTE() to have SAS stack them up to run when your current data step ends.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set products;
  call execute(cats('proc contents data=',product_name,';run;'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can use use normal SAS data statements to write a file that has the code you want to run and then use %INCLUDE statement to include that new file as source code. This is much easier to debug as you can review the file and make sure the code is being generate with the right syntax before running the %include.&amp;nbsp; Plus you have access to all of the power of the PUT statement to generate the code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set products;
  file code;
  put 'proc contents data=' product_name ';run;';
run;
%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There is a newer (and in may ways still experimental) DOSUBL() function that works a lot like CALL EXECUTE() but it actually allows the generated code to run immediately in a sub-process.&amp;nbsp; But usually this is not worth the effort.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a really complicated series of steps you want to execute for every observation in your data then encapsulate them into a macro and in your code generation step simply generate a call to the macro as the code you are generating.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymacro(product_name);
proc contents data=&amp;amp;product_name;
run;
%mend mymacro;

filename code temp;
data _null_;
  set products;
  file code;
  put '%mymacro(' product_name= ')';
run;
%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Resulting generated code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%mymacro(product_name=dt1 )
%mymacro(product_name=dt2 )
%mymacro(product_name=dt3 )
%mymacro(product_name=dt4 )
%mymacro(product_name=dt5 )
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 30 Jun 2021 18:37:27 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-06-30T18:37:27Z</dc:date>
    <item>
      <title>How to make a loop over a column of a SAS table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-make-a-loop-over-a-column-of-a-SAS-table/m-p/751313#M29669</link>
      <description>&lt;P&gt;I know this is a basic question, but the answer to the question can help me understand how loops work in SAS.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 5 different datasets named "dt1", "dt2",..., "dt5"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data products;
input product_name $ ;
datalines;
dt1
dt2
dt3
dt4
dt5
;
proc print data= products;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have faced many occasions that I needed to go through rows of a table and apply a function on them. Let's say I want to do a very simple step with these datasets for instance I want do &lt;STRONG&gt;proc contents&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;The code I wrote is as following:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro first_loop;
 %do iterate = 1 %to 5;
   data _null_;
   set products ;
   call symput ("prd", trim(left(product_name);
 %put &amp;amp;prd;  
   proc contents data = &amp;amp;prd ;
   run;

%end; 
%mend;&lt;BR /&gt;&lt;BR /&gt;%first_loop&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Obviously this code doesn't provide the result and log shows that &amp;amp;prd is not well defined, but I don't know how to pass the column name. Any help would be appreciated.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jun 2021 17:20:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-make-a-loop-over-a-column-of-a-SAS-table/m-p/751313#M29669</guid>
      <dc:creator>Al_senior</dc:creator>
      <dc:date>2021-06-30T17:20:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to make a loop over a column of a SAS table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-make-a-loop-over-a-column-of-a-SAS-table/m-p/751314#M29670</link>
      <description>&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n1q1527d51eivsn1ob5hnz0yd1hx.htm" target="_self"&gt;CALL EXECUTE()&lt;/A&gt;/DOSUBL() or Macros&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data products;
input product_name $ ;
str = catt('proc contents data=', product_name, '; run;');
call execute(str);
datalines;
dt1
dt2
dt3
dt4
dt5
;
&lt;/CODE&gt;&lt;/PRE&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&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;
&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/387876"&gt;@Al_senior&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I know this is a basic question, but the answer to the question can help me understand how loops work in SAS.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have 5 different datasets named "dt1", "dt2",..., "dt5"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data products;
input product_name $ ;
datalines;
dt1
dt2
dt3
dt4
dt5
;
proc print data= products;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have faced many occasions that I needed to go through rows of a table and apply a function on them. Let's say I want to do a very simple step with these datasets for instance I want do &lt;STRONG&gt;proc contents&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;The code I wrote is as following:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro first_loop;
 %do iterate = 1 %to 5;
   data _null_;
   set products ;
   call symput ("prd", trim(left(product_name);
 %put &amp;amp;prd;  
   proc contents data = &amp;amp;prd ;
   run;

%end; 
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Obviously this code doesn't provide the result and log shows that &amp;amp;prd is not well defined, but I don't know how to pass the column name. Any help would be appreciated.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jun 2021 17:22:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-make-a-loop-over-a-column-of-a-SAS-table/m-p/751314#M29670</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-30T17:22:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to make a loop over a column of a SAS table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-make-a-loop-over-a-column-of-a-SAS-table/m-p/751323#M29671</link>
      <description>&lt;P&gt;PROC CONTENTS in not a "function".&amp;nbsp; It is a procedure.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to use data to generate code there are many ways.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use CALL EXECUTE().&amp;nbsp; Basically generate a string (or series of strings) and use CALL EXECUTE() to have SAS stack them up to run when your current data step ends.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set products;
  call execute(cats('proc contents data=',product_name,';run;'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can use use normal SAS data statements to write a file that has the code you want to run and then use %INCLUDE statement to include that new file as source code. This is much easier to debug as you can review the file and make sure the code is being generate with the right syntax before running the %include.&amp;nbsp; Plus you have access to all of the power of the PUT statement to generate the code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set products;
  file code;
  put 'proc contents data=' product_name ';run;';
run;
%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There is a newer (and in may ways still experimental) DOSUBL() function that works a lot like CALL EXECUTE() but it actually allows the generated code to run immediately in a sub-process.&amp;nbsp; But usually this is not worth the effort.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a really complicated series of steps you want to execute for every observation in your data then encapsulate them into a macro and in your code generation step simply generate a call to the macro as the code you are generating.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymacro(product_name);
proc contents data=&amp;amp;product_name;
run;
%mend mymacro;

filename code temp;
data _null_;
  set products;
  file code;
  put '%mymacro(' product_name= ')';
run;
%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Resulting generated code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%mymacro(product_name=dt1 )
%mymacro(product_name=dt2 )
%mymacro(product_name=dt3 )
%mymacro(product_name=dt4 )
%mymacro(product_name=dt5 )
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Jun 2021 18:37:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-make-a-loop-over-a-column-of-a-SAS-table/m-p/751323#M29671</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-30T18:37:27Z</dc:date>
    </item>
  </channel>
</rss>

