<?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 How to repeat a macro for five levels of a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-repeat-a-macro-for-five-levels-of-a-variable/m-p/739315#M230728</link>
    <description>&lt;P&gt;I want to produce these outputs for the 5 levels of a LEVELS variable (BY statement does not work for the next steps I have in my macro). I tried a %do statement by that was not working. May you please help me with this? Thanks&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;%macro do_sf(varname,levels);
&lt;STRONG&gt;%do i = 1 %to 5;&lt;/STRONG&gt;
proc surveyfreq data = have VARHEADER = NAMELABEL nosummary;
    tables &amp;amp;varname /cl nofreq nostd; 
    WHERE &amp;amp;levels =&lt;STRONG&gt;&amp;amp;i&lt;/STRONG&gt;;
 weight weight_var;
run;&lt;BR /&gt;%end;
%mend;
%do_sf(coffee_cons_level,city);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 05 May 2021 19:17:39 GMT</pubDate>
    <dc:creator>Emma_at_SAS</dc:creator>
    <dc:date>2021-05-05T19:17:39Z</dc:date>
    <item>
      <title>How to repeat a macro for five levels of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-repeat-a-macro-for-five-levels-of-a-variable/m-p/739315#M230728</link>
      <description>&lt;P&gt;I want to produce these outputs for the 5 levels of a LEVELS variable (BY statement does not work for the next steps I have in my macro). I tried a %do statement by that was not working. May you please help me with this? Thanks&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;%macro do_sf(varname,levels);
&lt;STRONG&gt;%do i = 1 %to 5;&lt;/STRONG&gt;
proc surveyfreq data = have VARHEADER = NAMELABEL nosummary;
    tables &amp;amp;varname /cl nofreq nostd; 
    WHERE &amp;amp;levels =&lt;STRONG&gt;&amp;amp;i&lt;/STRONG&gt;;
 weight weight_var;
run;&lt;BR /&gt;%end;
%mend;
%do_sf(coffee_cons_level,city);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 19:17:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-repeat-a-macro-for-five-levels-of-a-variable/m-p/739315#M230728</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-05-05T19:17:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to repeat a macro for five levels of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-repeat-a-macro-for-five-levels-of-a-variable/m-p/739322#M230732</link>
      <description>&lt;P&gt;So the variable city has values from 1 to 5?&lt;/P&gt;
&lt;P&gt;Please post your log.&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 19:31:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-repeat-a-macro-for-five-levels-of-a-variable/m-p/739322#M230732</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-05T19:31:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to repeat a macro for five levels of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-repeat-a-macro-for-five-levels-of-a-variable/m-p/739324#M230734</link>
      <description>&lt;P&gt;Thank you,&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A class="trigger-hovercard" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562" target="_blank"&gt;KurtBremser&lt;/A&gt;. Yes, the variable city has five levels from 1 to 5. My log repeats the following 5 times.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NOTE: The input data set is subset by WHERE, OBS, or FIRSTOBS. This provides a completely&amp;nbsp;separate analysis of the subset. It does not provide a statistically valid subpopulation&amp;nbsp;or domain analysis, where the total number of units in the subpopulation is not known&amp;nbsp;with certainty. If you want a domain analysis, you should include the domain variables&amp;nbsp;in the TABLES request.&lt;BR /&gt;NOTE: PROCEDURE SURVEYFREQ used (Total process time):&lt;BR /&gt;real time 0.02 seconds&lt;BR /&gt;cpu time 0.00 seconds&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 19:36:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-repeat-a-macro-for-five-levels-of-a-variable/m-p/739324#M230734</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-05-05T19:36:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to repeat a macro for five levels of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-repeat-a-macro-for-five-levels-of-a-variable/m-p/739365#M230750</link>
      <description>&lt;P&gt;That is not the complete log. It doesn't show anything about how many observations were used or other bits expected. You should also set OPTIONS MPRINT; before running the macro to see what is generated and get notes in context to more details.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does your variable City actually have &lt;STRONG&gt;numeric&lt;/STRONG&gt; values 1, 2, 3, 4 and 5? That is what you are asking for&lt;/P&gt;
&lt;P&gt;with&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt; WHERE &amp;amp;levels =&amp;amp;i;&lt;/LI-CODE&gt;
&lt;P&gt;If City is character you need quotes around the value such as:&amp;nbsp; &amp;amp;levels= "&amp;amp;i." ;&lt;/P&gt;
&lt;P&gt;Suggestion: Add your &amp;amp;levels variable to the TABLES statement, drop the whole bit of looping in a macro and use ODS OUTPUT to place all the desired output into a data set.&lt;/P&gt;
&lt;P&gt;You could use (without a macro) to get analysis of these combinations.&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;proc surveyfreq data = have VARHEADER = NAMELABEL nosummary;
    tables coffee_cons_level * city /cl nofreq nostd; 
    WHERE city in (1:5);/* if city is a numeric variable*/
/*  Where city in ('1' '2' '3' '4' '5'); if city is character*/
 weight weight_var;
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have multiple variables you need to do such with the tables statement will accept syntax like&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;tables (a b c) *(x y z)&lt;/FONT&gt; .&lt;/P&gt;
&lt;P&gt;Which will create output for a*x a*y a*z b*x b*y b*z c*x c*y c*z&amp;nbsp; (nine tables in this case). Placing the output into a data set allows for later report writing or selecting the desired records for detailed examination.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So any looping or the macro may not be needed at all.&lt;/P&gt;
&lt;P&gt;If your data set is of any significant size this sort of loop requires reloading the data set an may cause a serious performance issue, especially when you start calling it with multiple combinations of different variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now:&lt;/P&gt;
&lt;P&gt;"Not working" is awful vague.&lt;BR /&gt;&lt;BR /&gt;Are there errors in the log?: Post the code and log in a code box opened with the "&amp;lt;&amp;gt;" to maintain formatting of error messages.&lt;BR /&gt;&lt;BR /&gt;No output? Post any log in a code box.&lt;BR /&gt;&lt;BR /&gt;Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "&amp;lt;/&amp;gt;" icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 22:23:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-repeat-a-macro-for-five-levels-of-a-variable/m-p/739365#M230750</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-05T22:23:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to repeat a macro for five levels of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-repeat-a-macro-for-five-levels-of-a-variable/m-p/739375#M230756</link>
      <description>&lt;P&gt;Shouldn't you use DOMAIN analysis instead?&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/statug/15.2/statug_surveyfreq_details06.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/statug/15.2/statug_surveyfreq_details06.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Perhaps something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro do_sf(varname,levels);
proc surveyfreq data = have VARHEADER = NAMELABEL nosummary;
  tables &amp;amp;levels*&amp;amp;varname /cl nofreq nostd domain=row; 
  weight weight_var;
run;
%mend;
%do_sf(coffee_cons_level,city);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 May 2021 00:14:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-repeat-a-macro-for-five-levels-of-a-variable/m-p/739375#M230756</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-06T00:14:04Z</dc:date>
    </item>
  </channel>
</rss>

