<?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: help with writing a macro to cycle through different combinations of variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/help-with-writing-a-macro-to-cycle-through-different/m-p/886907#M350445</link>
    <description>&lt;P&gt;I wouldn't spend much time thinking about this request without example data.&lt;/P&gt;
&lt;P&gt;This is also a lot like "please do my work for me" not a "help me with this bit I can't figure out". Which is where I start at $100 custom programming (or more, depends).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you have at least 3 syntax errors in 13 lines of code things don't bode well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you are discussing dates and intervals can we even hope that your current data has SAS date variables involved?&lt;/P&gt;</description>
    <pubDate>Fri, 28 Jul 2023 20:32:04 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-07-28T20:32:04Z</dc:date>
    <item>
      <title>help with writing a macro to cycle through different combinations of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-writing-a-macro-to-cycle-through-different/m-p/886836#M350412</link>
      <description>&lt;P&gt;Hi, I'm very new to macro writing. Thanks in advance.&amp;nbsp; I appreciate this community.&lt;/P&gt;
&lt;P&gt;I'd like to write a code that can cycle through combinations of:&lt;/P&gt;
&lt;P&gt;year; max, min, and average pay (and their associated graph labels); and months of the year (and their associated graph labels)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;without me having to change the combinations manually in the macro myself.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll paste my code and a bit of an example of the combinations below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%let Year = first;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /*looking at 10 years of data*/&amp;nbsp;
%let payv=pay_Average; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /*pay variable*/&amp;nbsp;
%let payv =pay_Minimum;&amp;nbsp;
%let payv =pay_Maximum;&amp;nbsp;
%let payv _label=Average Pay ; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /*pay variable for figure labels*/&amp;nbsp;
%let payv _label=Minimum Pay ;&amp;nbsp;
%let payv _label=Maximum Pay ;&amp;nbsp;
%let spendv= spend; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /*money spent variable */&amp;nbsp;
%let spendv_label = money spent&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /*money spent label for figures*/&amp;nbsp;
%let months_range=BETWEEN 1 and 6;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /*Set months range here */&amp;nbsp;
%let months_range=BETWEEN 7 and 12;&amp;nbsp;
%let months_label=Jan to June; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /*Months used for figure labels*/&amp;nbsp;
%let months_label=July to Dec;&amp;nbsp;

&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN data-contrast="auto"&gt;Three examples (out of many) of what I want the macro to do without having to manually type in the combinations:&lt;/SPAN&gt;&lt;SPAN data-ccp-props="{&amp;quot;201341983&amp;quot;:0,&amp;quot;335559739&amp;quot;:0,&amp;quot;335559740&amp;quot;:240}"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN data-contrast="auto"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;SPAN data-contrast="auto"&gt; The programme should output money spent for the first year of the average pay and its label on the figure should match this (i.e., payv_label is Average Pay). This will be for months 1 to 6 and its label on the figure should match this (i.e., months_label is Jan to June).&lt;/SPAN&gt;&lt;SPAN data-ccp-props="{&amp;quot;201341983&amp;quot;:0,&amp;quot;335559739&amp;quot;:0,&amp;quot;335559740&amp;quot;:240}"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;OL start="2"&gt;
&lt;LI&gt;&lt;SPAN data-contrast="auto"&gt; The programme should output money spent for the first year of minimum pay and its label on the figure should match this (i.e., payv_label is Minimum Pay). This will be for months 1 to 6 and its label on the figure should match this (i.e., months_label is Jan to June).&lt;/SPAN&gt;&lt;SPAN data-ccp-props="{&amp;quot;201341983&amp;quot;:0,&amp;quot;335559739&amp;quot;:0,&amp;quot;335559740&amp;quot;:240}"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;OL start="3"&gt;
&lt;LI&gt;&lt;SPAN data-contrast="auto"&gt; The programme should output money spent for the second year of maximum pay and its label on the figure should match this (i.e., payv_label is Maximum Pay). This will be for months 7-12 and its label on the figure should match this (i.e., months_label is July to Dec).&lt;/SPAN&gt;&lt;SPAN data-ccp-props="{&amp;quot;201341983&amp;quot;:0,&amp;quot;335559739&amp;quot;:0,&amp;quot;335559740&amp;quot;:240}"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Fri, 28 Jul 2023 17:55:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-writing-a-macro-to-cycle-through-different/m-p/886836#M350412</guid>
      <dc:creator>Angmar</dc:creator>
      <dc:date>2023-07-28T17:55:09Z</dc:date>
    </item>
    <item>
      <title>Re: help with writing a macro to cycle through different combinations of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-writing-a-macro-to-cycle-through-different/m-p/886841#M350416</link>
      <description>&lt;P&gt;The first step in writing macros is to write code that works and does what you want &lt;FONT color="#FF0000"&gt;WITHOUT&lt;/FONT&gt; macros and &lt;FONT color="#FF0000"&gt;WITHOUT&lt;/FONT&gt; macro variables, for one combination of variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Show us that code.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2023 16:57:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-writing-a-macro-to-cycle-through-different/m-p/886841#M350416</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-07-28T16:57:55Z</dc:date>
    </item>
    <item>
      <title>Re: help with writing a macro to cycle through different combinations of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-writing-a-macro-to-cycle-through-different/m-p/886907#M350445</link>
      <description>&lt;P&gt;I wouldn't spend much time thinking about this request without example data.&lt;/P&gt;
