<?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: macro not cycling through all values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-not-cycling-through-all-values/m-p/690747#M210151</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As shown in the log, your macro is looping appropriately.&amp;nbsp; If you turn on options MPRINT, it would allow you to see that you generate valid code for three data steps, and each executes correctly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the problem is in every data step, the output dataset is&amp;nbsp;&lt;SPAN&gt;DATA.REGIONALVALUES6.&amp;nbsp; So the first step creates&amp;nbsp;DATA.REGIONALVALUES6 (assuming it didn't exist already), the second step over-writes it, and the third step over-writes it.&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If you want to have three different output datasets, you need to have the macro generate different names for the output dataset.&amp;nbsp; For example, you could change the output dataset to be&amp;nbsp;DATA.REGIONALVALUES6_&amp;amp;regions, so that when you call the macro, it would create&amp;nbsp;DATA.REGIONALVALUES6_MCR&amp;nbsp;DATA.REGIONALVALUES6_SCR and&amp;nbsp;DATA.REGIONALVALUES6_NER.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 11 Oct 2020 02:27:46 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2020-10-11T02:27:46Z</dc:date>
    <item>
      <title>macro not cycling through all values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-not-cycling-through-all-values/m-p/690745#M210150</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;
&lt;P&gt;I have a dataset I want to do a series of if/then statements with a series of variables that have a prefix i've assigned to a macro value but the macro isn't cycling how it should be. I don't get any errors in the log (see below) but it only produces a result for my last macro value NER. any insight would be helpful please!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;code:&lt;/P&gt;
&lt;P&gt;%MACRO INTERVALS (REGION);&lt;/P&gt;
&lt;P&gt;%LET REGION=MCR SCR NER;&lt;/P&gt;
&lt;P&gt;%do i=1 %to %sysfunc(countw(&amp;amp;REGION)); &lt;BR /&gt;%LET REGIONS= %SCAN(&amp;amp;REGION,&amp;amp;i);&lt;BR /&gt;%put &amp;amp;REGIONS;&lt;/P&gt;
&lt;P&gt;DATA DATA.REGIONALVALUES6;&lt;BR /&gt;SET DATA.REGIONALVALUES5;&lt;/P&gt;
&lt;P&gt;IF (&amp;amp;REGIONS._HIB_COMPLETE_UCLM &amp;lt; STATE_HIB_COMPLETE_LCLM) THEN &amp;amp;REGIONS._HIB_SIGNIFICANCE=1;&lt;/P&gt;
&lt;P&gt;RUN;&lt;/P&gt;
&lt;P&gt;%END;&lt;/P&gt;
&lt;P&gt;%MEND INTERVALS;&lt;BR /&gt;%INTERVALS (MCR SCR NER);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;log:&lt;/P&gt;
&lt;P&gt;5038 %MACRO INTERVALS (REGION);&lt;BR /&gt;5039&lt;BR /&gt;5040 %LET REGION=MCR SCR NER;&lt;BR /&gt;5041&lt;BR /&gt;5042 %do i=1 %to %sysfunc(countw(&amp;amp;REGION));&lt;BR /&gt;5043 %LET REGIONS= %SCAN(&amp;amp;REGION,&amp;amp;i);&lt;BR /&gt;5044 %put &amp;amp;REGIONS;&lt;BR /&gt;5045&lt;BR /&gt;5046 DATA DATA.REGIONALVALUES6;&lt;BR /&gt;5047 SET DATA.REGIONALVALUES5;&lt;BR /&gt;5048&lt;BR /&gt;5049 IF (&amp;amp;REGIONS._HIB_COMPLETE_UCLM &amp;lt; STATE_HIB_COMPLETE_LCLM) THEN&lt;BR /&gt;5049! &amp;amp;REGIONS._HIB_SIGNIFICANCE=1;&lt;BR /&gt;5050&lt;BR /&gt;5051 RUN;&lt;BR /&gt;5052&lt;BR /&gt;5053 %END;&lt;BR /&gt;5054&lt;BR /&gt;5055 %MEND INTERVALS;&lt;BR /&gt;5056 %INTERVALS (MCR SCR NER);&lt;BR /&gt;MCR&lt;/P&gt;
&lt;P&gt;NOTE: There were 1 observations read from the data set DATA.REGIONALVALUES5.&lt;BR /&gt;NOTE: The data set DATA.REGIONALVALUES6 has 1 observations and 758 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.45 seconds&lt;BR /&gt;cpu time 0.03 seconds&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;SCR&lt;/P&gt;
&lt;P&gt;NOTE: There were 1 observations read from the data set DATA.REGIONALVALUES5.&lt;BR /&gt;NOTE: The data set DATA.REGIONALVALUES6 has 1 observations and 758 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.08 seconds&lt;BR /&gt;cpu time 0.03 seconds&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;NER&lt;/P&gt;
&lt;P&gt;NOTE: There were 1 observations read from the data set DATA.REGIONALVALUES5.&lt;BR /&gt;NOTE: The data set DATA.REGIONALVALUES6 has 1 observations and 758 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.06 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 11 Oct 2020 02:11:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-not-cycling-through-all-values/m-p/690745#M210150</guid>
      <dc:creator>TPayne</dc:creator>
      <dc:date>2020-10-11T02:11:31Z</dc:date>
    </item>
    <item>
      <title>Re: macro not cycling through all values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-not-cycling-through-all-values/m-p/690747#M210151</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As shown in the log, your macro is looping appropriately.&amp;nbsp; If you turn on options MPRINT, it would allow you to see that you generate valid code for three data steps, and each executes correctly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the problem is in every data step, the output dataset is&amp;nbsp;&lt;SPAN&gt;DATA.REGIONALVALUES6.&amp;nbsp; So the first step creates&amp;nbsp;DATA.REGIONALVALUES6 (assuming it didn't exist already), the second step over-writes it, and the third step over-writes it.&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If you want to have three different output datasets, you need to have the macro generate different names for the output dataset.&amp;nbsp; For example, you could change the output dataset to be&amp;nbsp;DATA.REGIONALVALUES6_&amp;amp;regions, so that when you call the macro, it would create&amp;nbsp;DATA.REGIONALVALUES6_MCR&amp;nbsp;DATA.REGIONALVALUES6_SCR and&amp;nbsp;DATA.REGIONALVALUES6_NER.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 11 Oct 2020 02:27:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-not-cycling-through-all-values/m-p/690747#M210151</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-10-11T02:27:46Z</dc:date>
    </item>
    <item>
      <title>Re: macro not cycling through all values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-not-cycling-through-all-values/m-p/690759#M210160</link>
      <description>&lt;P&gt;You may not want to use macro in this situation.&amp;nbsp; If your code is dealing with UCLM and SIGNIFICANCE variables that are aught to change or be different sets of variables in a different process flow macro might be warranted.&amp;nbsp; The strongest foundation for writing macro code that generates SAS code is to be able to write clear non-macro code first.&amp;nbsp; You want to trade off the clarity of direct code versus the reusability or semantic shortening offered by implementing a macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider this non macro version&lt;/P&gt;
