<?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: Cutting certain period of data by counting number of observations from a reference point in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577251#M163561</link>
    <description>&lt;P&gt;I would add that your hash based code is more robust to missing and duplicate dates in either dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers!&lt;/P&gt;</description>
    <pubDate>Sun, 28 Jul 2019 21:18:36 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2019-07-28T21:18:36Z</dc:date>
    <item>
      <title>Cutting certain period of data by counting number of observations from a reference point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577132#M163509</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The subject I wrote seem a bit unclear; however, what I want to ask is very simple.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'm trying to do is to cut a certain period of dataset with the same number of observations, around a certain reference point.&lt;/P&gt;&lt;P&gt;For example, the following is how my dataset looks:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class="  language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; work&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;item_output&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token statement"&gt;infile&lt;/SPAN&gt; datalines truncover&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; date: yymmddn&lt;SPAN class="token number"&gt;8&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; volume&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   datalines&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;20190101&amp;nbsp;30;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;20190103&amp;nbsp;40;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;20190104&amp;nbsp;25;&lt;BR /&gt;         ...&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;20190302&amp;nbsp;20;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;20190304&amp;nbsp;20;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;20190706&amp;nbsp;10; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to do is to take a reference date, say, 20190203, and extract the data containing of observations around that date, for example (-5 days) ~ (+5 days). And repeat this process for other reference dates as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But, as you can notice, the date variable is not always present. For example, there are 20190101 and 20190103 observations, but there isn't 20190102, and I want to count simply by the number of observations, not by the actual date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, for example, if I take +5 days data from 20190101, it would look something like this:&lt;/P&gt;&lt;P&gt;20190101 -&amp;gt; reference point&lt;/P&gt;&lt;P&gt;20190103&lt;/P&gt;&lt;P&gt;20190104&lt;/P&gt;&lt;P&gt;20190105&lt;/P&gt;&lt;P&gt;20190106&lt;/P&gt;&lt;P&gt;20190107&amp;nbsp; -&amp;gt; +5 days after the reference point&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, by saying '+5 days,' I'm actually meaning '+5 observations.'&lt;/P&gt;&lt;P&gt;And I want to extract that certain period of data as a separate dataset table in sas.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For now, since I don't know how to make sas do this job automatically,&lt;/P&gt;&lt;P&gt;I manually opened the original table, looked for the reference points, looked for the date that's 5 observations away from the reference point, copied that and extract that period of data manually by using data procedure.&lt;/P&gt;&lt;P&gt;However, it is too time-consuming and burdensome because there are more than just one or two reference periods.&lt;/P&gt;&lt;P&gt;So, I'd be great to know how to code this to work automatically (at least less manually than what I did), if there is a way to do so.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope what I wrote made sense.&lt;/P&gt;&lt;P&gt;It'd be great if anyone can help me out with this.&lt;/P&gt;</description>
      <pubDate>Sat, 27 Jul 2019 06:44:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577132#M163509</guid>
      <dc:creator>LzEr23</dc:creator>
      <dc:date>2019-07-27T06:44:03Z</dc:date>
    </item>
    <item>
      <title>Re: Cutting certain period of data by counting number of observations from a reference point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577138#M163510</link>
      <description>Let's begin by assuming you have a SAS data set holding a set of values for REFDATE.  Then you could use:&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set ref_point_list;&lt;BR /&gt;found=0;&lt;BR /&gt;do _n_=1 to ndates;&lt;BR /&gt;set item_output nobs=ndates point=_n_ ;&lt;BR /&gt;if date &amp;gt; refdate then found + 1;&lt;BR /&gt;if (1 &amp;lt;= found &amp;lt;= 5) then output;&lt;BR /&gt;else if found = 6 then delete;&lt;BR /&gt;end;&lt;BR /&gt;run;</description>
      <pubDate>Sat, 27 Jul 2019 08:56:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577138#M163510</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-07-27T08:56:41Z</dc:date>
    </item>
    <item>
      <title>Re: Cutting certain period of data by counting number of observations from a reference point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577182#M163533</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/232719"&gt;@LzEr23&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;If you mean to get meaningful assistance, please provide:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- a representative sample data set HAVE&lt;/P&gt;
&lt;P&gt;- a sample data set containing reference dates REF&lt;/P&gt;
&lt;P&gt;- a sample data set WANT illustrating the output you expect from processing HAVE and REF&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To anyone looking at your problem, it would mean a whole lot more than verbose explanations of your input and output, though a terse meaningful intelligible explanation is always welcome.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Jul 2019 18:20:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577182#M163533</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-27T18:20:10Z</dc:date>
    </item>
    <item>
      <title>Re: Cutting certain period of data by counting number of observations from a reference point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577247#M163557</link>
      <description>&lt;P&gt;Here is an efficient way to do this (for a window of + or - 1 obs):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date: yymmdd8. volume;
