<?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 within Macro in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956534#M42983</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt; why do you have to start and end the macro outside of data step?You mean this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro var_list;

data need;
set have;
run;

%mend
&amp;amp;var_list;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 18 Jan 2025 17:02:39 GMT</pubDate>
    <dc:creator>trevand</dc:creator>
    <dc:date>2025-01-18T17:02:39Z</dc:date>
    <item>
      <title>Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956500#M42977</link>
      <description>&lt;P&gt;To create indicators that equal to 1 I have to check if 20 variables have certain strings. So for instance if the variables VAR_1 through VAR_2 have "A1" "A2" "A3" I want to create testA=1. I am trying the following code but it doesn't work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let testA=("A1" "A2" "A3");
%let testB=("B1" "B2" "B3");


data NEED;
set HAVE;

%let var_list = TESTA TESTB;
%local i var;
%do i=1 %to %sysfunc(countw(&amp;amp;var_list.));
%let var = %scan(&amp;amp;var_list., &amp;amp;i.);

array cond {20} VAR_1-VAR_20;
do i=1 to dim (cond);
do j=1 to countw('&amp;amp;&amp;amp;&amp;amp;var..', ',');
if (cond {i} in &amp;amp;&amp;amp;&amp;amp;var..) the do;
&amp;amp;var.=1;
end;
end;
end;

%end;
%mend;
%var_list;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 18 Jan 2025 03:55:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956500#M42977</guid>
      <dc:creator>trevand</dc:creator>
      <dc:date>2025-01-18T03:55:17Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956509#M42979</link>
      <description>&lt;P&gt;First, you have not shown us a macro, because there is no %MACRO statement. So that needs to be corrected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When code doesn't work, saying "it doesn't work" and not providing more details is never helpful. We need to see the evidence that it doesn't work, that evidence is likely in the SAS log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you are trying to debug a macro, turn on the macro debugging command by running this line of code&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and then run your macro again, and look at the log. See if you can figure out from the log where the problem is and how to fix it. If you still need help, show us the ENTIRE log by copying the log as text and pasting it into the window that appears when you click on the &amp;lt;/&amp;gt; icon.&amp;nbsp;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;DO NOT SKIP THIS STEP.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PaigeMiller_0-1699900743276.png" style="width: 859px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89703i797B759183DE7484/image-size/large?v=v2&amp;amp;px=999" role="button" title="PaigeMiller_0-1699900743276.png" alt="PaigeMiller_0-1699900743276.png" /&gt;&lt;/span&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Why is the title "macro within macro" when your code doesn't show anything like that?&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 08:31:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956509#M42979</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-18T08:31:35Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956516#M42980</link>
      <description>&lt;P&gt;A general principle:&amp;nbsp; get your code working without macro language.&amp;nbsp; Once that is complete, convert your code to a macro.&amp;nbsp; Looking at your program, a couple of things would stand out.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The letter "n" is missing from the word "then":&amp;nbsp; "the do"&lt;/P&gt;
&lt;P&gt;The IN operator is being used incorrectly.&amp;nbsp; A single value can precede the word IN, with a list of values following the word IN.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is unlikely you want to include parentheses within your list of values, as would happen in your program.&lt;/P&gt;
&lt;P&gt;Also note (this could change the way you structure your program), following the IN operator you are allowed to use an array reference&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 10:01:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956516#M42980</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2025-01-18T10:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956531#M42981</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt; &amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt; I can't copy code of the environment. So typed up the code and had typos here is the correct code: &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let testA=("A1" "A2" "A3");
%let testB=("B1" "B2" "B3");


data NEED;
set HAVE;

%macro var_list;

%let var_list = TESTA TESTB;
%local i var;
%do i=1 %to %sysfunc(countw(&amp;amp;var_list.));
%let var = %scan(&amp;amp;var_list., &amp;amp;i.);

