<?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: For loop a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/For-loop-a-macro/m-p/761234#M240834</link>
    <description>&lt;P&gt;My concern doing all variables in PROC MEANS is that not all the variables require the same formatting for the same statistic. There would be a lot of columns to assign a format to.&lt;/P&gt;</description>
    <pubDate>Thu, 12 Aug 2021 18:38:19 GMT</pubDate>
    <dc:creator>mariko5797</dc:creator>
    <dc:date>2021-08-12T18:38:19Z</dc:date>
    <item>
      <title>For loop a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-loop-a-macro/m-p/761213#M240826</link>
      <description>&lt;P&gt;I created a basic macro with SAS procedures to run through PROC MEANS and assign formats using an array. See a very rough outline below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro(var, fmt1, fmt2);
   proc means data = have;
     class BMI;
     var &amp;amp;var;
     output out = stats_&amp;amp;var;
  run;

  data stats_&amp;amp;var;
     set stats_&amp;amp;var;
     array.......
  run;
  proc sort...; run;
  proc transpose data = stats_&amp;amp;var out = stats2_&amp;amp;var;
    by name;
    var value;
    id BMI;
  run;
%mend

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would like to run this macro repeated for a set of variables. Rather than filling in the macro multiple times, I want to run it through a do loop. I have a vague memory of doing this in the past. Something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop(variables);
  %scan();
     %stats(var = &amp;amp;variables, fmt1 = f5.2, fmt2 = f6.1);
%mend;&lt;BR /&gt;&lt;BR /&gt;%loop&amp;nbsp;(WGT,&amp;nbsp;HGT,&amp;nbsp;BMI);&amp;nbsp;/*variables&amp;nbsp;weight,&amp;nbsp;height,&amp;nbsp;and&amp;nbsp;bmi&amp;nbsp;will&amp;nbsp;run&amp;nbsp;through&amp;nbsp;the&amp;nbsp;macro*/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'm not familiar with loops, so I'm not sure all the macro functions required to run. Can someone help? Thank you in advance.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Aug 2021 17:26:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-loop-a-macro/m-p/761213#M240826</guid>
      <dc:creator>mariko5797</dc:creator>
      <dc:date>2021-08-12T17:26:45Z</dc:date>
    </item>
    <item>
      <title>Re: For loop a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-loop-a-macro/m-p/761218#M240827</link>
      <description>&lt;P&gt;Your proposed solution seems suboptimal.&amp;nbsp; If you are going to generate the same statistics from PROC MEANS then generate them for ALL of the variable of interest at once.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But to your question: to iterate over a list just use a DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop(variables);
%local i var;
%do i=1 %to %sysfunc(countw(&amp;amp;variables,%str( )));
  %let var=%scan(&amp;amp;variables,&amp;amp;i,%str( ));
  %stats(var = &amp;amp;var, fmt1 = f5.2, fmt2 = f6.1)
%end;
%mend;
%loop(variables=WGT HGT BMI)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Aug 2021 17:36:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-loop-a-macro/m-p/761218#M240827</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-08-12T17:36:03Z</dc:date>
    </item>
    <item>
      <title>Re: For loop a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-loop-a-macro/m-p/761234#M240834</link>
      <description>&lt;P&gt;My concern doing all variables in PROC MEANS is that not all the variables require the same formatting for the same statistic. There would be a lot of columns to assign a format to.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Aug 2021 18:38:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-loop-a-macro/m-p/761234#M240834</guid>
      <dc:creator>mariko5797</dc:creator>
      <dc:date>2021-08-12T18:38:19Z</dc:date>
    </item>
    <item>
      <title>Re: For loop a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-loop-a-macro/m-p/761237#M240836</link>
      <description>&lt;P&gt;Your example assigns the same format to every call of the macro, and then that macro doesn't even use those formats.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's seems to me that doing this via a macro is not more efficient, or any less typing, than doing it without a macro and assigning formats in a FORMAT statement. One way or another, you will have to type out the formats, having a macro does not eliminate the need to specify formats. And if you do this without a macro, and call one PROC MEANS that works on all of your variables in &amp;amp;VARIABLE, this is much more efficient in terms of run time (and in terms of programming) than a macro that calls PROC MEANS once for each variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And lastly, without a macro, it is much easier to program!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, if it was me, I would forget the macro idea for this call to PROC MEANS&lt;/P&gt;</description>
      <pubDate>Thu, 12 Aug 2021 19:10:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-loop-a-macro/m-p/761237#M240836</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-12T19:10:53Z</dc:date>
    </item>
    <item>
      <title>Re: For loop a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-loop-a-macro/m-p/761256#M240852</link>
      <description>&lt;P&gt;Large economy sized hint: Provide an example data in the form of data step code or use one of the SAS supplied data sets like SASHELP.CLASS or SASHELP.CARS and show what you expect.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you sure that you need a data set at all? The sort of statistics you are requesting (by not listing any) are available in the reporting procedures Tabulate and Report.&lt;/P&gt;
&lt;P&gt;Examine this data set output:&lt;/P&gt;
&lt;PRE&gt;proc means data=sashelp.class;
   class sex;
   var height weight;
   output out=stats;
run;&lt;/PRE&gt;
&lt;P&gt;And similar output from proc tabulate but specifying different formats for the same statistic for different variables&lt;/P&gt;
&lt;PRE&gt;proc tabulate data=sashelp.class;
   class sex;
   var height weight;
   tables height *(n mean*f=8.3  max*f=6.1 min*f=best5. std*f=8.5)
          weight *(n mean*f=9.5  max*f=4.0 min*f=best3. std*f=12.8)
          , All='All' Sex
   ;
run;
&lt;/PRE&gt;
&lt;P&gt;Also, Proc tabulate can have multiple table statements.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Aug 2021 19:48:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-loop-a-macro/m-p/761256#M240852</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-08-12T19:48:59Z</dc:date>
    </item>
  </channel>
</rss>

