<?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: Calculating the difference between two observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-the-difference-between-two-observation/m-p/398350#M278386</link>
    <description>&lt;P&gt;Your code is sound. Just a small modif is required:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
set test4 nobs=nobs;
do _i = _n_ to nobs until(other_date &amp;gt; date_l_);
    set test4(
        rename=(    date_l_= other_date 
                    price = other_price 
                    interval = other_time)
        keep=date_l_ price interval ) 
        point=_i;
    if other_time &amp;gt; interval and other_date = date_l_ then do;
        new_price = other_price;
        leave;
        end;
    end;
drop other_: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 24 Sep 2017 03:54:32 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2017-09-24T03:54:32Z</dc:date>
    <item>
      <title>Calculating the difference between two observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-the-difference-between-two-observation/m-p/398325#M278383</link>
      <description>&lt;P&gt;I want to calculate that Price at time&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;t&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;minus Price at time&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;t+1&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;for security&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;i&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;at day&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;k&lt;/EM&gt;. Particularly,&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;t+1&amp;nbsp;&lt;/EM&gt;is defined as the price at least 5 min after the price at time&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;t&lt;/EM&gt;. Hence, I added an variable to regulate the interval and used following code to create a row of Price at time&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;t+1&lt;/EM&gt;. Here is the sample of input data.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;    data test4;
       length _ric$ 25 type$ 5 interval$ 15 time_l_$ 25 ;
       input _ric$ date_l_ time_l_  type$ price interval$;
       datalines;
      AXPA031407800.U   20131212    9:52:56.537 Trade   5.85    09:50:00
    AXPA031407800.U 20131212    9:52:56.537 Trade   5.85    09:50:00
    AXPA031407800.U 20131212    9:53:13.586 Trade   5.8 09:50:00
    AXPA031407800.U 20131212    9:53:13.586 Trade   5.8 09:50:00
    AXPA031407800.U 20131212    9:53:13.607 Trade   5.8 09:50:00
    AXPA031407800.U 20131212    9:53:13.607 Trade   5.8 09:50:00
    AXPA031407800.U 20131212    9:53:34.990 Trade   5.8 09:50:00
    AXPA031407800.U 20131212    9:55:12.990 Trade   5.7 09:55:00
    AXPA031407800.U 20131212    9:55:12.990 Trade   5.7 09:55:00
    AXPA031407800.U 20131212    9:55:13.002 Trade   5.7 09:55:00
    AXPA031407800.U 20131212    9:55:13.002 Trade   5.7 09:55:00
    AXPA031407800.U 20131212    9:55:13.002 Trade   5.7 09:55:00
    AXPA031407800.U 20131212    9:55:13.011 Trade   5.7 09:55:00
    AXPA031407900.U 20131205    9:37:58.420 Trade   6.25    09:35:00
    AXPA031407900.U 20131205    9:39:04.996 Trade   6.25    09:35:00
    AXPA031407900.U 20131205    9:39:04.996 Trade   6.25    09:35:00
    AXPA031407900.U 20131205    9:39:04.996 Trade   6.25    09:35:00
    AXPA031407900.U 20131205    9:39:04.996 Trade   6.25    09:35:00
    AXPA031407900.U 20131205    9:39:04.996 Trade   6.25    09:35:00
;

data test1;
  set test nobs=nobs;
  do _i = _n_ to nobs until (new_date ne date_l_ or new_time &amp;gt; interval);
    set test (rename=(date_l_=new_date price=new_price interval=new_time)) point=_i;
  end;
  if (date_l_ ne new_date) or (_i &amp;gt; nobs) then call missing(new_price);  
  run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The output data is shown as following. However, the _RIC (security name), date_l_, and time_l_ are changed. As shown in output data, for example, AXPA031407800.U has 13 observations in input data but 7 observations in output data.&amp;nbsp;Does anyone know how to solve this problem, or a better way to calculate the difference between P at time &lt;EM&gt;t&lt;/EM&gt; and &lt;EM&gt;t+1&lt;/EM&gt;?&amp;nbsp;Thanks&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;_ric    type    interval    time_l_ date_l_ price   new_date    new_time    new_price
