<?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: Array to uptade row where criteria is met in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Array-to-uptade-row-where-criteria-is-met/m-p/919252#M362087</link>
    <description>&lt;P&gt;Do you mean something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Before:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="yabwon_0-1709797101374.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94452i41405391C77DBFA0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="yabwon_0-1709797101374.png" alt="yabwon_0-1709797101374.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if 0 then set points;
  declare hash H (dataset:"points");
  H.DefineKey("COL1");
  H.DefineData("COL2");
  H.DefineDone();

  do until(EOF);
    set base3 end=EOF;

    array Rule Rule:;

    do over Rule;
      if Rule = 1 then 
        do;
          COL1 = vname(Rule);
          if  0 = H.find() then Rule = Col2;
        end;
    end;
    output;
    drop COL2 COL1;
  end;
stop;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;After:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="yabwon_1-1709797132250.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94453iD066249B20EC2DC3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="yabwon_1-1709797132250.png" alt="yabwon_1-1709797132250.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 07 Mar 2024 07:38:59 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2024-03-07T07:38:59Z</dc:date>
    <item>
      <title>Array to uptade row where criteria is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-to-uptade-row-where-criteria-is-met/m-p/919244#M362084</link>
      <description>&lt;P&gt;Dear community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With the following code I want to add new column to existing dataset by defining array and this array should add value if my criteria met. For instance suppose that I have record and this record has 1 for first&amp;nbsp; and third columns. If value is 1 then I expect my array to add&amp;nbsp; corresponding numeric value to this rules (i.e 20, 30 , what is defined in points table). However following code updates each matched column's value with correspondingfinal loop value &lt;EM&gt;(my array loop finishes at 5 and&amp;nbsp; if Rule5's value is 100, than rest of the matched columns updated with 100).&lt;/EM&gt;&amp;nbsp; How to preserve each rule's own value and not to replace previous one's value with the next updated one?&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data points(DROP= i);&lt;BR /&gt;do i=1 TO 10;&lt;BR /&gt;COL1 = CATS("Rule",i);&lt;BR /&gt;COL2 = 10 * i;&lt;BR /&gt;OUTPUT;&lt;BR /&gt;END;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data base (drop=i);&lt;BR /&gt;do i=1 TO 5;&lt;BR /&gt;CL_NO = i;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data base2 (drop=i);&lt;BR /&gt;do i=1 to 5;&lt;BR /&gt;CL_NO = i;&lt;BR /&gt;Rule1 = RAND("integer",0,1);&lt;BR /&gt;Rule2 = RAND("integer",0,1);&lt;BR /&gt;Rule3 = RAND("integer",0,1);&lt;BR /&gt;Rule4 = RAND("integer",0,1);&lt;BR /&gt;Rule5 = RAND("integer",0,1);&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table base3 as&lt;BR /&gt;select a.CL_NO, Rule1, Rule2, Rule3,Rule4,Rule5&lt;BR /&gt;from base a left join base2 b&lt;BR /&gt;on a.cl_no = b.cl_no;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;options mprint;&lt;BR /&gt;%macro main_macro();&lt;/P&gt;
&lt;P&gt;%macro another_macro(inp_trig,inp_index);&lt;/P&gt;
&lt;P&gt;data out_&amp;amp;inp_index.;&lt;BR /&gt;set points;&lt;BR /&gt;where COL1 = "&amp;amp;inp_trig.";&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;%global puan_&amp;amp;inp_index.;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;select col2&lt;BR /&gt;into : point_&amp;amp;inp_index.&lt;BR /&gt;from out_&amp;amp;inp_index.;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;DATA RESULT;&lt;BR /&gt;SET base3;&lt;BR /&gt;ARRAY RULE[*] Rule:;&lt;BR /&gt;ARRAY RESULT[5] RESULT1-RESULT5;&lt;/P&gt;
&lt;P&gt;DO i=1 to 5;&lt;/P&gt;
&lt;P&gt;temp_result = CATS("Rule",i);&lt;BR /&gt;call symputx("temp_result_macro",temp_result,"G");&lt;BR /&gt;call symputx("index",i,"G");&lt;/P&gt;
&lt;P&gt;if RULE[i] = 1 then do;&lt;/P&gt;
&lt;P&gt;call execute('%another_macro(&amp;amp;temp_result_macro,&amp;amp;index)');&lt;/P&gt;
&lt;P&gt;result[i] = &amp;amp;&amp;amp;point_&amp;amp;index.;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%main_macro;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2024 06:58:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-to-uptade-row-where-criteria-is-met/m-p/919244#M362084</guid>
      <dc:creator>ern_23</dc:creator>
      <dc:date>2024-03-07T06:58:08Z</dc:date>
    </item>
    <item>
      <title>Re: Array to uptade row where criteria is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-to-uptade-row-where-criteria-is-met/m-p/919247#M362085</link>
      <description>&lt;P&gt;Really appreciated that you share code that creates sample data and includes what you've done so far.&lt;/P&gt;
