<?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 editing a macro variable in a data step do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799782#M314520</link>
    <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I am trying to create a loop which runs several programs multiple times using the %include function. These programs all use the macro variable&amp;nbsp;CUST_ID and I would like to change this macro variable for every loop based on a list I specify. This is what&amp;nbsp;I have so far but I can't manage to change the macro variable in the loop. Does anyone know how to do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/* specify list of values that have to be inserted into a macro variable*/&lt;BR /&gt;Data CUST_ID_LIST;
input CUST_ID;
datalines;
230419732
20476814
49500430
106808266
;
run;
&lt;BR /&gt;/*calculate the length of the list to determine the number of iterations of the loop*/
proc sql;
Select count(*)
into: NO_CUST_ID
from CUST_ID_LIST;
run;
&lt;BR /&gt;/* do the loop*/
data CUST_ID;
    do i = 1 to &amp;amp;NO_CUST_ID;
        set CUST_ID_LIST point = i;
		output CUST_ID ;
		%include "Program 1";
		%include "Program 2";
		%include "Program 3";
		%include "Program 4";
	end;
    stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 03 Mar 2022 09:03:48 GMT</pubDate>
    <dc:creator>Huub</dc:creator>
    <dc:date>2022-03-03T09:03:48Z</dc:date>
    <item>
      <title>editing a macro variable in a data step do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799782#M314520</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I am trying to create a loop which runs several programs multiple times using the %include function. These programs all use the macro variable&amp;nbsp;CUST_ID and I would like to change this macro variable for every loop based on a list I specify. This is what&amp;nbsp;I have so far but I can't manage to change the macro variable in the loop. Does anyone know how to do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/* specify list of values that have to be inserted into a macro variable*/&lt;BR /&gt;Data CUST_ID_LIST;
input CUST_ID;
datalines;
230419732
20476814
49500430
106808266
;
run;
&lt;BR /&gt;/*calculate the length of the list to determine the number of iterations of the loop*/
proc sql;
Select count(*)
into: NO_CUST_ID
from CUST_ID_LIST;
run;
&lt;BR /&gt;/* do the loop*/
data CUST_ID;
    do i = 1 to &amp;amp;NO_CUST_ID;
        set CUST_ID_LIST point = i;
		output CUST_ID ;
		%include "Program 1";
		%include "Program 2";
		%include "Program 3";
		%include "Program 4";
	end;
    stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2022 09:03:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799782#M314520</guid>
      <dc:creator>Huub</dc:creator>
      <dc:date>2022-03-03T09:03:48Z</dc:date>
    </item>
    <item>
      <title>Re: editing a macro variable in a data step do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799818#M314543</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I would like to change this macro variable for every loop based on a list I specify.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Show us (a representative portion of) the list. Also, where is this list stored? (SAS data set, text file, piece of paper, other?)&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2022 12:41:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799818#M314543</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-03-03T12:41:19Z</dc:date>
    </item>
    <item>
      <title>Re: editing a macro variable in a data step do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799820#M314545</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I create the list in the datastep mentioned in the code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/* specify list of values that have to be inserted into a macro variable*/
Data CUST_ID_LIST;
input CUST_ID;
datalines;
230419732
20476814
49500430
106808266
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Mar 2022 12:36:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799820#M314545</guid>
      <dc:creator>Huub</dc:creator>
      <dc:date>2022-03-03T12:36:41Z</dc:date>
    </item>
    <item>
      <title>Re: editing a macro variable in a data step do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799824#M314548</link>
      <description>&lt;P&gt;Then I'm lost. Your code takes this list, and computes the value of 4 for macro variable&amp;nbsp;&amp;amp;NO_CUST_ID, and your code should work properly. What is the problem? Please be specific and detailed, instead of assuming we can all see the problem you are seeing.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2022 12:44:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799824#M314548</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-03-03T12:44:28Z</dc:date>
    </item>
    <item>
      <title>Re: editing a macro variable in a data step do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799825#M314549</link>
      <description>Ah I'm sorry. I mean the macro variable in the do loop called CUST_ID. Not the variable &amp;amp;NO_CUST_ID. I use &amp;amp;NO_CUST_ID to determine the number of iterations, but i want to change variable CUST_ID for every iteration of the loop, where I replace the value of variable CUST_ID with the value of the list CUST_ID_LIST where the rownumber matches the iteration number. Is this clearer?</description>
      <pubDate>Thu, 03 Mar 2022 12:49:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799825#M314549</guid>
      <dc:creator>Huub</dc:creator>
      <dc:date>2022-03-03T12:49:51Z</dc:date>
    </item>
    <item>
      <title>Re: editing a macro variable in a data step do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799829#M314551</link>
      <description>&lt;P&gt;I think this example is what you are looking for&lt;BR /&gt;&lt;BR /&gt;First I have 2 simple programs that I'm going to %include (program1.sas &amp;amp; program2.sas):&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;
	put "This is program 1 with CUSTID=&amp;amp;custId" ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;
	put "This is program 2 with CUSTID=&amp;amp;custId" ;
run ; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then I have this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename inc1 "&amp;lt;path&amp;gt;\program1.sas" ;
filename inc2 "&amp;lt;path&amp;gt;\program2.sas" ;

data customerIds ;
	infile cards ;
	input custId $ ;
	call symput("custId"!!left(putn(_n_,"2.")),custId) ;
	call symput("count",putn(_n_,"2.")) ;
cards ;
Cust#001
Cust#002
;

%macro repeat ;
	%do i=1 %to &amp;amp;count ;
		%let custId=&amp;amp;&amp;amp;custId&amp;amp;i ;
		%include inc1 ;
		%include inc2 ;
	%end ;
%mend repeat ;

%let custId=Customer#001 ;

data _null_ ;
	%repeat ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's the log:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;272  %let custId=Customer 001 ;
273
274  filename inc1 "&amp;lt;Path&amp;gt;\program1.sas" ;
275  filename inc2 "&amp;lt;Path&amp;gt;\program2.sas" ;
276
277  data customerIds ;
278      infile cards ;
279      input custId $ ;
280      call symput("custId"!!left(putn(_n_,"2.")),custId) ;
281      call symput("count",putn(_n_,"2.")) ;
282  cards ;

NOTE: The data set WORK.CUSTOMERIDS has 2 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds


285  ;
286
287  %macro repeat ;
288      %do i=1 %to &amp;amp;count ;
289          %let custId=&amp;amp;&amp;amp;custId&amp;amp;i ;
290          %include inc1 ;
291          %include inc2 ;
292      %end ;
293  %mend repeat ;
294
295  %let custId=Customer#001 ;
296
297  data _null_ ;
298      %repeat ;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds



This is program 1 with CUSTID=Cust#001
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds



This is program 2 with CUSTID=Cust#001
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds



This is program 1 with CUSTID=Cust#002
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds



This is program 2 with CUSTID=Cust#002
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


317  run ;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Mar 2022 13:06:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799829#M314551</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2022-03-03T13:06:05Z</dc:date>
    </item>
    <item>
      <title>Re: editing a macro variable in a data step do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799830#M314552</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/410269"&gt;@Huub&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Ah I'm sorry. I mean the macro variable in the do loop called CUST_ID. Not the variable &amp;amp;NO_CUST_ID. I use &amp;amp;NO_CUST_ID to determine the number of iterations, but i want to change variable CUST_ID for every iteration of the loop, where I replace the value of variable CUST_ID with the value of the list CUST_ID_LIST where the rownumber matches the iteration number. Is this clearer?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not a bit clearer. I still don't know what is wrong with the code you have written, even though I specifically asked that question.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Mar 2022 13:06:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799830#M314552</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-03-03T13:06:24Z</dc:date>
    </item>
    <item>
      <title>Re: editing a macro variable in a data step do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799831#M314553</link>
      <description>&lt;P&gt;If the number of values is small enough that they can fit into a single macro variable (65K bytes) then just modify your PROC SQL step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unless your %INCLUDE files all contain only statements that can run inside a the &lt;STRONG&gt;single data step&lt;/STRONG&gt; you probably need to switch your DO loop to a %DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select distinct cust_id 
  into :cust_id_list separated by '|'
  from cust_id_list
;
%let no_cust_id = &amp;amp;sqlobs;
quit;

%do index=1 to &amp;amp;no_cust_id;
  %let cust_id = %scan(&amp;amp;cust_id_list,&amp;amp;index,|);
		
%include "Program 1";
%include "Program 2";
%include "Program 3";
%include "Program 4";

%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Mar 2022 13:10:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/editing-a-macro-variable-in-a-data-step-do-loop/m-p/799831#M314553</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-03-03T13:10:15Z</dc:date>
    </item>
  </channel>
</rss>