AXPA031407800.U Trade   09:50:00    9:55:12.990 20131212    5.85    20131212    09:55:00    5.7
AXPA031407800.U Trade   09:50:00    9:55:12.990 20131212    5.85    20131212    09:55:00    5.7
AXPA031407800.U Trade   09:50:00    9:55:12.990 20131212    5.8 20131212    09:55:00    5.7
AXPA031407800.U Trade   09:50:00    9:55:12.990 20131212    5.8 20131212    09:55:00    5.7
AXPA031407800.U Trade   09:50:00    9:55:12.990 20131212    5.8 20131212    09:55:00    5.7
AXPA031407800.U Trade   09:50:00    9:55:12.990 20131212    5.8 20131212    09:55:00    5.7
AXPA031407800.U Trade   09:50:00    9:55:12.990 20131212    5.8 20131212    09:55:00    5.7
AXPA031407900.U Trade   09:55:00    9:37:58.420 20131212    5.7 20131205    09:35:00    
AXPA031407900.U Trade   09:55:00    9:37:58.420 20131212    5.7 20131205    09:35:00    
AXPA031407900.U Trade   09:55:00    9:37:58.420 20131212    5.7 20131205    09:35:00    
AXPA031407900.U Trade   09:55:00    9:37:58.420 20131212    5.7 20131205    09:35:00    
AXPA031407900.U Trade   09:55:00    9:37:58.420 20131212    5.7 20131205    09:35:00    
AXPA031407900.U Trade   09:55:00    9:37:58.420 20131212    5.7 20131205    09:35:00    
AXPA031407900.U Trade   09:35:00    9:39:04.996 20131205    6.25    20131205    09:35:00    
AXPA031407900.U Trade   09:35:00    9:39:04.996 20131205    6.25    20131205    09:35:00    
AXPA031407900.U Trade   09:35:00    9:39:04.996 20131205    6.25    20131205    09:35:00    
AXPA031407900.U Trade   09:35:00    9:39:04.996 20131205    6.25    20131205    09:35:00    
AXPA031407900.U Trade   09:35:00    9:39:04.996 20131205    6.25    20131205    09:35:00    
AXPA031407900.U Trade   09:35:00    9:39:04.996 20131205    6.25    20131205    09:35:00 &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Sep 2017 23:19:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-the-difference-between-two-observation/m-p/398325#M278383</guid>
      <dc:creator>Neal3321</dc:creator>
      <dc:date>2017-09-23T23:19:37Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the difference between two observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-the-difference-between-two-observation/m-p/398332#M278384</link>
      <description>&lt;P&gt;Do you have a license for SAS ETS? If so, look at PROC EXPAND.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Depending on the size of your data, reversing that calculation is super easy, ie sort descending and then use the LAG or DIF function but they're not optimized.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There's also a trick of merging the data with itself but starting one observation later so they line up and you can do direct subtraction.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    merge have1 have1 (firstobs=2);
    by ID;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/167162"&gt;@Neal3321&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I want to calculate that Price at time&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;t&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;minus Price at time&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;t+1&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;for security&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;i&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;at day&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;k&lt;/EM&gt;. Particularly,&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;t+1&amp;nbsp;&lt;/EM&gt;is defined as the price at least 5 min after the price at time&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;t&lt;/EM&gt;. Hence, I added an variable to regulate the interval and used following code to create a row of Price at time&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;t+1&lt;/EM&gt;. Here is the sample of input data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;    data test4;
       length _ric$ 25 type$ 5 interval$ 15 time_l_$ 25 ;
       input _ric$ date_l_ time_l_  type$ price interval$;
       datalines;
      AXPA031407800.U   20131212    9:52:56.537 Trade   5.85    09:50:00
    AXPA031407800.U 20131212    9:52:56.537 Trade   5.85    09:50:00
    AXPA031407800.U 20131212    9:53:13.586 Trade   5.8 09:50:00
    AXPA031407800.U 20131212    9:53:13.586 Trade   5.8 09:50:00
    AXPA031407800.U 20131212    9:53:13.607 Trade   5.8 09:50:00
    AXPA031407800.U 20131212    9:53:13.607 Trade   5.8 09:50:00
    AXPA031407800.U 20131212    9:53:34.990 Trade   5.8 09:50:00
    AXPA031407800.U 20131212    9:55:12.990 Trade   5.7 09:55:00
    AXPA031407800.U 20131212    9:55:12.990 Trade   5.7 09:55:00
    AXPA031407800.U 20131212    9:55:13.002 Trade   5.7 09:55:00
    AXPA031407800.U 20131212    9:55:13.002 Trade   5.7 09:55:00
    AXPA031407800.U 20131212    9:55:13.002 Trade   5.7 09:55:00
    AXPA031407800.U 20131212    9:55:13.011 Trade   5.7 09:55:00
    AXPA031407900.U 20131205    9:37:58.420 Trade   6.25    09:35:00
    AXPA031407900.U 20131205    9:39:04.996 Trade   6.25    09:35:00
    AXPA031407900.U 20131205    9:39:04.996 Trade   6.25    09:35:00
    AXPA031407900.U 20131205    9:39:04.996 Trade   6.25    09:35:00
    AXPA031407900.U 20131205    9:39:04.996 Trade   6.25    09:35:00
    AXPA031407900.U 20131205    9:39:04.996 Trade   6.25    09:35:00