&lt;P&gt;I'm unfortunately still not fully&amp;nbsp;following your explanation and it's for me too much uncommented code to try and work out/guess what you're trying to achieve.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What would help me is if you just would share code that creates your HAVE tables, a desired result (code that creates a WANT table or just grid with the desired values) and then an explanation how you intend to get from Have to Want.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2024 07:19:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-to-uptade-row-where-criteria-is-met/m-p/919247#M362085</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-03-07T07:19:55Z</dc:date>
    </item>
    <item>
      <title>Re: Array to uptade row where criteria is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-to-uptade-row-where-criteria-is-met/m-p/919252#M362087</link>
      <description>&lt;P&gt;Do you mean something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Before:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="yabwon_0-1709797101374.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94452i41405391C77DBFA0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="yabwon_0-1709797101374.png" alt="yabwon_0-1709797101374.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if 0 then set points;
  declare hash H (dataset:"points");
  H.DefineKey("COL1");
  H.DefineData("COL2");
  H.DefineDone();

  do until(EOF);
    set base3 end=EOF;

    array Rule Rule:;

    do over Rule;
      if Rule = 1 then 
        do;
          COL1 = vname(Rule);
          if  0 = H.find() then Rule = Col2;
        end;
    end;
    output;
    drop COL2 COL1;
  end;
stop;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;After:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="yabwon_1-1709797132250.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94453iD066249B20EC2DC3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="yabwon_1-1709797132250.png" alt="yabwon_1-1709797132250.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2024 07:38:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-to-uptade-row-where-criteria-is-met/m-p/919252#M362087</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-03-07T07:38:59Z</dc:date>
    </item>
    <item>
      <title>Re: Array to uptade row where criteria is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-to-uptade-row-where-criteria-is-met/m-p/919253#M362088</link>
      <description>Thank you, this is what I want.  Just for wondering, in my macro code I am trying to catch and assign point of rule with the following statement; &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;if RULE[i] = 1 then do;&lt;BR /&gt;&lt;BR /&gt;call execute('%another_macro(&amp;amp;temp_result_macro,&amp;amp;index)');&lt;BR /&gt;&lt;BR /&gt;result[i] = &amp;amp;&amp;amp;point_&amp;amp;index.;&lt;BR /&gt;&lt;BR /&gt;so I expect in each iteration, my Rule's point should assign into &amp;amp;point_1, &amp;amp;point_2 etc macro variables. This approach also works , however in the end of the iteration , it updates all rules that meet condition with the Rule5's point. Is there a way fix this issue in the macro and ensure each rule takes its own value ?</description>
      <pubDate>Thu, 07 Mar 2024 08:04:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-to-uptade-row-where-criteria-is-met/m-p/919253#M362088</guid>
      <dc:creator>ern_23</dc:creator>
      <dc:date>2024-03-07T08:04:56Z</dc:date>
    </item>
    <item>
      <title>Re: Array to uptade row where criteria is met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-to-uptade-row-where-criteria-is-met/m-p/919256#M362090</link>
      <description>&lt;P&gt;It doesn't seem to work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I run the macro I got the following in the log:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1    %main_macro;