array cond {20} VAR_1-VAR_20;
do i=1 to dim (cond);
do j=1 to countw('&amp;amp;&amp;amp;&amp;amp;var..', ',');
if (cond {i} in &amp;amp;&amp;amp;&amp;amp;var..) then do;
&amp;amp;var.=1;
end;
end;
end;

%end;
%mend;
%var_list;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get the following error: ERROR 124-185: The variable cond has already been defined&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 16:22:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956531#M42981</guid>
      <dc:creator>trevand</dc:creator>
      <dc:date>2025-01-18T16:22:45Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956532#M42982</link>
      <description>&lt;P&gt;Okay, problem #1:&amp;nbsp;Do not define a macro beginning with %MACRO and ending with %MEND inside a data step. Define the macro outside (before) the data step and then call it within the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Problem #2, and agreeing with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;, you absolutely (as in MUST, not optional) get the code to work properly in one situation, without macros and without macro variables. If the code doesn't do what you want without macros and without macro variables, then you will NEVER be able to get it to work with macros and with macro variables.&amp;nbsp; So do that, if you are having trouble doing this, show us the code you tried that doesn't work properly WITHOUT macros and WITHOUT macro variables. And if you get it to work without macros and without macro variables, then we can help you turn it into a macro.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 16:53:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956532#M42982</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-18T16:53:19Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956534#M42983</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt; why do you have to start and end the macro outside of data step?You mean this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro var_list;

data need;
set have;
run;

%mend
&amp;amp;var_list;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 18 Jan 2025 17:02:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956534#M42983</guid>
      <dc:creator>trevand</dc:creator>
      <dc:date>2025-01-18T17:02:39Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956535#M42984</link>
      <description>&lt;P&gt;Your definition of cond is inside the %DO loop, so the ARRAY statement will be repeatedly created, causing the ERROR.&lt;/P&gt;
&lt;P&gt;But that's just the obvious thing. You&amp;nbsp;&lt;U&gt;&lt;STRONG&gt;MUST&lt;/STRONG&gt;&lt;/U&gt; start your macro development with working non-macro code for a single instance. Then decide what must be repeated and therefore created dynamically, and create the macro for that.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 17:14:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956535#M42984</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-01-18T17:14:33Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956537#M42986</link>
      <description>&lt;P&gt;I said&amp;nbsp; "&lt;SPAN&gt;Define the macro outside (before) the data step and then call it within the data step." So, %MACRO all the way down to %MEND is before the data step.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why? Because you have to define a macro (starting with %MACRO and then some code and ending with %MEND) before you can call it. Then you can do this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data need;
    set have;
    %var_list
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this way, the macro code and the data step code will work together properly, if the macro %VAR_LIST is written properly. You can't combine SAS tools (like macros and data steps) willy-nilly without regards to how they work, and expect them to work. What does happen when you execute macro code? It replaces the macro with the code inside the macro, and you CANNOT do things that are not allowed in a data step, here is an example of something that is not allowed in a data step and so won't work, even if it is called by a macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    twas brillig = and the slithy toves;
run;&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;This won't work because you cannot have spaces between variables named TWAS BRILLIG on the left side of the equal sign, you can't even have two variable names on the left side; and similarly the space on the right side will cause an error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But again, I consider it mandatory that you write code that works and does what you want without macros and without macro variables for one instance of this problem you are trying to solve. MANDATORY. You will never make any progress otherwise. Now three people have told you to do this, I expect you will give that a try rather than continuing to attempt to write macros without that step.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 17:20:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956537#M42986</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-18T17:20:36Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956543#M42990</link>
      <description>&lt;P&gt;I figured it out. Below it the code that works.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let testA=("A1" "A2" "A3");
%let testB=("B1" "B2" "B3");


data NEED;
set HAVE;

%macro var_list;

%let var_list = TESTA TESTB;
%local i var;
%do i=1 %to %sysfunc(countw(&amp;amp;var_list.));
%let var = %scan(&amp;amp;var_list., &amp;amp;i.);

