<?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: Help with macro loop in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/605863#M17308</link>
    <description>&lt;PRE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;I have two sets of column names in my database such as columnA_1, columnA_2 ... all the way till 17. And another column name like columnB_1, columnB_2... all the way to 9. I want to create a new variable that checks if these columns have same values (character) and how many of them have same values. For Example consider the following table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A_1&amp;nbsp; &amp;nbsp; &amp;nbsp;A_2&amp;nbsp; &amp;nbsp; &amp;nbsp; A_3&amp;nbsp; &amp;nbsp; &amp;nbsp; A_4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B_1&amp;nbsp; &amp;nbsp; &amp;nbsp;B_2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B_3&amp;nbsp; &amp;nbsp; B_4&lt;/P&gt;&lt;P&gt;John&amp;nbsp; &amp;nbsp;Adam&amp;nbsp; &amp;nbsp;David&amp;nbsp; &amp;nbsp;Sam&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Adam&amp;nbsp; John&amp;nbsp; &amp;nbsp; Matt&amp;nbsp; &amp;nbsp; Tom&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the above data, I want the code to check all the columns from A_X with values of columns in B_X and give me a number = 2. i.e Adam and John is common between them. This is the code I am trying but is not working at all. I am not an expert in SAS so I am learning by doing but am unsuccessful so far. Any help is appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Image 11-20-19 at 11.39 AM.jpg" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34115iAC3A60B6B0F56A0B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Image 11-20-19 at 11.39 AM.jpg" alt="Image 11-20-19 at 11.39 AM.jpg" /&gt;&lt;/span&gt;
%macro create(adv);
data nestedcommon2;
set nestedcommon1;
%do i=1 %to 17;
%do j= 1 %to 9;
common=0;
%if acq_adv_&amp;amp;i=tar_adv_&amp;amp;j %then common =  common+1;
%end;
%end;
 final=common;
run;
%mend create;
%create(153)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Mjizzle&lt;/P&gt;</description>
    <pubDate>Wed, 20 Nov 2019 17:58:26 GMT</pubDate>
    <dc:creator>mjizzle</dc:creator>
    <dc:date>2019-11-20T17:58:26Z</dc:date>
    <item>
      <title>Help with macro loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/595376#M15796</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was wondering if someone could explain this loop for me please, and if it's well indented and stuff.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;put "loop start";
     %do j=1 %to 9 ;
          %let k=%eval(&amp;amp;j-1);
          put "boucle j=&amp;amp;j, k=&amp;amp;k";       
          if surv&amp;amp;k&amp;gt;0 and surv&amp;amp;j=0 
          then do;
                surv&amp;amp;j=datesurv; 
                put "loop end";
          end;
          else do;
     %end;
     %do j=1 %to 9 ;
          end;
     %end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks a lot&lt;/P&gt;</description>
      <pubDate>Thu, 10 Oct 2019 13:20:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/595376#M15796</guid>
      <dc:creator>polpel</dc:creator>
      <dc:date>2019-10-10T13:20:23Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macro loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/595381#M15798</link>
      <description>&lt;P&gt;You want style feedback?&lt;/P&gt;
&lt;P&gt;The first PUT statement is wrong.&amp;nbsp; The generated SAS code does not have any loops.&amp;nbsp; It is just 9 nested IF/THEN statements.&lt;/P&gt;
&lt;P&gt;The other PUT statements also don't make much sense.&amp;nbsp; Are you trying to see which IF statements are being evaluated for every observation?&amp;nbsp; Why not just store that information into another variable instead?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want functionality feedback provide explanation of what the code is trying to do and provide input data and expected output.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Oct 2019 13:30:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/595381#M15798</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-10T13:30:53Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macro loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/595388#M15799</link>
      <description>&lt;P&gt;Almost classic abuse of the macro language for data manipulation. The poor creature who came up with this should have all memory of macros removed from their brains, so that there will be enough space to learn how to use Base SAS properly:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array surv[0:9] surv0-surv9;
do j = 1 to 9;
  if surv[j-1] &amp;gt; 0 and surv[j] = 0
  then do;
    surv[j] = datesurv;
    j=10;
  end;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Oct 2019 13:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/595388#M15799</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-10T13:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macro loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/597290#M16128</link>
      <description>&lt;P&gt;The person who wrote that code explained it to me and it actually makes a lot of sense.&lt;/P&gt;
&lt;P&gt;He cleverly used macro language to actually write SAS code which is how you should be using macro language.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2019 12:08:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/597290#M16128</guid>
      <dc:creator>polpel</dc:creator>
      <dc:date>2019-10-17T12:08:37Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macro loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/597295#M16132</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/291423"&gt;@polpel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The person who wrote that code explained it to me and it actually makes a lot of sense.&lt;/P&gt;
