<?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: Looping through list of different numbers in Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-list-of-different-numbers-in-Macro/m-p/464462#M118422</link>
    <description>&lt;P&gt;As you have seen, macro language doesn't support all the forms of a %DO statement that exist in a DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For this particular example, I would recommend just using two loops.&amp;nbsp; Package your complex code into a separate macro, and just use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do i=1 %to 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %complex_code (&amp;amp;i)&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;%do i=7 %to 9;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %complex_code (&amp;amp;i)&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 23 May 2018 17:13:01 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-05-23T17:13:01Z</dc:date>
    <item>
      <title>Looping through list of different numbers in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-list-of-different-numbers-in-Macro/m-p/464457#M118420</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I apologize if this has been answered elsewhere. I tried to search for it on the boards but couldn't find the exact answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a list of, say, 10 variables.&lt;/P&gt;
&lt;P&gt;I want to run a macro on the first 3 variables and the 7th, 8th and 9th variables. How would I go about indicating this? Let't just keep it to something simple like Proc Freq only for the sake of this example (but I end up manipulating the output and doing other things with it, and then doing other things with the other variables). The problem is that I usually know to say something like "%do i = 6 %to 9'"&lt;/P&gt;
&lt;P&gt;but in this case, here I want to say "%do i = 1 %to 3, and i =&amp;nbsp;7 %to 9;"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason I'm doing this&amp;nbsp; is because I'll run the same commands on those sets of variables, and manipulate them the same way. And then I'll manipulate the output of the other frequencies a different way. The only other alternative I can think of is grouping said variables together in a list and running a loop on that group only, a different loop on a different group, etc.&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;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list = q1
q2
q3
q4
q5
q6
q7
q8
q9
q10;

%macro q9freq;
     %do IncMiss = 1 %to 2  /*We want to run this all including missing, and not including missing*/
&lt;BR /&gt;          /***********************please check here***********************************/
         &lt;FONT color="#FF0000"&gt; %do 1 %to 3, 7 %to 9; /*This doesn't work, but I don't know how to fix it*/&lt;BR /&gt;&lt;/FONT&gt;          /****************************************************************************/
          %let var = %scan(&amp;amp;list, %eval(&amp;amp;i));

          proc freq data = irblabel_1;
               tables &amp;amp;var/ list
                    %if &amp;amp;IncMiss = 1 %then missing ; /*this semi colon closes the %if statement*/
               ; /*This semicolon closes the tables statement*/
               ods output Freq.Table1.OneWayFreqs = Freq&amp;amp;var.A&amp;amp;IncMiss;
          run;

          data freq&amp;amp;var.b&amp;amp;IncMiss (keep = Vname Answer PercentN);
               length Vname $256;
               format Vname $256.;
               retain Vname F_&amp;amp;var PercentN;
               set freq&amp;amp;var.A&amp;amp;IncMiss;
               where index(F_&amp;amp;var, "Yes") &amp;gt; 0
                    or index(F_&amp;amp;var, "Not") &amp;gt; 0
                    or index(F_&amp;amp;var, "Missing") &amp;gt; 0;
               Vname = scan(table, 2, " "); 
               PercentN = round(percent,0.1);
               Answer = f_&amp;amp;var;/* Get the first word, blank delimited */
          run;

	data Q9100&amp;amp;IncMiss;
		retain vname label
		length label $256;
		length vname $256;
		length answer $256;
				
		format label $256.;
		format vname $256.;
		format answer $256.;
		set
			blankq9 
			freqq1b&amp;amp;IncMiss 
			freqq2b&amp;amp;IncMiss 
                        freqq3b&amp;amp;IncMiss 
			freqq7b&amp;amp;IncMiss 
			freqq8b&amp;amp;IncMiss 
			freqq9b&amp;amp;IncMiss 
			;
			%MACRO LABELQ9(OLD, NEW);
			if Vname = &amp;amp;OLD then label = &amp;amp;new;
		%mend;
		%LABELQ9(" ", "label1:");
		%LABELQ9("q1", "label2");
		%LABELQ9("q2", "blah blah?");
     %end; /*End loop going thru Q9-12*/
%end; /*End loop going thru missing and nonmissing*/
%mend;
%q9freq;&lt;/CODE&gt;&lt;/PRE&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, 23 May 2018 16:57:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-list-of-different-numbers-in-Macro/m-p/464457#M118420</guid>
      <dc:creator>ginak</dc:creator>
      <dc:date>2018-05-23T16:57:07Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through list of different numbers in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-list-of-different-numbers-in-Macro/m-p/464462#M118422</link>
      <description>&lt;P&gt;As you have seen, macro language doesn't support all the forms of a %DO statement that exist in a DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For this particular example, I would recommend just using two loops.&amp;nbsp; Package your complex code into a separate macro, and just use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do i=1 %to 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %complex_code (&amp;amp;i)&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;%do i=7 %to 9;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %complex_code (&amp;amp;i)&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 17:13:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-list-of-different-numbers-in-Macro/m-p/464462#M118422</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-05-23T17:13:01Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through list of different numbers in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-list-of-different-numbers-in-Macro/m-p/464575#M118459</link>
      <description>&lt;P&gt;Hello. I do not have much experience with Macros but would a macro version of the below code do what you wish.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA Test;