&lt;P&gt;This is also a lot like "please do my work for me" not a "help me with this bit I can't figure out". Which is where I start at $100 custom programming (or more, depends).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you have at least 3 syntax errors in 13 lines of code things don't bode well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you are discussing dates and intervals can we even hope that your current data has SAS date variables involved?&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2023 20:32:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-writing-a-macro-to-cycle-through-different/m-p/886907#M350445</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-07-28T20:32:04Z</dc:date>
    </item>
    <item>
      <title>Re: help with writing a macro to cycle through different combinations of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-writing-a-macro-to-cycle-through-different/m-p/886924#M350450</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/197740"&gt;@Angmar&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS macros are basically SAS code generators. First step is to write static SAS code that fully works. Only once you've got such code start with the macro to make the code dynamic.&lt;/P&gt;
&lt;P&gt;Also consider using SAS by-group processing which is much more efficient than creating many steps that each re-process the data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use loops in your macro to create individual pieces of SAS code. I hope below sample code will give you the idea.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro demo(payv=);
  %local n_terms;
  %let n_terms=%sysfunc(countw(%str(&amp;amp;payv),%str( )));
  %do i=1 %to &amp;amp;n_terms;
    data _null_;
      file print;
      put "Number of words in string is &amp;amp;n_terms";
      put "Macro value is %scan(&amp;amp;payv,&amp;amp;i,%str( ))";
    run;
  %end;
%mend;

%demo(
  payv=pay_Average pay_Minimum pay_Maximum
  );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1690587139245.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/86234i99B7E5DAB27AAAED/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1690587139245.png" alt="Patrick_0-1690587139245.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another way often used is to have a data step that calls the macro multiple times, each time passing different parameter values. Example below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro demo(payv=);
  data _null_;
    file print;
    put "Macro value is &amp;amp;payv";
  run;
%mend;

data driver;
  value='pay_Average'; output;
  value='pay_Minimum'; output;
  value='pay_Maximum'; output;
run;

data _null_;
  set driver;
  length cmd $300;
  cmd=cats('%demo(payv=',value,');');
  put cmd=;
  call execute( cmd );
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1690587527006.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/86235iD6743ABCA6C49F39/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_1-1690587527006.png" alt="Patrick_1-1690587527006.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And a 3rd option for data driven code generation is to first write the generated SAS code to a file and then %include this file for execution.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data driver;
  value='pay_Average'; output;
  value='pay_Minimum'; output;
  value='pay_Maximum'; output;
run;

filename codegen temp;
data _null_;
  file codegen;
  set driver;
  put 
    'data _null_;' /
    '  file print;' /
    '  put "Driver value is ' value '";' /
    'run;'
    ;
run;
%include codegen /source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_2-1690587879663.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/86236iD8C84C46DD69648E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_2-1690587879663.png" alt="Patrick_2-1690587879663.png" /&gt;&lt;/span&gt;&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>Fri, 28 Jul 2023 23:45:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-writing-a-macro-to-cycle-through-different/m-p/886924#M350450</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-07-28T23:45:01Z</dc:date>
    </item>
    <item>
      <title>Re: help with writing a macro to cycle through different combinations of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-writing-a-macro-to-cycle-through-different/m-p/888197#M350933</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;This was really helpful and I was able to make it work by referring to your first example and cobbling together some of that with what I already knew.&lt;BR /&gt;This is what I wrote (ignore the different variable names from my posted question- I ended up creating a dummy dataset with a simpler analysis so I could practice):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro a1;&lt;BR /&gt;%let tempvar_list=TAv TMax TMin;&lt;/P&gt;
&lt;P&gt;/* Macro to calculate the means for specific months */&lt;BR /&gt;%macro calculate_means(month_var);&lt;BR /&gt;%do i=1 %to 3; &lt;BR /&gt;%let tempvar=%scan(&amp;amp;tempvar_list, &amp;amp;i);&lt;BR /&gt;&lt;BR /&gt;proc means data=dat3;&lt;BR /&gt;where &amp;amp;month_var;&lt;BR /&gt;var &amp;amp;tempvar; &lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;%mend;&lt;/P&gt;
&lt;P&gt;/* Call the macro for months 3 to 9 */&lt;BR /&gt;%let month_var = month &amp;gt;= 3 and month &amp;lt;= 9;&lt;BR /&gt;%calculate_means(&amp;amp;month_var);&lt;/P&gt;
&lt;P&gt;/* Call the macro for months 5 to 8 */&lt;BR /&gt;%let month_var = month &amp;gt;= 5 and month &amp;lt;= 8;&lt;BR /&gt;%calculate_means(&amp;amp;month_var);&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;ods listing;&amp;nbsp;&lt;BR /&gt;%a1;&lt;BR /&gt;ods listing close;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2023 18:23:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-writing-a-macro-to-cycle-through-different/m-p/888197#M350933</guid>
      <dc:creator>Angmar</dc:creator>
      <dc:date>2023-08-07T18:23:49Z</dc:date>
    </item>
  </channel>
</rss>

