<?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: How to extract specific ranges of obs from a data set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-specific-ranges-of-obs-from-a-data-set/m-p/34096#M6701</link>
    <description>Here is one solution.  It requires two passes of the primary data set &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt; .  If this is too big of an inefficiency there is probably a HASH table solution.  try it and let us know.&lt;BR /&gt;
Art&lt;BR /&gt;
[pre]&lt;BR /&gt;
* build set of extreme values - you already have these;&lt;BR /&gt;
data select(keep=region subsidiary product stores);&lt;BR /&gt;
   set sashelp.shoes(where=(stores&amp;gt;30));&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
* First pass of data to determine obs numbers of the extreme values;&lt;BR /&gt;
data cnt(keep=region subsidiary product stores cnt&lt;BR /&gt;
         rename=(region=cregion subsidiary=csubsidiary product=cproduct stores=cstores));&lt;BR /&gt;
   merge sashelp.shoes(in=inshoes)&lt;BR /&gt;
         select(in=inselect);&lt;BR /&gt;
   by region subsidiary product;&lt;BR /&gt;
   cnt+1;&lt;BR /&gt;
   if inshoes and inselect;&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
* select leading and trailing obs;&lt;BR /&gt;
* retrieve +/- 3 obs;&lt;BR /&gt;
data surrounded(keep=region subsidiary product stores sales );&lt;BR /&gt;
   * array to hold used obs numbers - dimension depends on the total number of obs;&lt;BR /&gt;
   array used {100000] $1 _temporary_;&lt;BR /&gt;
   set cnt;&lt;BR /&gt;
   do point=(cnt-3) to (cnt+3);&lt;BR /&gt;
      if 1 le point le nobs then do;&lt;BR /&gt;
         set sashelp.shoes point=point nobs=nobs;&lt;BR /&gt;
         * PRODUCT is not included in the comparison so duplicate obs are possible without checking array;&lt;BR /&gt;
         if used{point}=' ' &amp;amp; region=cregion &amp;amp; subsidiary=csubsidiary then do;&lt;BR /&gt;
            output surrounded;&lt;BR /&gt;
            used{point} = 'x';&lt;BR /&gt;
         end;&lt;BR /&gt;
      end;&lt;BR /&gt;
   end;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]

Message was edited by: ArtC</description>
    <pubDate>Thu, 10 Jun 2010 20:51:02 GMT</pubDate>
    <dc:creator>ArtC</dc:creator>
    <dc:date>2010-06-10T20:51:02Z</dc:date>
    <item>
      <title>How to extract specific ranges of obs from a data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-specific-ranges-of-obs-from-a-data-set/m-p/34095#M6700</link>
      <description>I have a large sequential data set.  I have identified a small subset of extreme observations in the set.  What I want to do is extract the extreme observations and the 10 observation before and after each extreme observations.  If that is possible, I would also like to ensure that duplicates are removed as well.  And ideally, since I have by variables, I would love to be able to abide by the first.var and last.var parameters so as not to include an obs from a different by variable within the range.  Can anyone help?</description>
      <pubDate>Thu, 10 Jun 2010 18:01:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-specific-ranges-of-obs-from-a-data-set/m-p/34095#M6700</guid>
      <dc:creator>MattM</dc:creator>
      <dc:date>2010-06-10T18:01:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract specific ranges of obs from a data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-specific-ranges-of-obs-from-a-data-set/m-p/34096#M6701</link>
      <description>Here is one solution.  It requires two passes of the primary data set &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt; .  If this is too big of an inefficiency there is probably a HASH table solution.  try it and let us know.&lt;BR /&gt;
Art&lt;BR /&gt;
[pre]&lt;BR /&gt;
* build set of extreme values - you already have these;&lt;BR /&gt;
data select(keep=region subsidiary product stores);&lt;BR /&gt;
   set sashelp.shoes(where=(stores&amp;gt;30));&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
* First pass of data to determine obs numbers of the extreme values;&lt;BR /&gt;
data cnt(keep=region subsidiary product stores cnt&lt;BR /&gt;
         rename=(region=cregion subsidiary=csubsidiary product=cproduct stores=cstores));&lt;BR /&gt;
   merge sashelp.shoes(in=inshoes)&lt;BR /&gt;
         select(in=inselect);&lt;BR /&gt;
   by region subsidiary product;&lt;BR /&gt;
   cnt+1;&lt;BR /&gt;
   if inshoes and inselect;&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
* select leading and trailing obs;&lt;BR /&gt;
* retrieve +/- 3 obs;&lt;BR /&gt;
data surrounded(keep=region subsidiary product stores sales );&lt;BR /&gt;
   * array to hold used obs numbers - dimension depends on the total number of obs;&lt;BR /&gt;
   array used {100000] $1 _temporary_;&lt;BR /&gt;
   set cnt;&lt;BR /&gt;
   do point=(cnt-3) to (cnt+3);&lt;BR /&gt;
      if 1 le point le nobs then do;&lt;BR /&gt;
         set sashelp.shoes point=point nobs=nobs;&lt;BR /&gt;
         * PRODUCT is not included in the comparison so duplicate obs are possible without checking array;&lt;BR /&gt;
         if used{point}=' ' &amp;amp; region=cregion &amp;amp; subsidiary=csubsidiary then do;&lt;BR /&gt;
            output surrounded;&lt;BR /&gt;
            used{point} = 'x';&lt;BR /&gt;
         end;&lt;BR /&gt;
      end;&lt;BR /&gt;
   end;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]

Message was edited by: ArtC</description>
      <pubDate>Thu, 10 Jun 2010 20:51:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-specific-ranges-of-obs-from-a-data-set/m-p/34096#M6701</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-06-10T20:51:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract specific ranges of obs from a data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-specific-ranges-of-obs-from-a-data-set/m-p/34097#M6702</link>
      <description>Hi. Can you give some what your dataset look like with some dummy data,&lt;BR /&gt;
and what output you would like to be . giving a example  would help understand what your purpose.</description>
      <pubDate>Fri, 11 Jun 2010 08:29:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-specific-ranges-of-obs-from-a-data-set/m-p/34097#M6702</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2010-06-11T08:29:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract specific ranges of obs from a data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-specific-ranges-of-obs-from-a-data-set/m-p/34098#M6703</link>
      <description>Try this.  You will want to check for end of file etc to avoid warnings, but this works in the simple example I wrote.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Data new;&lt;BR /&gt;
set old;&lt;BR /&gt;
counter+1;&lt;BR /&gt;
chkvar = byvar;&lt;BR /&gt;
if (condition met) then do pv = counter -10 to counter + 10;&lt;BR /&gt;
set old point = pv;&lt;BR /&gt;
if byvar  = chkvar then output;&lt;BR /&gt;
end;&lt;BR /&gt;
run;</description>
      <pubDate>Fri, 11 Jun 2010 12:50:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-specific-ranges-of-obs-from-a-data-set/m-p/34098#M6703</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2010-06-11T12:50:46Z</dc:date>
    </item>
  </channel>
</rss>