;

data test1;
  set test nobs=nobs;
  do _i = _n_ to nobs until (new_date ne date_l_ or new_time &amp;gt; interval);
    set test (rename=(date_l_=new_date price=new_price interval=new_time)) point=_i;
  end;
  if (date_l_ ne new_date) or (_i &amp;gt; nobs) then call missing(new_price);  
  run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output data is shown as following. However, the _RIC (security name), date_l_, and time_l_ are changed. As shown in output data, for example, AXPA031407800.U has 13 observations in input data but 7 observations in output data.&amp;nbsp;Does anyone know how to solve this problem, or a better way to calculate the difference between P at time &lt;EM&gt;t&lt;/EM&gt; and &lt;EM&gt;t+1&lt;/EM&gt;?&amp;nbsp;Thanks&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;_ric    type    interval    time_l_ date_l_ price   new_date    new_time    new_price
AXPA031407800.U Trade   09:50:00    9:55:12.990 20131212    5.85    20131212    09:55:00    5.7
AXPA031407800.U Trade   09:50:00    9:55:12.990 20131212    5.85    20131212    09:55:00    5.7
AXPA031407800.U Trade   09:50:00    9:55:12.990 20131212    5.8 20131212    09:55:00    5.7
AXPA031407800.U Trade   09:50:00    9:55:12.990 20131212    5.8 20131212    09:55:00    5.7
AXPA031407800.U Trade   09:50:00    9:55:12.990 20131212    5.8 20131212    09:55:00    5.7
AXPA031407800.U Trade   09:50:00    9:55:12.990 20131212    5.8 20131212    09:55:00    5.7
AXPA031407800.U Trade   09:50:00    9:55:12.990 20131212    5.8 20131212    09:55:00    5.7
AXPA031407900.U Trade   09:55:00    9:37:58.420 20131212    5.7 20131205    09:35:00    
AXPA031407900.U Trade   09:55:00    9:37:58.420 20131212    5.7 20131205    09:35:00    
AXPA031407900.U Trade   09:55:00    9:37:58.420 20131212    5.7 20131205    09:35:00    
AXPA031407900.U Trade   09:55:00    9:37:58.420 20131212    5.7 20131205    09:35:00    
AXPA031407900.U Trade   09:55:00    9:37:58.420 20131212    5.7 20131205    09:35:00    
AXPA031407900.U Trade   09:55:00    9:37:58.420 20131212    5.7 20131205    09:35:00    
AXPA031407900.U Trade   09:35:00    9:39:04.996 20131205    6.25    20131205    09:35:00    
AXPA031407900.U Trade   09:35:00    9:39:04.996 20131205    6.25    20131205    09:35:00    
AXPA031407900.U Trade   09:35:00    9:39:04.996 20131205    6.25    20131205    09:35:00    
AXPA031407900.U Trade   09:35:00    9:39:04.996 20131205    6.25    20131205    09:35:00    
AXPA031407900.U Trade   09:35:00    9:39:04.996 20131205    6.25    20131205    09:35:00    
AXPA031407900.U Trade   09:35:00    9:39:04.996 20131205    6.25    20131205    09:35:00 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 01:07:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-the-difference-between-two-observation/m-p/398332#M278384</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-09-24T01:07:00Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the difference between two observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-the-difference-between-two-observation/m-p/398350#M278386</link>
      <description>&lt;P&gt;Your code is sound. Just a small modif is required:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
set test4 nobs=nobs;
do _i = _n_ to nobs until(other_date &amp;gt; date_l_);
    set test4(
        rename=(    date_l_= other_date 
                    price = other_price 
                    interval = other_time)
        keep=date_l_ price interval ) 
        point=_i;
    if other_time &amp;gt; interval and other_date = date_l_ then do;
        new_price = other_price;
        leave;
        end;
    end;
drop other_: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 Sep 2017 03:54:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-the-difference-between-two-observation/m-p/398350#M278386</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-09-24T03:54:32Z</dc:date>
    </item>
  </channel>
</rss>

