<?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: Can someone help me troubleshoot my macro code?  Thanks! in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Can-someone-help-me-troubleshoot-my-macro-code-Thanks/m-p/974225#M377841</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/477636"&gt;@colettb11&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I repeat what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;said, use an array.&lt;/P&gt;</description>
    <pubDate>Fri, 05 Sep 2025 10:42:40 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2025-09-05T10:42:40Z</dc:date>
    <item>
      <title>Can someone help me troubleshoot my macro code?  Thanks!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-someone-help-me-troubleshoot-my-macro-code-Thanks/m-p/974163#M377825</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset that contains variables dt_1 through dt_7 and start date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I&amp;nbsp;need to loop through the dt_ variables to see which one start_date is after.&amp;nbsp; I want to check dt_7, then dt_6, then dt_5, etc, and then assign the corresponding value of tier_.&amp;nbsp; (For example, if start date is &amp;gt;= dt_7 then I want to assign the value of tier_7 to applied_tier).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I run this code, &amp;amp;i is resolving to the correct number, however it is not returning the value of dt_7, dt_6, etc.&amp;nbsp; It's just returning the string "dt_7",'dt_6", etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd appreciate some feedback on where I'm going wrong in my code.&amp;nbsp; My company doesn't allow uploading of files to this website so here is some sample ( I know it's hard to read).&amp;nbsp; In this case the macro should return the value of tier_5 because the first date that Start Date is after (going from right to left) is 24MAY2024&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Start Date&amp;nbsp; &amp;nbsp; &amp;nbsp; dt_1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Tier_1&amp;nbsp; &amp;nbsp; dt_2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Tier_2&amp;nbsp; &amp;nbsp; &amp;nbsp; dt_3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Tier_3&amp;nbsp; &amp;nbsp; &amp;nbsp; dt_4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Tier_4&amp;nbsp; &amp;nbsp; &amp;nbsp; dt_5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Tier_5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dt_6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Tier_6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dt_7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Tier_7&lt;BR /&gt;9/30/2024&amp;nbsp; &amp;nbsp; 17-Nov-22&amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22-Mar-24&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;23-Mar-24&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 8-May-24&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;18-Oct-24&amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;21-Jan-25&amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25-Mar-25&amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%macro test;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;data test;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;merge v (in=a)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; trans (in=b);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;by site_number;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if a;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%let i = 7;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%do %while (&amp;amp;i &amp;gt; 0);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%if (dt_&amp;amp;i ne . and start_date ge dt_&amp;amp;i) %then %do;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;applied_tier = tier_&amp;amp;i;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%end;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%let i = %eval(&amp;amp;i - 1);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%end;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;%mend test;&lt;/DIV&gt;&lt;DIV&gt;%test;&lt;/DIV&gt;</description>
      <pubDate>Thu, 04 Sep 2025 20:28:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-someone-help-me-troubleshoot-my-macro-code-Thanks/m-p/974163#M377825</guid>
      <dc:creator>colettb11</dc:creator>
      <dc:date>2025-09-04T20:28:12Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone help me troubleshoot my macro code?  Thanks!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-someone-help-me-troubleshoot-my-macro-code-Thanks/m-p/974173#M377828</link>
      <description>&lt;P&gt;What do you mean by "&lt;SPAN&gt;the macro should return the value of tier_5"?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;In general the macro language just generates code.&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;SAS itself is what runs the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your case t&lt;SPAN&gt;he macro is generating a DATA step. So what is the data step that you want to run?&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;Type that code out by hand and then you can begin to work on creating a macro that generates the code you want.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Do you even need to generate any code?? (and hence do you even need to create a macro??)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Why not just use an ARRAY?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Perhaps something like this?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  merge v (in=a)
    trans (in=b)
  ;
  by site_number;
  if a;
  array dt_ [7];
  array Tier_ [7];
  do index=dim(dt_) to 1 by -1 until (not missing(applied_tier));
    if .Z &amp;lt; dt_[index] &amp;lt;= start_date then applied_tier = tier_[index];
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that you current macro logic is just going to generate 7 assignment statement as part of the data step:&lt;/P&gt;
&lt;PRE&gt;MPRINT(TEST):   applied_tier = tier_7;
MPRINT(TEST):   applied_tier = tier_6;
MPRINT(TEST):   applied_tier = tier_5;
MPRINT(TEST):   applied_tier = tier_4;
MPRINT(TEST):   applied_tier = tier_3;
MPRINT(TEST):   applied_tier = tier_2;
MPRINT(TEST):   applied_tier = tier_1;
&lt;/PRE&gt;
&lt;P&gt;That is because the condition in your %IF statements is TRUE for all values of the macro variable I.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;dt_&amp;amp;i ne .&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A string starting with the letters dt is never never going to match a period.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;start_date ge dt_&amp;amp;i&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And a string starting with s is always greater than a string starting with d.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Sep 2025 00:09:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-someone-help-me-troubleshoot-my-macro-code-Thanks/m-p/974173#M377828</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-09-05T00:09:34Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone help me troubleshoot my macro code?  Thanks!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-someone-help-me-troubleshoot-my-macro-code-Thanks/m-p/974225#M377841</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/477636"&gt;@colettb11&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I repeat what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;said, use an array.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Sep 2025 10:42:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-someone-help-me-troubleshoot-my-macro-code-Thanks/m-p/974225#M377841</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-09-05T10:42:40Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone help me troubleshoot my macro code?  Thanks!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-someone-help-me-troubleshoot-my-macro-code-Thanks/m-p/974233#M377845</link>
      <description>&lt;P&gt;Array that has been suggested is a good alternative.&lt;/P&gt;
&lt;P&gt;The reason why your macro is not working is because you are using DATA step variables on a %IF condition which is not correct. If you change your code to use an IF statement, you should get the expected results.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;
 
data test;
merge v (in=a)
  trans (in=b);
by site_number;
if a;
 
%let i = 7;
 
%do %while (&amp;amp;i &amp;gt; 0);
if (dt_&amp;amp;i ne . and start_date ge dt_&amp;amp;i) then do;
 applied_tier = tier_&amp;amp;i;
end;
%let i = %eval(&amp;amp;i - 1);
%end;
 
run;
%mend test;
options mprint mlogic symbolgen;
%test;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Sep 2025 12:18:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-someone-help-me-troubleshoot-my-macro-code-Thanks/m-p/974233#M377845</guid>
      <dc:creator>Kathryn_SAS</dc:creator>
      <dc:date>2025-09-05T12:18:28Z</dc:date>
    </item>
  </channel>
</rss>