array cond_&amp;amp;var. {20} VAR_1-VAR_20;
do i=1 to dim (cond_&amp;amp;var.);
do j=1 to countw('&amp;amp;&amp;amp;&amp;amp;var..', ',');
if (cond_&amp;amp;var.{i} in &amp;amp;&amp;amp;&amp;amp;var..) then do;
&amp;amp;var.=1;
end;
end;
end;

%end;
%mend;
%var_list;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code works even if you start the %macro inside the data step. So I'm not sure about your point &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;. Could you please provide an example when it matters when you start and end the macro and maybe also the code. I'm still confused where you would put&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro var_list;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;%end;
%mend;
%var_list;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 19:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956543#M42990</guid>
      <dc:creator>trevand</dc:creator>
      <dc:date>2025-01-18T19:15:55Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956544#M42991</link>
      <description>&lt;P&gt;Off the top of my head, I don't have an example of why you shouldn't begin a macro definition inside a data step. If I think of one I will let you know, or maybe the others can jump in with an example. And I never said it can't work, I said (or implied) that it is considered a poor practice and should be avoided.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems that you have learned all the wrong lessons here. You appear to have learned that either through brute force or sheer luck or both, you can make this macro work. This is not a good lesson to carry forward, and we have tried to steer you into a better approach, which requires neither brute force nor sheer luck. There's a reason why people like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;(and me) recommend a different approach, and this is because it is less effort and more likely to succeed.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, it's not that your way will never work; it's that it is inefficient and has a higher probability of failure.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 21:00:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956544#M42991</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-18T21:00:35Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956545#M42992</link>
      <description>&lt;P&gt;The main reason to define the macro BEFORE the data step is to avoid confusing yourself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro processor interprets the text of your program and the results are passed onto to the actual SAS language processor to operate.&amp;nbsp; By placing the macro definition inside the DATE step it gives the programmer the false impression that the macro is compiled while the data step is running.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The other reason is it make the code easier to read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So first let's move the macro definition to the first thing in your program.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro var_list;
%local i var varlist;
%let var_list = TESTA TESTB;
%do i=1 %to %sysfunc(countw(&amp;amp;var_list.));
  %let var = %scan(&amp;amp;var_list., &amp;amp;i.);
array cond_&amp;amp;var. {20} VAR_1-VAR_20;
do i=1 to dim (cond_&amp;amp;var.);
  do j=1 to countw('&amp;amp;&amp;amp;&amp;amp;var..', ',');
    if (cond_&amp;amp;var.{i} in &amp;amp;&amp;amp;&amp;amp;var..) then do;
      &amp;amp;var.=1;
    end;
  end;
end;
%end;
%mend ;

%let testA=("A1" "A2" "A3");
%let testB=("B1" "B2" "B3");

data NEED;
  set HAVE;
%var_list;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Notice how I added VAR_LIST to your list of LOCAL macro variables that the macro is defining and using while it executes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now let's try to understand what the macro code is doing.&amp;nbsp; One thing that looks wrong is the use of macro variable references inside of a string quoted with single quotes.&amp;nbsp; Such strings are ignored by the macro processor. So this DO loop&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do j=1 to countw('&amp;amp;&amp;amp;&amp;amp;var..', ',');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will iterate only one time since there are no commas in the string passed as the first argument to the COUNTW() function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And why are you defining multiple arrays that contain the same set of variables?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MPRINT(VAR_LIST): array cond_TESTA {20} VAR_1-VAR_20;
...
MPRINT(VAR_LIST): array cond_TESTB {20} VAR_1-VAR_20;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you explain in words what you are trying to do?&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 21:04:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956545#M42992</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-18T21:04:44Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956546#M42993</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt; oh I did use the advice of you, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;, and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt; gave me. I removed the macro and wrote the code for two cases without the macro. This is how I realized what I was missing.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 21:06:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956546#M42993</guid>
      <dc:creator>trevand</dc:creator>
      <dc:date>2025-01-18T21:06:33Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956547#M42994</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; I have 20 variables that contain different strings. I need to create a lot of different indicator variables depending on combinations of strings in those 20 variables. For instance if any of the 20 variables contain the strings A1, A2, and A3 the indicator testA should equal to 1 and 0 otherwise. If any of the 20 variables contain the strings B1, B2, and B3 the indicator testB should equal to 1 and 0 otherwise.&amp;nbsp;&amp;nbsp; &lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 21:17:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956547#M42994</guid>
      <dc:creator>trevand</dc:creator>
      <dc:date>2025-01-18T21:17:52Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956548#M42995</link>
      <description>&lt;P&gt;So you want to do something like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  row+1;
  input (var_1-var_4) ($);
