<?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: Conditional data trimming in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525517#M143003</link>
    <description>&lt;P&gt;Can be done most simply with an array :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data trimmed;
array cond{99} $23 _temporary_;
do count = 1 by 1 until(last.STRUCTURE_NUMBER_008);
    set sasforum.psc_super_count02; by STRUCTURE_NUMBER_008;
    cond{count} = SUPERSTRUCTURE_COND_059;
    end;

if count ge 10 then do;
    do cb = 4 to 1 by -1;
        if cond{cb} ne cond{5} then leave;
        end;
    begin = cb + 1;
    do ce = count-3 to count;
        if cond{ce} ne cond{count-4} then leave;
        end;
    end = ce - 1;
    end;
else do; /* Don't touch if begin and end sequence overlap */
    begin = 1;
    end = count;
    end;

do count = 1 by 1 until(last.STRUCTURE_NUMBER_008);
    set sasforum.psc_super_count02; by STRUCTURE_NUMBER_008;
    if count &amp;gt;= begin and count &amp;lt;= end then output;
    end;

drop cb ce count begin end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I didn't do any trimming for sequences shorter than 10 because your rules don't cover such cases.&lt;/P&gt;</description>
    <pubDate>Tue, 08 Jan 2019 18:39:51 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2019-01-08T18:39:51Z</dc:date>
    <item>
      <title>Conditional data trimming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525329#M142940</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a dataset of records for bridges (attached), each bridge has its own structure number given as &lt;STRONG&gt;STRUCTURE_NUMBER_008,&amp;nbsp;&lt;/STRONG&gt;and the dataset is ordered based on &lt;STRONG&gt;STRUCTURE_NUMBER_008&lt;/STRONG&gt; and &lt;STRONG&gt;Inspection_year&lt;/STRONG&gt;. The frequency each bridge is recorded is different as seen in the last column variable &lt;STRONG&gt;Freq.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The variable &lt;STRONG&gt;SUPERSTRUCTURE_COND_059&lt;/STRONG&gt; can take rating number of &lt;STRONG&gt;4, 5, 6, 7, and 8&lt;/STRONG&gt;. I would like to trim the dataset for each bridge based on the following condition.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the first 5 rating or last five rating for each bridge for variable &lt;STRONG&gt;SUPERSTRUCTURE_COND_059&lt;/STRONG&gt; is identical (e.g., &lt;STRONG&gt;4 4 4 4 4&lt;/STRONG&gt; or &lt;STRONG&gt;6 6 6 6 6&lt;/STRONG&gt;, ...) I want to retain all the observations with the middle observations. But if the first five rating for each bridge for variable&amp;nbsp;&lt;STRONG&gt;SUPERSTRUCTURE_COND_059 &lt;/STRONG&gt;is different (e.g., &lt;STRONG&gt;4 4 4 4 5&lt;/STRONG&gt; or &lt;STRONG&gt;7 7 6 6 6&lt;/STRONG&gt; or &lt;STRONG&gt;8 7 7 7 7&lt;/STRONG&gt;) I would like to delete the observations encountered first. For the example above I want to delete &lt;STRONG&gt;4 4 4 4&lt;/STRONG&gt; or &lt;STRONG&gt;7 7&lt;/STRONG&gt; or &lt;STRONG&gt;8&lt;/STRONG&gt; and retain the remaining observation with the middle observations for the bridge. if the the last five observations for a bridge is (e.g., &lt;STRONG&gt;4 4 4 4 5&lt;/STRONG&gt; or &lt;STRONG&gt;7 7 6 6 6&lt;/STRONG&gt; or &lt;STRONG&gt;8 7 7 7 7&lt;/STRONG&gt;), I want to delete observations that is changed, which in this case I would delete &lt;STRONG&gt;5&lt;/STRONG&gt;, &lt;STRONG&gt;6 6 6&lt;/STRONG&gt;, or &lt;STRONG&gt;7 7 7 7&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would be grateful I anyone could help me on this. The data set is attached.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 02:39:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525329#M142940</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-01-09T02:39:34Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional data trimming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525350#M142954</link>
      <description>&lt;P&gt;I understand you want to remove the first batch of identical-condition records if that string of records is less than 5.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you also want to remove the LAST batch of identical-condition records if the string is also less than 5?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jan 2019 08:36:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525350#M142954</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-01-08T08:36:18Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional data trimming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525454#M142977</link>
      <description>&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;I don't want to delete the first five and last five identical observations for a bridge. But, I want to delete observations that is changed within first five and last five observations. For example if the first five or last five observation for a bridge is ( 7 7 7 7 7) I will retain all of them. But if the first five observation for a bridge is (8 8 8 7 7), I want to delete the first three observations which is (8 8 8). Similarly, if the last five observation is (6 6 6 6 5), I want to delete the last observation which in this case is 5.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 02:40:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525454#M142977</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-01-09T02:40:56Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional data trimming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525517#M143003</link>
      <description>&lt;P&gt;Can be done most simply with an array :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data trimmed;
array cond{99} $23 _temporary_;
do count = 1 by 1 until(last.STRUCTURE_NUMBER_008);
    set sasforum.psc_super_count02; by STRUCTURE_NUMBER_008;
    cond{count} = SUPERSTRUCTURE_COND_059;
    end;

if count ge 10 then do;
    do cb = 4 to 1 by -1;
        if cond{cb} ne cond{5} then leave;
        end;
    begin = cb + 1;
    do ce = count-3 to count;
        if cond{ce} ne cond{count-4} then leave;
        end;
    end = ce - 1;
    end;