&lt;PRE&gt;DATA DATA.REGIONALVALUES6;

  SET DATA.REGIONALVALUES5;

  ARRAY UCLMS
    MCR_HIB_COMPLETE_UCLM
    SCR_HIB_COMPLETE_UCLM
    NER_HIB_COMPLETE_UCLM
  ;

  ARRAY SIGS
    MCR_HIB_SIGNIFICANCE
    SCR_HIB_SIGNIFICANCE
    NER_HIB_SIGNIFICANCE
  ;

  do _n_ = 1 to dim (UCLMS);  * repurpose _n_ as loop index;
    
    if UCLMS(_N_) &amp;lt; STATE_HIB_COMPLETE_LCLM then SIGS(_n_) = 1;

  end;
RUN;
&lt;/PRE&gt;
&lt;P&gt;You might want to ask your self, if I go macro should the entire step be macroized or just the generation of the variable names listed for the array definitions.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;If the entire step, should the input (SET) and output (DATA) data set names be hard coded, or be parameters of the macro ?
&lt;UL&gt;
&lt;LI&gt;&amp;nbsp; If hard coded, does that offered any benefit long term ? (Probably not)&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;Should the limit variable holding the cutoff value (STATE_HIB_COMPLETE_LCLM ) be hardcoded or a parameter ?&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;If you go through the abstraction process and everything in the code&amp;nbsp;&lt;EM&gt;could &lt;/EM&gt;be parameterized, why not just write the plain code instead ?&amp;nbsp; If you go the macro route the future code users/readers/maintainers will have to deal with an extra level of abstraction.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose the use case indicates the entire step should be macroized to better deal with different prefixes of UCLM and SIGNIFICANCE variables.&lt;/P&gt;
&lt;P&gt;The macro changes from a possibly reusable role-based macro to a restricted use process-flow macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps something like this&lt;/P&gt;
&lt;PRE&gt;%macro step6_intervals(regions=);
  %* macro name is 'process-flow' based;
  %* significance assignment assigned directly (not via array loop) by
  %* macro generated if/then source code;

  %local i region;

  /* emit source code as boilerplate */

  DATA DATA.REGIONALVALUES6;

    SET DATA.REGIONALVALUES5;

    /* emit 'generated' source */
    /* generated in the sense of resolving macro variables and expressions */

    %do i = 1 %to %sysfunc(countw(&amp;amp;regions));

      %let region = %scan(&amp;amp;regions,&amp;amp;i);

      if &amp;amp;region._HIB_COMPLETE_UCLM &amp;lt; STATE_HIB_COMPLETE_LCLM then 
        &amp;amp;region._SIGNIFICANCE = 1;
    %end;
  RUN;
%mend;

%step6_intervals(regions=MCR SCR NER)
------------ LOG ------------
577  %step6_intervals(regions=MCR SCR NER)
MPRINT(STEP6_INTERVALS):   DATA DATA.REGIONALVALUES6;
MPRINT(STEP6_INTERVALS):   SET DATA.REGIONALVALUES5;
MPRINT(STEP6_INTERVALS):   if MCR_HIB_COMPLETE_UCLM &amp;lt; STATE_HIB_COMPLETE_LCLM then MCR_SIGNIFICANCE = 1;
MPRINT(STEP6_INTERVALS):   if SCR_HIB_COMPLETE_UCLM &amp;lt; STATE_HIB_COMPLETE_LCLM then SCR_SIGNIFICANCE = 1;
MPRINT(STEP6_INTERVALS):   if NER_HIB_COMPLETE_UCLM &amp;lt; STATE_HIB_COMPLETE_LCLM then NER_SIGNIFICANCE = 1;
MPRINT(STEP6_INTERVALS):   RUN;
&lt;/PRE&gt;</description>
      <pubDate>Sun, 11 Oct 2020 04:51:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-not-cycling-through-all-values/m-p/690759#M210160</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-10-11T04:51:41Z</dc:date>
    </item>
  </channel>
</rss>