cards;
A1 C1 .  .
D1 C2 B3 .
. . . .
;

data want;
  set have;
  array var_[4];
  do i=1 to dim(var_) until(testA);
    testA = var_[i] in ('A1' 'A2' 'A3');
  end;
  do i=1 to dim(var_) until(testB);
    testB = var_[i] in ('B1' 'B2' 'B3');
  end;
  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Where the only part that varies are the DO loops at the end.&lt;/P&gt;
&lt;P&gt;You could make a macro that just generates the DO loops.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dx_test(names,codelists);
%local i name codelist ;
%do i=1 %to %sysfunc(countw(&amp;amp;names,%str( )));
  %let name=%scan(&amp;amp;names,&amp;amp;i,%str( ));
  %let codelist=%scan(&amp;amp;codelist,&amp;amp;i,|);
do i=1 to dim(var_) until(&amp;amp;name);
  &amp;amp;name = var_[i] in (&amp;amp;codelist);
end;
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then call it like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array var_[4];
%dx_test(names=testA testB,codelists='A1' 'A2' 'A3'|'B1' 'B2' 'B3')
  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But does that really save you any typing or make the code easier to follow?&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 21:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956548#M42995</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-18T21:28:52Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956550#M42996</link>
      <description>&lt;P&gt;Probably even better to make a really simple macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dx_test(name,codelist);
do i=1 to dim(var_) until(&amp;amp;name);
  &amp;amp;name = var_[i] in (&amp;amp;codelist);
end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And just call it multiple times.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array var_[4];
%dx_test(testA,'A1' 'A2' 'A3')
%dx_test(testB,'B1' 'B2' 'B3')
  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 18 Jan 2025 22:43:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956550#M42996</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-18T22:43:01Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956552#M42998</link>
      <description>&lt;P&gt;If I use the last suggestion I would have to spell out 20 times or so the different indicators that I want to create. That's why I wanted to use the var_list macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you please explain why&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do j=1 to countw('&amp;amp;&amp;amp;&amp;amp;var..', ',');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will only iterate until the first string? In the example that you provided B3 is still found if use the code above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2025 23:36:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956552#M42998</guid>
      <dc:creator>trevand</dc:creator>
      <dc:date>2025-01-18T23:36:21Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956553#M42999</link>
      <description>&lt;P&gt;Try it yourself and see.&lt;/P&gt;
&lt;PRE&gt; 1          data _null_;
 2            do j=1 to countw('&amp;amp;&amp;amp;&amp;amp;var..', ',');
 3              put j=1;
 4            end;
 5          run;
 
 j=1
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds&lt;/PRE&gt;
&lt;P&gt;Try this to see WHY.&lt;/P&gt;
&lt;PRE&gt; 1          data _null_;
 2            string = '&amp;amp;&amp;amp;&amp;amp;var..';
 3            put string=;
 4          run;
 
 string=&amp;amp;&amp;amp;&amp;amp;var..
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds&lt;/PRE&gt;
&lt;P&gt;The macro processor IGNORES strings made using single quote characters instead of double quote characters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have to type the same information either way.&lt;/P&gt;
&lt;P&gt;Better to have the lists in data&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dxgroups;
  infile datalines truncover;
  length varname $32 num 8 code $10 disease $50;
  input varname  disease $50. ;
  do codenum=1 by 1 until(missing(code));
     input code @ ;
     if not missing(code) then output;
  end;