MLOGIC(MAIN_MACRO):  Beginning execution.
MPRINT(MAIN_MACRO):   DATA RESULT;
MPRINT(MAIN_MACRO):   SET base3;
MPRINT(MAIN_MACRO):   ARRAY RULE[*] Rule:;
MPRINT(MAIN_MACRO):   ARRAY RESULT[5] RESULT1-RESULT5;
MPRINT(MAIN_MACRO):   DO i=1 to 5;
MPRINT(MAIN_MACRO):   temp_result = CATS("Rule",i);
MPRINT(MAIN_MACRO):   call symputx("temp_result_macro",temp_result,"G");
MPRINT(MAIN_MACRO):   call symputx("index",i,"G");
MPRINT(MAIN_MACRO):   if RULE[i] = 1 then do;
MPRINT(MAIN_MACRO):   call execute('%another_macro(&amp;amp;temp_result_macro,&amp;amp;index)');
SYMBOLGEN:  &amp;amp;&amp;amp; resolves to &amp;amp;.
WARNING: Apparent symbolic reference INDEX not resolved.
SYMBOLGEN:  Unable to resolve the macro variable reference &amp;amp;index
NOTE: Line generated by the macro variable "INDEX".
1     &amp;amp;point_&amp;amp;
      -
      22
WARNING: Apparent symbolic reference POINT_ not resolved.
WARNING: Apparent symbolic reference INDEX not resolved.
NOTE 137-205: Line generated by the invoked macro "MAIN_MACRO".
3    ('%another_macro(&amp;amp;temp_result_macro,&amp;amp;index)');  result[i] = &amp;amp;&amp;amp;point_&amp;amp;index.;  end;  end;  run;
                                                                          ------
                                                                          22
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant,
              a datetime constant, a missing value, INPUT, PUT.

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant,
              a datetime constant, a missing value, arrayname, (, +, -, INPUT, NOT, PUT, ^, _NEW_, ~.

NOTE: Line generated by the invoked macro "MAIN_MACRO".
3    ('%another_macro(&amp;amp;temp_result_macro,&amp;amp;index)');  result[i] = &amp;amp;&amp;amp;point_&amp;amp;index.;  end;  end;  run;
                                                                          ------
                                                                          201
ERROR 201-322: The option is not recognized and will be ignored.

MPRINT(MAIN_MACRO):   result[i] = &amp;amp;point_&amp;amp; index.;
MPRINT(MAIN_MACRO):   end;
MPRINT(MAIN_MACRO):   end;
MPRINT(MAIN_MACRO):   run;

INFO: Character variables have defaulted to a length of 200 at the places given by: (Line):(Column).
      Truncation can result.
      2:65     temp_result
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.RESULT may be incomplete.  When this step was stopped there were 0
         observations and 14 variables.
WARNING: Data set WORK.RESULT was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


MLOGIC(MAIN_MACRO):  Ending execution.
&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;Basically, the reason it does _not_ work is bad "timing" in the program logic.&lt;/P&gt;
&lt;P&gt;The:&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;call execute('%another_macro(&amp;amp;temp_result_macro,&amp;amp;index)');&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;line executes after the:&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;result[i] = &amp;amp;&amp;amp;point_&amp;amp;index.;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;line needs it.&lt;/P&gt;
&lt;P&gt;Call Execute() executes long after you need its results, if you run:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  call execute('proc sql; select sum(age) into: S_A from sashelp.class; quit;');

  x = "&amp;amp;S_A.";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the log will show warning about not resolved symbol in compilation phase of the data step, long before SQL was executed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1    data _null_;
2      call execute('proc sql; select sum(age) into: S_A from sashelp.class; quit;');
3
4      x = "&amp;amp;S_A.";
WARNING: Apparent symbolic reference S_A not resolved.
5    run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


NOTE: CALL EXECUTE generated line.
1   + proc sql;
1   +           select sum(age) into: S_A from sashelp.class;
1   +                                                         quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds
&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;Proper timing when using macro language, if one does not have experience, can be hard.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I can share an advice, despite my limitless affection to the macro language, a "golden rules" (which is a paraphrase of saying about regular expressions) which goes like this: "If you can equivalently do something with macro language and without it, do it without it." &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2024 08:47:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-to-uptade-row-where-criteria-is-met/m-p/919256#M362090</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-03-07T08:47:14Z</dc:date>
    </item>
  </channel>
</rss>

