<?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: Create macro loop with parameter lists of different lengths in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-macro-loop-with-parameter-lists-of-different-lengths/m-p/892556#M352521</link>
    <description>&lt;P&gt;You cannot replace the list with a single item and expect it to work for more than one item.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If NAME and PREFIX go together then you only need one %DO loop to cover both lists.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have to use the same delimiter when counting how many items are in the list as you use when scanning the list.&amp;nbsp; What is the delimiter for the other lists?&amp;nbsp; Be specific when telling COUNT() and %SCAN() what delimiter to use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you really using both single quote and ~ as delimiters in the METHOD list? Why?&amp;nbsp; You appear to be using the value scanned from METHOD as part of a dataset name, so why not just use space as the delimiter?&amp;nbsp; You can add the Q modifier when scanning the quoted name strings.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro train(method=,prefix=,name=,fold=);
%local m p n f nm np nf;
%do nm=1 %to %sysfunc(countw(&amp;amp;method,%str( )));
  %let m=%scan(&amp;amp;method,&amp;amp;nm,%str( ));
  %do np=1 %to %sysfunc(countw(&amp;amp;prefix,%str( )));
    %let p=%scan(&amp;amp;prefix,&amp;amp;np,%str( ));
    %let n=%scan(&amp;amp;name,&amp;amp;np,%str( ),q);
    %do nf=1 %to %sysfunc(countw(&amp;amp;fold,%str( )));
      %let f=%scan(&amp;amp;fold,&amp;amp;nf,%str( ));
%put &amp;amp;=nm &amp;amp;=m &amp;amp;=np &amp;amp;=p &amp;amp;=n &amp;amp;=nf &amp;amp;=f;
proc assess
   data=casuser.scored_&amp;amp;p._fold_eq_&amp;amp;f._&amp;amp;m. (where=(outer_k_fold ne 1)) 
   ROCOUT=casuser.&amp;amp;p._fold_ne_&amp;amp;f._rocinfo_&amp;amp;m.
;
  input P_target1;
  target target / level=nominal event='1';
  fitstat pvar=P_target0 / pevent='0';
run;
    %end;
  %end;
%end;
%mend train;

%train(method=mtd1, prefix=DT RF, name="Decision_Tree" "Random Forest",fold=1 2 3);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If I remove the actual SAS code so I can test the macro without your dataset I get:&lt;/P&gt;
&lt;PRE&gt;554  %train(method=mtd1, prefix=DT RF, name="Decision_Tree" "Random Forest",fold=1 2 3);
NM=1 M=mtd1 NP=1 P=DT N="Decision_Tree" NF=1 F=1
NM=1 M=mtd1 NP=1 P=DT N="Decision_Tree" NF=2 F=2
NM=1 M=mtd1 NP=1 P=DT N="Decision_Tree" NF=3 F=3
NM=1 M=mtd1 NP=2 P=RF N="Random Forest" NF=1 F=1
NM=1 M=mtd1 NP=2 P=RF N="Random Forest" NF=2 F=2
NM=1 M=mtd1 NP=2 P=RF N="Random Forest" NF=3 F=3
&lt;/PRE&gt;</description>
    <pubDate>Mon, 04 Sep 2023 13:45:16 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-09-04T13:45:16Z</dc:date>
    <item>
      <title>Create macro loop with parameter lists of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-macro-loop-with-parameter-lists-of-different-lengths/m-p/892531#M352503</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to write a macro loop that can take the following input datasets:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;casuser.scored_DT_fold_eq_1_mtd1&lt;BR /&gt;casuser.scored_DT_fold_eq_2_mtd1&lt;BR /&gt;casuser.scored_DT_fold_eq_3_mtd1&lt;BR /&gt;casuser.scored_RF_fold_eq_1_mtd1&lt;BR /&gt;casuser.scored_RF_fold_eq_2_mtd1&lt;BR /&gt;casuser.scored_RF_fold_eq_3_mtd1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and output the following datasets:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;casuser.DT_fold_eq_1_rocinfo_mtd1&lt;BR /&gt;casuser.DT_fold_eq_2_rocinfo_mtd1&lt;BR /&gt;casuser.DT_fold_eq_3_rocinfo_mtd1&lt;BR /&gt;casuser.RF_fold_eq_1_rocinfo_mtd1&lt;BR /&gt;casuser.RF_fold_eq_2_rocinfo_mtd1&lt;BR /&gt;casuser.RF_fold_eq_3_rocinfo_mtd1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the code I have tried so far, with no luck:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro train(method=,prefix=,name=,fold=);
  %do m=1 %to %sysfunc(countw(&amp;amp;method));
    %let method=%scan(&amp;amp;method,&amp;amp;m,'~');
    %do p=1 %to %sysfunc(countw(&amp;amp;prefix));
      %let prefix=%scan(&amp;amp;prefix,&amp;amp;p);
	%do n=1 %to %sysfunc(countw(&amp;amp;name));
      %let name=%scan(&amp;amp;name,&amp;amp;n);
	  %do f=1 %to %sysfunc(countw(&amp;amp;fold));
      	%let fold=%scan(&amp;amp;fold,&amp;amp;f,);
      	proc assess data=casuser.scored_&amp;amp;prefix._fold_eq_&amp;amp;fold._&amp;amp;method (where=(outer_k_fold ne 1)) 
										ROCOUT=casuser.&amp;amp;prefix._fold_ne_&amp;amp;fold._rocinfo_&amp;amp;method;
    	input P_target1;
    	target target / level=nominal event='1';
    	fitstat pvar=P_target0 / pevent='0';
		run;
      run;
	  %end;
    %end;
	%end;
  %end;