&lt;P&gt;He cleverly used macro language to actually write SAS code which is how you should be using macro language.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, that's what one uses SAS macro language mostly for: To dynamically create SAS code. BUT: The rule of thumb is to only use macro language if you can't do it with normal SAS language - and this is not the case here.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Compare the macro code you've posted with the Base SAS language alternative&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;proposes. What do you think is easier to understand, maintain and debug?&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2019 12:20:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/597295#M16132</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-10-17T12:20:38Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macro loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/597298#M16133</link>
      <description>&lt;P&gt;Okay yes I see. Thanks again for all the help and explanations!&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2019 12:32:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/597298#M16133</guid>
      <dc:creator>polpel</dc:creator>
      <dc:date>2019-10-17T12:32:10Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macro loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/605863#M17308</link>
      <description>&lt;PRE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;I have two sets of column names in my database such as columnA_1, columnA_2 ... all the way till 17. And another column name like columnB_1, columnB_2... all the way to 9. I want to create a new variable that checks if these columns have same values (character) and how many of them have same values. For Example consider the following table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A_1&amp;nbsp; &amp;nbsp; &amp;nbsp;A_2&amp;nbsp; &amp;nbsp; &amp;nbsp; A_3&amp;nbsp; &amp;nbsp; &amp;nbsp; A_4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B_1&amp;nbsp; &amp;nbsp; &amp;nbsp;B_2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B_3&amp;nbsp; &amp;nbsp; B_4&lt;/P&gt;&lt;P&gt;John&amp;nbsp; &amp;nbsp;Adam&amp;nbsp; &amp;nbsp;David&amp;nbsp; &amp;nbsp;Sam&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Adam&amp;nbsp; John&amp;nbsp; &amp;nbsp; Matt&amp;nbsp; &amp;nbsp; Tom&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the above data, I want the code to check all the columns from A_X with values of columns in B_X and give me a number = 2. i.e Adam and John is common between them. This is the code I am trying but is not working at all. I am not an expert in SAS so I am learning by doing but am unsuccessful so far. Any help is appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Image 11-20-19 at 11.39 AM.jpg" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34115iAC3A60B6B0F56A0B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Image 11-20-19 at 11.39 AM.jpg" alt="Image 11-20-19 at 11.39 AM.jpg" /&gt;&lt;/span&gt;
%macro create(adv);
data nestedcommon2;
set nestedcommon1;
%do i=1 %to 17;
%do j= 1 %to 9;
common=0;
%if acq_adv_&amp;amp;i=tar_adv_&amp;amp;j %then common =  common+1;
%end;
%end;
 final=common;
run;
%mend create;
%create(153)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Mjizzle&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2019 17:58:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/605863#M17308</guid>
      <dc:creator>mjizzle</dc:creator>
      <dc:date>2019-11-20T17:58:26Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macro loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/605865#M17309</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/300505"&gt;@mjizzle&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;I have two sets of column names in my database such as columnA_1, columnA_2 ... all the way till 17. And another column name like columnB_1, columnB_2... all the way to 9. I want to create a new variable that checks if these columns have same values (character) and how many of them have same values. For Example consider the following table:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A_1&amp;nbsp; &amp;nbsp; &amp;nbsp;A_2&amp;nbsp; &amp;nbsp; &amp;nbsp; A_3&amp;nbsp; &amp;nbsp; &amp;nbsp; A_4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B_1&amp;nbsp; &amp;nbsp; &amp;nbsp;B_2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B_3&amp;nbsp; &amp;nbsp; B_4&lt;/P&gt;
&lt;P&gt;John&amp;nbsp; &amp;nbsp;Adam&amp;nbsp; &amp;nbsp;David&amp;nbsp; &amp;nbsp;Sam&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Adam&amp;nbsp; John&amp;nbsp; &amp;nbsp; Matt&amp;nbsp; &amp;nbsp; Tom&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the above data, I want the code to check all the columns from A_X with values of columns in B_X and give me a number = 2. i.e Adam and John is common between them. This is the code I am trying but is not working at all. I am not an expert in SAS so I am learning by doing but am unsuccessful so far. Any help is appreciated.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Image 11-20-19 at 11.39 AM.jpg" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34115iAC3A60B6B0F56A0B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Image 11-20-19 at 11.39 AM.jpg" alt="Image 11-20-19 at 11.39 AM.jpg" /&gt;&lt;/span&gt;
%macro create(adv);
data nestedcommon2;
set nestedcommon1;
%do i=1 %to 17;
%do j= 1 %to 9;
common=0;
%if acq_adv_&amp;amp;i=tar_adv_&amp;amp;j %then common =  common+1;
%end;
%end;
 final=common;
run;
%mend create;
%create(153)&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;As stated above to the original poster, get rid of macros and macro variables here. They are a completely unnecessary (and in this case ineffective) way to accomplish the goal. Learn to use data steps and the power of data steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Specifically, use ARRAYs when you are in a data step and want to perform tasks across multiple variables in the same observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Untested Code */

data nestedcommon2;
    set nestedcommon1;
    array acq_adv acq_adv1-acq_adv17;
    array tar_adv tar_adv1-tar_adv9;
    common=0;
    do i=1 to 17;
        do j=1 to 9;
             if acq_adv(i)=tar-adv(j) then common=common+1;
        and;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2019 18:25:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/605865#M17309</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-20T18:25:05Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macro loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/605963#M17317</link>
      <description>&lt;P&gt;Perfect! Thank you so much for your quick reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;Mjizzle&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2019 22:36:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Help-with-macro-loop/m-p/605963#M17317</guid>
      <dc:creator>mjizzle</dc:creator>
      <dc:date>2019-11-20T22:36:38Z</dc:date>
    </item>
  </channel>
</rss>