else do; /* Don't touch if begin and end sequence overlap */
    begin = 1;
    end = count;
    end;

do count = 1 by 1 until(last.STRUCTURE_NUMBER_008);
    set sasforum.psc_super_count02; by STRUCTURE_NUMBER_008;
    if count &amp;gt;= begin and count &amp;lt;= end then output;
    end;

drop cb ce count begin end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I didn't do any trimming for sequences shorter than 10 because your rules don't cover such cases.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jan 2019 18:39:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525517#M143003</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-01-08T18:39:51Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional data trimming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525564#M143015</link>
      <description>&lt;P&gt;I'd suggest reading the records for each bridge counting the size of each successive condition in variable _NCOND.&amp;nbsp; If the first _NCOND&amp;nbsp;is less than 5 then set a _keepstart variable to that number+1&amp;nbsp; (i.e. if the start is&amp;nbsp;&amp;nbsp; 1 1 2 2 2, then the first _NCOND is 2, and therefore _KEEPSTART=3&amp;nbsp;&amp;nbsp;&amp;nbsp; --- for record 3).&amp;nbsp;&amp;nbsp; Use a similar process for the last condition group.&amp;nbsp; If its size is less than 5 then set _KEEPEND to total count (_NTOTAL) minus the group size (_NCOND).&amp;nbsp; Otherwise _KEEPEND is set to _NTOTAL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then reread the records for the same bridge, keeping only those record numbers between _KEEPSTART and _KEEPEND.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  _ntotal=0;
  _keepstart=1;
  do until (last.STRUCTURE_NUMBER_008);
    do _ncond=1 by 1 until (last.SUPERSTRUCTURE_COND_059);
      set psc_super_count02 ;
      by STRUCTURE_NUMBER_008 SUPERSTRUCTURE_COND_059 notsorted;
    end;

    if _ntotal=0 and _ncond&amp;lt;5 then _keepstart=_ncond+1;
    _ntotal=_ntotal+_ncond;
  end;

  if _ncond&amp;lt;5 then _keepend=_ntotal-_ncond;
  else _keepend=_ntotal;

  do _n=1 to _ntotal;
    set psc_super_count02 ;
    if _keepstart &amp;lt;= _n &amp;lt;= _keepend then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Jan 2019 20:29:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525564#M143015</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-01-08T20:29:50Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional data trimming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525629#M143029</link>
      <description>&lt;P&gt;Thank you for your help. I run your code and the result was different from what I described in my first post. I would be happy if you could read my first post to revise your code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 02:33:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525629#M143029</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-01-09T02:33:27Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional data trimming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525630#M143030</link>
      <description>&lt;P&gt;Thank you for you help. I run your code and the result was different from what I described in my first post. Please read my first post and let me know if there is need for clarification.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 02:35:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525630#M143030</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-01-09T02:35:53Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional data trimming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525637#M143035</link>
      <description>&lt;P&gt;I think this would be more productive if you give an&amp;nbsp;example of what you expected versus what you actually got from my program.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 04:00:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525637#M143035</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-01-09T04:00:27Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional data trimming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525645#M143040</link>
      <description>&lt;P&gt;Can you point a structure number where the trimmed sequence is not as expected, and describe in what what way it differs from your expectation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS. Your rules are ambiguous for sequences of less than 9 years: For example, what would be the trimmed version of the sequence 4 4 4 5 4 5 5 5 ?&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 06:30:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525645#M143040</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-01-09T06:30:37Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional data trimming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525791#M143081</link>
      <description>&lt;P&gt;Thank you for your time. I found out the problem was with bridge observations less than 10. When I deleted bridges with less than 10 observations your code worked.&lt;/P&gt;&lt;P&gt;Also, When I only replaced 10 in your code with 5 as follows, the code worked with observations less than 10 too. Does it affect my trimming in the wrong way if I do this?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if count ge 5 then do;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you for your help.&lt;/P&gt;&lt;P&gt;Could you please give comment about the your code that I could learn from it, specially the array part and if-else part.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks alot! I really appreciate it.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 17:42:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525791#M143081</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-01-09T17:42:45Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional data trimming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525828#M143094</link>
      <description>&lt;P&gt;Here is a commented version of my code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data trimmed;

/* Read the COND sequence for a given structure into array cond */
array cond{99} $23 _temporary_;
do count = 1 by 1 until(last.STRUCTURE_NUMBER_008);
    set sasforum.psc_super_count02; by STRUCTURE_NUMBER_008;
    cond{count} = SUPERSTRUCTURE_COND_059;
    end;

/* Check how many COND values need to be trimmed at the 
   beginning and end of the sequence */
if count ge 5 then do;
    /* Scan backwards from 5 to trim the beginning */
    do cb = 4 to 1 by -1;
        if cond{cb} ne cond{5} then leave;
        end;
    begin = cb + 1;
    /* Scan the last 5 entries to trim the end */
    do ce = count-3 to count;
        if cond{ce} ne cond{count-4} then leave;
        end;
    end = ce - 1;
    end;
else do; /* Don't trim if sequence isn't long enough */
    begin = 1;
    end = count;
    end;

/* Read the data from the same structure again, copy the untrimmed 
   entries to the output dataset */
do count = 1 by 1 until(last.STRUCTURE_NUMBER_008);
    set sasforum.psc_super_count02; by STRUCTURE_NUMBER_008;
    if count &amp;gt;= begin and count &amp;lt;= end then output;
    end;

drop cb ce count begin end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;hth&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 19:25:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-data-trimming/m-p/525828#M143094</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-01-09T19:25:54Z</dc:date>
    </item>
  </channel>
</rss>