%mend train;

%train(method=mtd1, prefix=DT RF, name="Decision_Tree" "Random Forest",fold=1 2 3);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the errors I am getting have something to do with how the list is written for every macro parameter in the last &lt;CODE class=" language-sas"&gt;%train&lt;/CODE&gt; statement. Note that the prefix and name macro parameters have lists of the same lengths.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 11:17:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-macro-loop-with-parameter-lists-of-different-lengths/m-p/892531#M352503</guid>
      <dc:creator>GuyTreepwood</dc:creator>
      <dc:date>2023-09-04T11:17:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create macro loop with parameter lists of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-macro-loop-with-parameter-lists-of-different-lengths/m-p/892546#M352515</link>
      <description>&lt;P&gt;Analyze (and share) your log with options SYMBOLGEN and MPRINT.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 12:40:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-macro-loop-with-parameter-lists-of-different-lengths/m-p/892546#M352515</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2023-09-04T12:40:22Z</dc:date>
    </item>
    <item>
      <title>Re: Create macro loop with parameter lists of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-macro-loop-with-parameter-lists-of-different-lengths/m-p/892547#M352516</link>
      <description>&lt;P&gt;Here's a set of issues you need to attend to.&amp;nbsp; There could be more of course.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You call the macro with two parameters &amp;amp;NAME and &amp;amp;FOLD which take on multiple values.&amp;nbsp; But in the middle of executing the macro, you replace &amp;amp;NAME and &amp;amp;FOLD with just a single item from the original list.&amp;nbsp; You will never be able to go back and retrieve the original lists of values provided when calling the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of replacing &amp;amp;NAME and &amp;amp;FOLD, assign "single item from a list" to a new macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a general rule, when you get an error, tell us what the error is.&amp;nbsp; Better yet, post the log.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 12:42:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-macro-loop-with-parameter-lists-of-different-lengths/m-p/892547#M352516</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-09-04T12:42:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create macro loop with parameter lists of different lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-macro-loop-with-parameter-lists-of-different-lengths/m-p/892556#M352521</link>
      <description>&lt;P&gt;You cannot replace the list with a single item and expect it to work for more than one item.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If NAME and PREFIX go together then you only need one %DO loop to cover both lists.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have to use the same delimiter when counting how many items are in the list as you use when scanning the list.&amp;nbsp; What is the delimiter for the other lists?&amp;nbsp; Be specific when telling COUNT() and %SCAN() what delimiter to use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you really using both single quote and ~ as delimiters in the METHOD list? Why?&amp;nbsp; You appear to be using the value scanned from METHOD as part of a dataset name, so why not just use space as the delimiter?&amp;nbsp; You can add the Q modifier when scanning the quoted name strings.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro train(method=,prefix=,name=,fold=);
%local m p n f nm np nf;
%do nm=1 %to %sysfunc(countw(&amp;amp;method,%str( )));
  %let m=%scan(&amp;amp;method,&amp;amp;nm,%str( ));
  %do np=1 %to %sysfunc(countw(&amp;amp;prefix,%str( )));
    %let p=%scan(&amp;amp;prefix,&amp;amp;np,%str( ));
    %let n=%scan(&amp;amp;name,&amp;amp;np,%str( ),q);
    %do nf=1 %to %sysfunc(countw(&amp;amp;fold,%str( )));
      %let f=%scan(&amp;amp;fold,&amp;amp;nf,%str( ));
%put &amp;amp;=nm &amp;amp;=m &amp;amp;=np &amp;amp;=p &amp;amp;=n &amp;amp;=nf &amp;amp;=f;
proc assess
   data=casuser.scored_&amp;amp;p._fold_eq_&amp;amp;f._&amp;amp;m. (where=(outer_k_fold ne 1)) 
   ROCOUT=casuser.&amp;amp;p._fold_ne_&amp;amp;f._rocinfo_&amp;amp;m.
;
  input P_target1;
  target target / level=nominal event='1';
  fitstat pvar=P_target0 / pevent='0';
run;
    %end;
  %end;
%end;
%mend train;

%train(method=mtd1, prefix=DT RF, name="Decision_Tree" "Random Forest",fold=1 2 3);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If I remove the actual SAS code so I can test the macro without your dataset I get:&lt;/P&gt;
&lt;PRE&gt;554  %train(method=mtd1, prefix=DT RF, name="Decision_Tree" "Random Forest",fold=1 2 3);
NM=1 M=mtd1 NP=1 P=DT N="Decision_Tree" NF=1 F=1
NM=1 M=mtd1 NP=1 P=DT N="Decision_Tree" NF=2 F=2
NM=1 M=mtd1 NP=1 P=DT N="Decision_Tree" NF=3 F=3
NM=1 M=mtd1 NP=2 P=RF N="Random Forest" NF=1 F=1
NM=1 M=mtd1 NP=2 P=RF N="Random Forest" NF=2 F=2
NM=1 M=mtd1 NP=2 P=RF N="Random Forest" NF=3 F=3
&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Sep 2023 13:45:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-macro-loop-with-parameter-lists-of-different-lengths/m-p/892556#M352521</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-04T13:45:16Z</dc:date>
    </item>
  </channel>
</rss>

