<?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 Retain and update across array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-and-update-across-array/m-p/647052#M193606</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the below dataset where some events happen. C- stands for Create and D- stands for Delete.&lt;/P&gt;&lt;P&gt;At some point in time, I am creating some values (i.e. C-a) or delete them (D-a) within an array of N Events.&lt;/P&gt;&lt;P&gt;My task would be to retain the previous created values along with the new created values, and if a detelete event occurs, then the output should stop retaining the value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;infile datalines delimiter=',';&lt;BR /&gt;input ID date mmddyy10. Event1 $ Event2 $ Event3 $ Event4 $;&lt;BR /&gt;format date date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;1,03-31-2013,C-a,C-b,.,.&lt;BR /&gt;1,05-31-2013,D-b,.,.,.&lt;BR /&gt;1,07-31-2013,C-d,C-e,C-f,.&lt;BR /&gt;1,08-31-2013,D-e,C-g,.,.&lt;BR /&gt;2,04-30-2018,C-h,C-i,D-h,C-j&lt;BR /&gt;2,07-31-2018,C-k,.,.,.&lt;BR /&gt;2,12-31-2018,D-i,C-o,D-j,D-k&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;infile datalines delimiter=',';&lt;BR /&gt;input ID date mmddyy10. Event1 $ Event2 $ Event3 $ Event4 $;&lt;BR /&gt;format date date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;1,03-31-2013,C-a,C-b,.,.&lt;BR /&gt;1,05-31-2013,C-a,.,.,.&lt;BR /&gt;1,07-31-2013,C-a,C-d,C-e,C-f&lt;BR /&gt;1,08-31-2013,C-a,C-d,C-f,C-g&lt;BR /&gt;2,04-30-2018,C-i,C-j,.,.&lt;BR /&gt;2,07-31-2018,C-i,C-j,C-k,.&lt;BR /&gt;2,12-31-2018,C-o,.,.,.&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would very much appreciate your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
    <pubDate>Tue, 12 May 2020 11:20:21 GMT</pubDate>
    <dc:creator>victoras1893</dc:creator>
    <dc:date>2020-05-12T11:20:21Z</dc:date>
    <item>
      <title>Retain and update across array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-and-update-across-array/m-p/647052#M193606</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the below dataset where some events happen. C- stands for Create and D- stands for Delete.&lt;/P&gt;&lt;P&gt;At some point in time, I am creating some values (i.e. C-a) or delete them (D-a) within an array of N Events.&lt;/P&gt;&lt;P&gt;My task would be to retain the previous created values along with the new created values, and if a detelete event occurs, then the output should stop retaining the value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;infile datalines delimiter=',';&lt;BR /&gt;input ID date mmddyy10. Event1 $ Event2 $ Event3 $ Event4 $;&lt;BR /&gt;format date date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;1,03-31-2013,C-a,C-b,.,.&lt;BR /&gt;1,05-31-2013,D-b,.,.,.&lt;BR /&gt;1,07-31-2013,C-d,C-e,C-f,.&lt;BR /&gt;1,08-31-2013,D-e,C-g,.,.&lt;BR /&gt;2,04-30-2018,C-h,C-i,D-h,C-j&lt;BR /&gt;2,07-31-2018,C-k,.,.,.&lt;BR /&gt;2,12-31-2018,D-i,C-o,D-j,D-k&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;infile datalines delimiter=',';&lt;BR /&gt;input ID date mmddyy10. Event1 $ Event2 $ Event3 $ Event4 $;&lt;BR /&gt;format date date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;1,03-31-2013,C-a,C-b,.,.&lt;BR /&gt;1,05-31-2013,C-a,.,.,.&lt;BR /&gt;1,07-31-2013,C-a,C-d,C-e,C-f&lt;BR /&gt;1,08-31-2013,C-a,C-d,C-f,C-g&lt;BR /&gt;2,04-30-2018,C-i,C-j,.,.&lt;BR /&gt;2,07-31-2018,C-i,C-j,C-k,.&lt;BR /&gt;2,12-31-2018,C-o,.,.,.&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would very much appreciate your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 11:20:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-and-update-across-array/m-p/647052#M193606</guid>
      <dc:creator>victoras1893</dc:creator>
      <dc:date>2020-05-12T11:20:21Z</dc:date>
    </item>
    <item>
      <title>Re: Retain and update across array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-and-update-across-array/m-p/647076#M193616</link>
      <description>&lt;P&gt;hey, try this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want;
   length list $16;
   set have;
   by id;
   retain list '';
   array events(*) Event1-Event4;
   
   if first.id then call missing(list);

   *Determine list of events;
   do y=1 to dim(events);      
      if not missing(events(y)) then do;
         if not prxmatch('/(C|D)-[a-z]/',events(y)) then put 'W' 'ARNING: event does not respect expected syntax' events(y)=;
         list=left(compbl(ifc(substr(events(y),1,1) eq 'C',catx(' ',list,events(y)),tranwrd(list,'C-'||substr(events(y),3,1),''))));
      end;
   end;
   
   *reassign events according to their position in the list;
   do z=1 to dim(events);
      events(z)=scan(list,z,' ');
   end;
   drop list y z;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 May 2020 12:54:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-and-update-across-array/m-p/647076#M193616</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2020-05-12T12:54:43Z</dc:date>
    </item>
    <item>
      <title>Re: Retain and update across array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-and-update-across-array/m-p/647089#M193622</link>
      <description>&lt;P&gt;You can not maintain the current Event values in the &lt;STRONG&gt;same&lt;/STRONG&gt; variables that the Event values are being read in from the SET statement.&amp;nbsp; &amp;nbsp;When the data flow reaches the SET, any retained values will be over written.&amp;nbsp; So, instead, maintain state in a separate array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Searching for the slot containing a value to delete or a slot containing a blank to place a value is done with iteration in a simple loop.&amp;nbsp; Deletion is performed by shifting,&amp;nbsp;leftward&amp;nbsp; by 1 position, all values to the right.&amp;nbsp; Creation is limited to array size of state so that no index errors occur.&amp;nbsp; The code does not log messages what might be improper operations -- i.e. no space to create a value, or a delete value was not found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  by id;

  array event event1-event4;

  length state1-state4 $4;
  array state state1-state4;
  retain state:;

  if first.id then call missing(of state:);

  do index=1 to dim(event);
    operation = substr(event(index),1,1);
    value     = substr(event(index),3);

    if operation = 'D' then
      do searchIndex = 1 to dim(state);
        if state(searchIndex) = value then
          do moveIndex = searchIndex to dim(state)-1;
            state(moveIndex) = state(moveIndex+1);
            state(moveIndex+1) = ' ';
          end;
      end;
    else 
    if operation = 'C' then 
      do searchIndex = 1 to dim(state);
        if not missing(state(searchIndex)) then continue;
        state(searchIndex) = value;
        leave;
      end;
    
  end;

  keep id event: state:;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 13:37:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-and-update-across-array/m-p/647089#M193622</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-05-12T13:37:29Z</dc:date>
    </item>
    <item>
      <title>Re: Retain and update across array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-and-update-across-array/m-p/647133#M193639</link>
      <description>&lt;P&gt;Thank you very much!&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 15:33:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-and-update-across-array/m-p/647133#M193639</guid>
      <dc:creator>victoras1893</dc:creator>
      <dc:date>2020-05-12T15:33:54Z</dc:date>
    </item>
  </channel>
</rss>