format date yymmdd10.;
datalines;     
20190101 30     
20190103 40     
20190104 25  
20190105 99  
20190301 99     
20190302 20     
20190304 20     
20190706 10
;

data ref;
format date yymmdd10.;
input date yymmdd10.;
datalines;
20190103
20190304
;

data pos;
merge have ref(in=ref); by date;
if ref then do;
    pos = _n_;
    output;
    end;
keep date pos;
run;

%let window=1;

data want;
set pos(rename=date=refDate);
do point = max(1, pos - &amp;amp;window) to min(nobs, pos + &amp;amp;window);
    set have point=point nobs=nobs;
    output;
    end;
drop pos;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 28 Jul 2019 18:44:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577247#M163557</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-07-28T18:44:55Z</dc:date>
    </item>
    <item>
      <title>Re: Cutting certain period of data by counting number of observations from a reference point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577248#M163558</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Neato! Yet since you've mentioned efficiency, why not eschew the writing and reading of POS altogether and do it all in a single step? E.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                    
  input date: yymmdd8. volume;                                 
  format date yymmdd10.;                                       
  cards ;                                                      
20190101 30                                                    
20190103 40                                                    
20190104 25                                                    
20190105 99                                                    
20190301 99                                                    
20190302 20                                                    
20190304 20                                                    
20190706 10                                                    
run ;                                                          
                                                               
data ref ;                                                     
  format date yymmdd10.;                                       
  input date yymmdd10.;                                        
  cards ;                                                      
20190103                                                       
20190304                                                       
run ;                                                          
                                                               
%let w = 1 ;                                                   
                                                               
data want ;                                                    
  merge have ref (in = ref) ;                                  
  by date ;                                                    
  if ref then do _n_ = max (1, _n_ - &amp;amp;w) to min (n, _n_ + &amp;amp;w) ;
    set have point = _n_ nobs = n ;                            
    output ;                                                   
  end ;                                                        
run ;                                                          
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above assumes, of course, that REF is sorted by DATE. If not:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;                                                              
  if _n_ = 1 then do ;                                                   
    dcl hash h (dataset: "ref") ;                                        
    h.definekey ("date") ;                                               
    h.definedone () ;                                                    
  end ;                                                                  
  set have ;                                                             
  if h.check() = 0 then do _n_ = max (1, _n_ - &amp;amp;w) to min (n, _n_ + &amp;amp;w) ;
    set have point = _n_ nobs = n ;                                      
    output ;                                                             
  end ;                                                                  
run ;                                                                    
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As a side note, I've noticed that your code works when the data set option RENAME with a single variable being renamed is coded &lt;EM&gt;without parentheses&lt;/EM&gt; as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;pos(rename=date=refDate) &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I didn't know SAS can swallow that; but now I do. Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jul 2019 19:33:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577248#M163558</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-28T19:33:34Z</dc:date>
    </item>
    <item>
      <title>Re: Cutting certain period of data by counting number of observations from a reference point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577251#M163561</link>
      <description>&lt;P&gt;I would add that your hash based code is more robust to missing and duplicate dates in either dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers!&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jul 2019 21:18:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577251#M163561</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-07-28T21:18:36Z</dc:date>
    </item>
    <item>
      <title>Re: Cutting certain period of data by counting number of observations from a reference point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577272#M163576</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Agree. In fact, any lookup table approach (hash table, key-indexed table, bitmap, binary-searched array, etc.) vs the sorted serial match approach (aka merge) has the advantages you've pointed out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Gaudeamus igitur!&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jul 2019 23:45:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/577272#M163576</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-28T23:45:01Z</dc:date>
    </item>
    <item>
      <title>Re: Cutting certain period of data by counting number of observations from a reference point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/613507#M179179</link>
      <description>Sorry for the delay. I forgot I posted this here. I've tried your way, just to see if it works, and it worked perfectly. Thank you for your help! It really was helpful.</description>
      <pubDate>Mon, 23 Dec 2019 11:09:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cutting-certain-period-of-data-by-counting-number-of/m-p/613507#M179179</guid>
      <dc:creator>LzEr23</dc:creator>
      <dc:date>2019-12-23T11:09:40Z</dc:date>
    </item>
  </channel>
</rss>