DO i = 1 to 6;
	IF i le 3 then j = i; else j = (i-4)+7; *&lt;SPAN&gt;(Alternatively j = i + 3);&lt;/SPAN&gt;
	OUTPUT;
END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It gives outputs that look like this&lt;/P&gt;&lt;P&gt;i -- j&lt;/P&gt;&lt;P&gt;1--1&lt;/P&gt;&lt;P&gt;2--2&lt;/P&gt;&lt;P&gt;3--3&lt;/P&gt;&lt;P&gt;4--7&lt;/P&gt;&lt;P&gt;5--8&lt;/P&gt;&lt;P&gt;6--9&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And then&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var = %scan(&amp;amp;list, %eval(&amp;amp;j));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 May 2018 20:58:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-list-of-different-numbers-in-Macro/m-p/464575#M118459</guid>
      <dc:creator>DanielLangley</dc:creator>
      <dc:date>2018-05-23T20:58:54Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through list of different numbers in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-list-of-different-numbers-in-Macro/m-p/464602#M118467</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/209943"&gt;@DanielLangley&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello. I do not have much experience with Macros but would a macro version of the below code do what you wish.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA Test;
DO i = 1 to 6;
	IF i le 3 then j = i; else j = (i-4)+7; *&lt;SPAN&gt;(Alternatively j = i + 3);&lt;/SPAN&gt;
	OUTPUT;
END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It gives outputs that look like this&lt;/P&gt;
&lt;P&gt;i -- j&lt;/P&gt;
&lt;P&gt;1--1&lt;/P&gt;
&lt;P&gt;2--2&lt;/P&gt;
&lt;P&gt;3--3&lt;/P&gt;
&lt;P&gt;4--7&lt;/P&gt;
&lt;P&gt;5--8&lt;/P&gt;
&lt;P&gt;6--9&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And then&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var = %scan(&amp;amp;list, %eval(&amp;amp;j));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Something like this?:&lt;/P&gt;
&lt;PRE&gt;%macro dummy;
%DO i = 1 %to 6;
   %IF &amp;amp;i. le 3 %then %let j = &amp;amp;i.; 
   %else %let j =  %eval(&amp;amp;i. + 3);
   %put i=&amp;amp;i. j=&amp;amp;j;
%END;
%mend;

%dummy;&lt;/PRE&gt;
&lt;P&gt;Since I put the %eval in the %else bit then you wouldn't need to use %eval in the %scan call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 23:37:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-list-of-different-numbers-in-Macro/m-p/464602#M118467</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-05-23T23:37:55Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through list of different numbers in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-list-of-different-numbers-in-Macro/m-p/464616#M118473</link>
      <description>&lt;P&gt;The same way you would iterate over any set of non-numeric values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=1 %to %sysfunc(countw(&amp;amp;list));
  %let next=%scan(&amp;amp;list,&amp;amp;i);
  ....
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If it is too hard for you to generate the space delimited list you could take advantage of the power of the data step to generate it.&lt;/P&gt;
&lt;P&gt;For example you might do something like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  length list $200 ;
  do i=1 to 3,7 to 9 ;
    list=catx(' ',list,cats('q',i));
  end;
  call symputx('list',list);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 May 2018 01:37:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-list-of-different-numbers-in-Macro/m-p/464616#M118473</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-05-24T01:37:41Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through list of different numbers in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-list-of-different-numbers-in-Macro/m-p/464637#M118479</link>
      <description>&lt;P&gt;Thanks. Yes. So the final code could look something like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list = q1
q2
q3
q4
q5
q6
q7
q8
q9
q10;

%macro q9freq;
	%DO IncMiss=1 %to 2; *this needed a semicolon here;
        %DO i=1 %to 6;
			%IF &amp;amp;i. le 3 %then %let j = &amp;amp;i.;
			%else %let j =  %eval(&amp;amp;i. + 3);
			%let var = %scan(&amp;amp;list, &amp;amp;j);
	
			/*Insert Complex Code here*/

		%end; *End loop going thru 1,2,3,7,8,9*;
	%end; *End loop going thru missing and nonmissing;
%mend;

%q9freq;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;which iterates as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="sasSource"&gt;i=1 j=1 var=q1 IncMiss=1&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;i=2 j=2 var=q2 IncMiss=1&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;i=3 j=3 var=q3 IncMiss=1&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;i=4 j=7 var=q7 IncMiss=1&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;i=5 j=8 var=q8 IncMiss=1&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;i=6 j=9 var=q9 IncMiss=1&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;i=1 j=1 var=q1 IncMiss=2&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;i=2 j=2 var=q2 IncMiss=2&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;i=3 j=3 var=q3 IncMiss=2&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;i=4 j=7 var=q7 IncMiss=2&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;i=5 j=8 var=q8 IncMiss=2&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;i=6 j=9 var=q9 IncMiss=2&lt;/DIV&gt;</description>
      <pubDate>Thu, 24 May 2018 05:28:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-list-of-different-numbers-in-Macro/m-p/464637#M118479</guid>
      <dc:creator>DanielLangley</dc:creator>
      <dc:date>2018-05-24T05:28:04Z</dc:date>
    </item>
  </channel>
</rss>