cards;
hpb Hypertension
A1 A2 A3 A4
db Diabetes
B1 B2 B3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and use the data to generate the code.&lt;/P&gt;
&lt;P&gt;For example you could use it to generate calls to %DXTEST() macro I showed.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  file code;
  set dxgroups;
&amp;nbsp;&amp;nbsp;by&amp;nbsp;varname&amp;nbsp;notsorted;
&amp;nbsp;&amp;nbsp;if&amp;nbsp;first.varname&amp;nbsp;then&amp;nbsp;put&amp;nbsp;'%dxtest('&amp;nbsp;varname&amp;nbsp;','&amp;nbsp;@;
  put code :$quote. @;
  if last.varname then put ')';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then you can use %INCLUDE to execute that generated code where you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array var_[20] ;
%include code / source2;
  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 19 Jan 2025 00:04:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956553#M42999</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-19T00:04:46Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956554#M43000</link>
      <description>&lt;P&gt;But this should be okay then?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let testA=("A1" "A2" "A3");
%let testB=("B1" "B2" "B3");


data NEED;
set HAVE;

array cond_testA {20} VAR_1-VAR_20;
do i=1 to dim (cond_testA);
do j=1 to countw('&amp;amp;testA.', ',');
if (cond_testA {i} in &amp;amp;testA.) then do;
testA=1;
end;
end;
end;

array cond_testB {20} VAR_1-VAR_20;
do i=1 to dim (cond_testB);
do j=1 to countw('&amp;amp;testB.', ',');
if (cond_testB {i} in &amp;amp;testB.) then do;
testB=1;
end;
end;
end;


run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The problem only arises with &amp;amp;&amp;amp;&amp;amp;var..? Sorry I'm still confused. I'm coming from stata where it's super easy to loop through strings.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jan 2025 00:28:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956554#M43000</guid>
      <dc:creator>trevand</dc:creator>
      <dc:date>2025-01-19T00:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956555#M43001</link>
      <description>&lt;P&gt;There is a huge difference to SAS macro processor between&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do j=1 to countw('&amp;amp;testB.', ',');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do j=1 to countw("&amp;amp;testB.", ',');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And even if you changed to the second then SAS code that the macro processor would pass onto SAS to execute would look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do j=1 to countw("("B1" "B2" "B3")", ',');
if (cond_testB {i} in ("B1" "B2" "B3")) then do;
testB=1;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which not only is invalid syntax it does not make any sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did you mean to do something like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;testB=0;
do i=1 to dim(cond_testB);
  if (cond_testB[i] in &amp;amp;testB.) then testB=1;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If so then that is what the DO loops I showed are doing,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to dim(cond_testB) until(testB);
  testB= (cond_testB[i] in &amp;amp;testB.) ;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but they stop looping as soon as there is a match.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So your code worked because the two mistakes (miss use of macro code and the extra unneeded DO loop) cancel each other out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note also that [ and ] are easier to type and easier to distinguish from ( and ) in the code than { and }.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jan 2025 01:18:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956555#M43001</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-19T01:18:36Z</dc:date>
    </item>
    <item>
      <title>Re: Macro within Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956560#M43002</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  row+1;
  input (var_1-var_4) ($);
cards;
A1 C1 .  .
D1 C2 B3 .
. . . .
;

data want;
  set have;
  array var_[4];
  do i=1 to dim(var_) until(testA);
    testA = var_[i] in ('A1' 'A2' 'A3');
  end;
  do i=1 to dim(var_) until(testB);
    testB = var_[i] in ('B1' 'B2' 'B3');
  end;
  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I will just use this code as you suggested. One question though. Why do you need until(testA)?&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jan 2025 01:37:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-within-Macro/m-p/956560#M43002</guid>
      <dc:creator>trevand</dc:creator>
      <dc:date>2025-01-19T01:37:13Z</dc:date>
    </item>
  </channel>
</rss>

