<?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: Dynamic macro values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-values/m-p/907323#M358166</link>
    <description>&lt;P&gt;Since you don't tell us what these "various data tasks" are, I create my own. I will use each value of code in a text file (not an excel file) and then do, in my example: &lt;FONT face="courier new,courier"&gt;proc print data=real_data(where=(id=44123320)); run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the text file named example_data.txt&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;44123310
44123320
44123390&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;here is the SAS code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
    infile "example_data.txt";
    input code;
    call execute(cats('proc print data=real_data(where=(id=',code,')); run;'));
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will see in the log that the desired PROC PRINTs appear (they don't run because I don't really have data). You could also use macros inside CALL EXECUTE if that would work better. Depending on what you are trying to do (which you really didn't tell us), macros may or may not be needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 11 Dec 2023 13:08:57 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-12-11T13:08:57Z</dc:date>
    <item>
      <title>Dynamic macro values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-values/m-p/907320#M358164</link>
      <description>&lt;P&gt;I have a working macro, that substitutes a code value on each iteration. The following example has three codes/iterations&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro imp (code); 
             various data tasks
%mend imp;
&lt;BR /&gt;%imp(44123310)
%imp(44123320)
%imp(44123390)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, the reality is that I have hundreds of codes/iterations to run, which can be tedious (and lead to human error).&amp;nbsp; Instead of hardcoding each value and tailoring the number of iterations by hand, I'd like to automate it from a list of values for the code variable.&amp;nbsp; Perhaps the list of code values is in a spreadsheet.&amp;nbsp;&amp;nbsp; I'd appreciate any tips.&amp;nbsp; SAS 9.4 M6&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2023 12:54:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-values/m-p/907320#M358164</guid>
      <dc:creator>texasmfp</dc:creator>
      <dc:date>2023-12-11T12:54:32Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-values/m-p/907322#M358165</link>
      <description>&lt;P&gt;Look at CALL EXECUTE in a data step. You can use data step variable(s) in submitted code.&lt;/P&gt;
&lt;P&gt;Note: there are some tricks about calling macros though as you don't want them called in the data step .&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;%macro dummy( parm);
  %put parm is: &amp;amp;parm.;
%mend;


data _null_;
   set sashelp.class;
   call execute ('%dummy('||name||')');
run;&lt;/PRE&gt;
&lt;P&gt;Call execute places statements into a buffer that executes after the data step completes. So create a string that will not attempt to execute the macro by placing it in single quotes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or use a data step to write the macro calls to a program file and then %include the that file after the data step completes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It might also be as simple as BY group processing, possibly with a WHERE statement. It might help to provide some details on the actual tasks. The macro may not be needed at all.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2023 15:25:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-values/m-p/907322#M358165</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-12-11T15:25:34Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-values/m-p/907323#M358166</link>
      <description>&lt;P&gt;Since you don't tell us what these "various data tasks" are, I create my own. I will use each value of code in a text file (not an excel file) and then do, in my example: &lt;FONT face="courier new,courier"&gt;proc print data=real_data(where=(id=44123320)); run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the text file named example_data.txt&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;44123310
44123320
44123390&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;here is the SAS code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
    infile "example_data.txt";
    input code;
    call execute(cats('proc print data=real_data(where=(id=',code,')); run;'));
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will see in the log that the desired PROC PRINTs appear (they don't run because I don't really have data). You could also use macros inside CALL EXECUTE if that would work better. Depending on what you are trying to do (which you really didn't tell us), macros may or may not be needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2023 13:08:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-values/m-p/907323#M358166</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-12-11T13:08:57Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-values/m-p/907326#M358167</link>
      <description>&lt;P&gt;Here are a couple of suggestions:&lt;/P&gt;
&lt;P&gt;First, let's make that Excel spreadsheet containing the codes, and compile the desired macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create the Excel spreadsheet to use for demo purposes */
libname xl xlsx "myFile.xlsx";
data xl.myCodes;
	do Code=44123310,44123320,44123390;
		output;
	end;
run;
libname xl clear;

/* Compile the desired macro */
%macro imp (code); 
   %PUT NOTE: Macro &amp;amp;sysmacroname executing for &amp;amp;=code;
%mend imp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Next, we can read the data from Excel using SQL and iterate over the codes:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Suggested solution #1 */
libname xl xlsx "myFile.xlsx";

/* Create a driver macro to drive execution */
%macro runIMP;
/* Get the codes in a series of macro variables Code1-CodeN */
proc sql noprint;
	select Code into :code1-
	from xl.myCodes
;
quit;

/* SQLOBS records number of rows returned by the query */
/* Execute the desired macro once for each code returned */
%do i=1 %to &amp;amp;sqlobs;
	%imp(&amp;amp;&amp;amp;Code&amp;amp;i)
%end;
%mend;

%runIMP;
libname xl clear;
/* End Solution #1 */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From the Log:&lt;/P&gt;
&lt;PRE&gt;NOTE: Macro IMP executing for CODE=44123310
NOTE: Macro IMP executing for CODE=44123320
NOTE: Macro IMP executing for CODE=44123390&lt;/PRE&gt;
&lt;P&gt;Another solution is to use a DATA step and DOSUBL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Suggestion 2: Use DATA step and DOSUBL */
libname xl xlsx "myFile.xlsx";

data _null_;
	/* read inthe codes */
	set xl.myCodes;
	/* Execute the macro once for each code */
	rc=dosubl(cats('%imp(',code,')'));
run;
libname xl clear;
/* End Solution #2 */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From the Log:&lt;/P&gt;
&lt;PRE&gt;NOTE: Macro IMP executing for CODE=44123310
NOTE: Macro IMP executing for CODE=44123320
NOTE: Macro IMP executing for CODE=44123390
NOTE: The import data set has 3 observations and 1 variables.
NOTE: There were 3 observations read from the data set XL.myCodes.&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Dec 2023 13:16:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-values/m-p/907326#M358167</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2023-12-11T13:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-values/m-p/907333#M358169</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/296215"&gt;@texasmfp&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You already got a bunch of great solutions frmo:&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;, and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13728"&gt;@SASJedi&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I just want to share one more. It's called &lt;STRONG&gt;&lt;A title="https://github.com/SASPAC/macroarray" href="https://github.com/SASPAC/macroarray" target="_self"&gt;macroArray&lt;/A&gt;&lt;/STRONG&gt; package.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you install macroArray package and load it into your session, assuming you have an Excel with data (i'm using Mark's example here):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create the Excel spreadsheet to use for demo purposes */
libname xl xlsx "myFile.xlsx";
data xl.myCodes;
	do Code=44123310,44123320,44123390;
		output;
	end;
run;
libname xl clear;

/* Compile the desired macro */
%macro imp (code); 
   %PUT NOTE: Macro &amp;amp;sysmacroname executing for &amp;amp;=code;
%mend imp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;all you need are the &lt;A title="https://github.com/SASPAC/macroarray/blob/main/macroarray.md#array-macro" href="https://github.com/SASPAC/macroarray/blob/main/macroarray.md#array-macro" target="_self"&gt;%array()&lt;/A&gt; and the &lt;A title="https://github.com/SASPAC/macroarray/blob/main/macroarray.md#do-over-macro" href="https://github.com/SASPAC/macroarray/blob/main/macroarray.md#do-over-macro" target="_self"&gt;%do_over()&lt;/A&gt; macros,&amp;nbsp; run then like this:&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;libname xl xlsx "R:\myFile.xlsx";
%array(ds=xl.myCodes, vars=Code, macarray=Y)

%do_over(Code,phrase=%nrstr(
  %imp(%code(&amp;amp;_i_)) 
))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and in the log you will see:&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;1    libname xl xlsx "R:\myFile.xlsx";
NOTE: Libref XL was successfully assigned as follows:
      Engine:        XLSX
      Physical Name: R:\myFile.xlsx
2    %array(ds=xl.myCodes, vars=Code, macarray=Y)
NOTE:[ARRAY] 3 macrovariables created
3
4    %do_over(Code,phrase=%nrstr(
5      %imp(%code(&amp;amp;_i_))
6    ))
NOTE: Macro IMP executing for CODE=44123310
NOTE: Macro IMP executing for CODE=44123320
NOTE: Macro IMP executing for CODE=44123390
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;P.S. To install and use macroArray package do:&lt;/P&gt;
&lt;UL dir="auto"&gt;
&lt;LI&gt;Enable the framework [first time only]:&lt;/LI&gt;
&lt;LI&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename SPFinit url "https://raw.githubusercontent.com/yabwon/SAS_PACKAGES/main/SPF/SPFinit.sas";
%include SPFinit; /* enable the framework */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;Install the framework and the package on your machine in the folder you created:&lt;/LI&gt;
&lt;LI&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename packages "&amp;lt;/your/directory/for/packages/&amp;gt;"; 
%installPackage(SPFinit macroArray) &lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;From now on, in your SAS session just run it like this:&lt;/LI&gt;
&lt;LI&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename packages "&amp;lt;/your/directory/for/packages/&amp;gt;";
%include packages(SPFinit.sas);

%loadPackage(packageName)  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;A title="https://github.com/yabwon/SAS_PACKAGES?tab=readme-ov-file#the-user" href="https://github.com/yabwon/SAS_PACKAGES?tab=readme-ov-file#the-user" target="_self"&gt;Link to details&lt;/A&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>Mon, 11 Dec 2023 14:11:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-values/m-p/907333#M358169</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-12-11T14:11:29Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-values/m-p/908475#M358500</link>
      <description>&lt;P&gt;Thanks to all of you for excellent suggestions.&amp;nbsp; I am accepting this answer, but all are good options.&amp;nbsp; Thanks&lt;/P&gt;</description>
      <pubDate>Sat, 16 Dec 2023 12:25:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-values/m-p/908475#M358500</guid>
      <dc:creator>texasmfp</dc:creator>
      <dc:date>2023-12-16T12:25:20Z</dc:date>
    </item>
  </channel>
</rss>

