<?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: Variation between dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19130#M2946</link>
    <description>I probably should have used&lt;BR /&gt;
[pre]&lt;BR /&gt;
... then dummy=lag(valor);&lt;BR /&gt;
[/pre]</description>
    <pubDate>Tue, 20 May 2008 16:58:56 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2008-05-20T16:58:56Z</dc:date>
    <item>
      <title>Variation between dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19126#M2942</link>
      <description>Hi there,&lt;BR /&gt;
&lt;BR /&gt;
I need to calculate the variation in "VALOR" between dates (1 day&lt;BR /&gt;
diference). How can I do that?&lt;BR /&gt;
&lt;BR /&gt;
Thanks.&lt;BR /&gt;
&lt;BR /&gt;
CODFONTE        CODTIT  ULTDATA MOEDA   BOLSA   CODITEM VALOR   DTALT&lt;BR /&gt;
1       100019900833    20080417        EUR     LUXEMBOURG      Preco 15H30     96.224  17-04-2008&lt;BR /&gt;
1       100019900833    20080418        EUR     LUXEMBOURG      Preco 15H30     96.249  18-04-2008&lt;BR /&gt;
1       100019900833    20080421        EUR     LUXEMBOURG      Preco 15H30     95.764  21-04-2008&lt;BR /&gt;
1       100019900833    20080422        EUR     LUXEMBOURG      Preco 15H30     96.256  22-04-2008&lt;BR /&gt;
1       100019900833    20080423        EUR     LUXEMBOURG      Preco 15H30     96.367  23-04-2008&lt;BR /&gt;
1       100019900833    20080424        EUR     LUXEMBOURG      Preco 15H30     96.366  24-04-2008&lt;BR /&gt;
1       100019900833    20080425        EUR     LUXEMBOURG      Preco 15H30     95.562  25-04-2008&lt;BR /&gt;
1       100019900833    20080428        EUR     LUXEMBOURG      Preco 15H30     96.416  28-04-2008&lt;BR /&gt;
1       100019900833    20080429        EUR     LUXEMBOURG      Preco 15H30     96.843  29-04-2008&lt;BR /&gt;
1       100019900833    20080430        EUR     LUXEMBOURG      Preco 15H30     96.848  01-05-2008&lt;BR /&gt;
1       100019900833    20080502        EUR     LUXEMBOURG      Preco 15H30     96.851  02-05-2008&lt;BR /&gt;
1       100019900833    20080505        EUR     LUXEMBOURG      Preco 15H30     96.855  05-05-2008&lt;BR /&gt;
1       100019900833    20080506        EUR     LUXEMBOURG      Preco 15H30     96.857  06-05-2008&lt;BR /&gt;
1       100019900833    20080507        EUR     LUXEMBOURG      Preco 15H30     96.887  07-05-2008&lt;BR /&gt;
1       100019900833    20080508        EUR     LUXEMBOURG      Preco 15H30     96.869  08-05-2008&lt;BR /&gt;
1       100056520533    20080104        EUR     EURONEXT-LISBON Preco 15H30     99.625&lt;BR /&gt;
04-01-2008&lt;BR /&gt;
1       100056520533    20080107        EUR     EURONEXT-LISBON Preco 15H30     99.616&lt;BR /&gt;
08-01-2008&lt;BR /&gt;
1       100056520533    20080109        EUR     EURONEXT-LISBON Preco 15H30     99.633&lt;BR /&gt;
09-01-2008&lt;BR /&gt;
1       100056520533    20080110        EUR     EURONEXT-LISBON Preco 15H30     99.64&lt;BR /&gt;
11-01-2008&lt;BR /&gt;
1       100056520533    20080111        EUR     EURONEXT-LISBON Preco 15H30     99.634&lt;BR /&gt;
14-01-2008&lt;BR /&gt;
1       100056520533    20080115        EUR     EURONEXT-LISBON Preco 15H30     99.676&lt;BR /&gt;
15-01-2008&lt;BR /&gt;
1       100056520533    20080116        EUR     EURONEXT-LISBON Preco 15H30     99.661&lt;BR /&gt;
16-01-2008&lt;BR /&gt;
1       100056520533    20080117        EUR     EURONEXT-LISBON Preco 15H30     99.671&lt;BR /&gt;
17-01-2008&lt;BR /&gt;
1       100056520533    20080118        EUR     EURONEXT-LISBON Preco 15H30     99.702&lt;BR /&gt;
18-01-2008&lt;BR /&gt;
1       100056520533    20080121        EUR     EURONEXT-LISBON Preco 15H30     99.708&lt;BR /&gt;
21-01-2008&lt;BR /&gt;
1       100056520533    20080122        EUR     EURONEXT-LISBON Preco 15H30     99.719&lt;BR /&gt;
22-01-2008&lt;BR /&gt;
1       100056520533    20080123        EUR     EURONEXT-LISBON Preco 15H30     99.701&lt;BR /&gt;
24-01-2008&lt;BR /&gt;
1       100056520533    20080125        EUR     EURONEXT-LISBON Preco 15H30     99.691&lt;BR /&gt;
25-01-2008 &lt;BR /&gt;
&lt;BR /&gt;
The lag funtion don't work because I have more than one COD_TIT and the data is not sorted by date.&lt;BR /&gt;
&lt;BR /&gt;
data testt;&lt;BR /&gt;
    set values;&lt;BR /&gt;
    diff=valor-lag(valor);&lt;BR /&gt;
run;</description>
      <pubDate>Tue, 20 May 2008 14:48:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19126#M2942</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-05-20T14:48:28Z</dc:date>
    </item>
    <item>
      <title>Re: Variation between dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19127#M2943</link>
      <description>LAG can work.  Assuming DTALT is a numeric value with a european style data format.&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sort data=indata;&lt;BR /&gt;
  by codfonte codtit dtalt;&lt;BR /&gt;
run;&lt;BR /&gt;
 &lt;BR /&gt;
data outdata;&lt;BR /&gt;
  set indata;&lt;BR /&gt;
  by codfonte codtit;&lt;BR /&gt;
 &lt;BR /&gt;
  if first.codtit then lag(valor);&lt;BR /&gt;
 &lt;BR /&gt;
  diff = valor - lag(valor);&lt;BR /&gt;
&lt;BR /&gt;
  if not first.codtit then output;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Does this help?&lt;BR /&gt;
&lt;BR /&gt;
Lag does not mean "use the previous value", like you would hope it does.&lt;BR /&gt;
The "LAG" documentation is a little confusing and easily mis-interpreted.  I had to spend a couple hours/days experimenting until I fully understood what was happening.&lt;BR /&gt;
&lt;BR /&gt;
LAG simply represents a FIF0 queue of some depth:  lag = lag1 = depth of 2; lag10 = depth of 11 (could be 1 and 10 for the actual implementation, but this is easier to explain).  When the function is called, it loads the current value at the variable into a queue for that variable and then retrieves the last value in the queue and returns that value.  So, the first time LAG(valor) is used/called, it loads the value of VALOR into the queue and then returns a missing value.  The next time LAG(valor) is executed, a new value is put into the queue (now holding 2 actual meaningful values) and then retrieves the end/last/previous value and returns that value for use.  This is why it is easy to trip over the use of LAG within conditional statements, because it does not necessarily have the value from the previous observation if the conditional was different for that observation.  This is also why you have to be careful with the number of times LAG is used with a particular variable in a datastep.  If it is desired that the lag variable explicitly mean "previous observation value" then use:&lt;BR /&gt;
[pre]&lt;BR /&gt;
prior_value = lag(value);&lt;BR /&gt;
[/pre]&lt;BR /&gt;
in the code to hold that prior value for use&lt;BR /&gt;
[pre]&lt;BR /&gt;
diff1 = value - lag(value);&lt;BR /&gt;
value3 = value2*lag(value);&lt;BR /&gt;
[/pre]&lt;BR /&gt;
is very different from&lt;BR /&gt;
[pre]&lt;BR /&gt;
prior = lag(value);&lt;BR /&gt;
diff1 = value - prior;&lt;BR /&gt;
value3 = value2 * prior;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Tue, 20 May 2008 15:20:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19127#M2943</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-05-20T15:20:17Z</dc:date>
    </item>
    <item>
      <title>Re: Variation between dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19128#M2944</link>
      <description>Hello Chuck, thanks for your help.&lt;BR /&gt;
&lt;BR /&gt;
When I try to do like you say it gives the following error:&lt;BR /&gt;
&lt;BR /&gt;
463  proc sort data=mylib.TAcc_Obri;&lt;BR /&gt;
464    by codfonte codtit dtalt;&lt;BR /&gt;
465  run;&lt;BR /&gt;
&lt;BR /&gt;
NOTE: Input data set is already sorted, no sorting done.&lt;BR /&gt;
NOTE: PROCEDURE SORT used (Total process time):&lt;BR /&gt;
      real time           0.00 seconds&lt;BR /&gt;
      cpu time            0.00 seconds&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
466  data mylib.TAcc_Obriga;&lt;BR /&gt;
467    set mylib.TAcc_Obri;&lt;BR /&gt;
468    by codtit;&lt;BR /&gt;
469&lt;BR /&gt;
470    if first.codtit then lag(valor);&lt;BR /&gt;
                                      -&lt;BR /&gt;
                                      22&lt;BR /&gt;
                                      76&lt;BR /&gt;
ERROR: Undeclared array referenced: lag.&lt;BR /&gt;
ERROR 22-322: Syntax error, expecting one of the following: +, =.&lt;BR /&gt;
&lt;BR /&gt;
ERROR 76-322: Syntax error, statement will be ignored.&lt;BR /&gt;
&lt;BR /&gt;
471&lt;BR /&gt;
472    diff = valor - lag(valor);&lt;BR /&gt;
473&lt;BR /&gt;
474    if not first.codtit then output;&lt;BR /&gt;
475  run;&lt;BR /&gt;
&lt;BR /&gt;
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).&lt;BR /&gt;
      472:10   472:18&lt;BR /&gt;
NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;
WARNING: The data set MYLIB.TACC_OBRIGA may be incomplete.  When this step was stopped there were 0&lt;BR /&gt;
         observations and 13 variables.&lt;BR /&gt;
WARNING: Data set MYLIB.TACC_OBRIGA was not replaced because this step was stopped.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.00 seconds&lt;BR /&gt;
      cpu time            0.00 seconds</description>
      <pubDate>Tue, 20 May 2008 16:49:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19128#M2944</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-05-20T16:49:12Z</dc:date>
    </item>
    <item>
      <title>Re: Variation between dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19129#M2945</link>
      <description>Nanita&lt;BR /&gt;
you sorted by codfonte codtit dtalt, so in your data set you need the same by sequence in the data step&lt;BR /&gt;
&lt;BR /&gt;
set mylib.TAcc_Obri;&lt;BR /&gt;
  by codfonte codtit;&lt;BR /&gt;
&lt;BR /&gt;
 ......&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
-Darryl</description>
      <pubDate>Tue, 20 May 2008 16:51:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19129#M2945</guid>
      <dc:creator>darrylovia</dc:creator>
      <dc:date>2008-05-20T16:51:50Z</dc:date>
    </item>
    <item>
      <title>Re: Variation between dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19130#M2946</link>
      <description>I probably should have used&lt;BR /&gt;
[pre]&lt;BR /&gt;
... then dummy=lag(valor);&lt;BR /&gt;
[/pre]</description>
      <pubDate>Tue, 20 May 2008 16:58:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19130#M2946</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-05-20T16:58:56Z</dc:date>
    </item>
    <item>
      <title>Re: Variation between dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19131#M2947</link>
      <description>I've tried this code:&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=mylib.TAcc_Obri;&lt;BR /&gt;
  by codfonte codtit dtalt;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
   &lt;BR /&gt;
data mylib.TAcc_Obriga;&lt;BR /&gt;
  set mylib.TAcc_Obri;&lt;BR /&gt;
  by codfonte codtit dtalt;&lt;BR /&gt;
 &lt;BR /&gt;
  if first.codtit then diff=0; &lt;BR /&gt;
  if not first.codtit then diff = valor - lag(valor);&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
It seems to be working but I don't understand why in line 2 the value of diff is '.'.&lt;BR /&gt;
&lt;BR /&gt;
Where did I fail?&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
 Obs    CODTIT          VALOR             DTALT            ISIN                               diff&lt;BR /&gt;
&lt;BR /&gt;
                  1    100019900833    96.224        17APR2008    XS0195487912     0.000&lt;BR /&gt;
                  2    100019900833    96.249        18APR2008    XS0195487912      .&lt;BR /&gt;
                  3    100019900833    95.764        21APR2008    XS0195487912    -0.485&lt;BR /&gt;
                  4    100019900833    96.256        22APR2008    XS0195487912     0.492&lt;BR /&gt;
                  5    100019900833    96.367        23APR2008    XS0195487912     0.111&lt;BR /&gt;
                  6    100019900833    96.366        24APR2008    XS0195487912    -0.001&lt;BR /&gt;
                  7    100019900833    95.562        25APR2008    XS0195487912    -0.804&lt;BR /&gt;
                  8    100019900833    96.416        28APR2008    XS0195487912     0.854&lt;BR /&gt;
                  9    100019900833    96.843        29APR2008    XS0195487912     0.427&lt;BR /&gt;
                 10    100019900833    96.848        01MAY2008    XS0195487912     0.005&lt;BR /&gt;
                 11    100019900833    96.851        02MAY2008    XS0195487912     0.003&lt;BR /&gt;
                 12    100019900833    96.855        05MAY2008    XS0195487912     0.004&lt;BR /&gt;
                 13    100019900833    96.857        06MAY2008    XS0195487912     0.002&lt;BR /&gt;
                 14    100019900833    96.887        07MAY2008    XS0195487912     0.030&lt;BR /&gt;
                 15    100019900833    96.869        08MAY2008    XS0195487912    -0.018&lt;BR /&gt;
                 16    100056520533    99.625        04JAN2008    PTSEMCOE0006     0.000&lt;BR /&gt;
                 17    100056520533    99.616        08JAN2008    PTSEMCOE0006     2.747&lt;BR /&gt;
                 18    100056520533    99.633        09JAN2008    PTSEMCOE0006     0.017&lt;BR /&gt;
                 19    100056520533    99.64         11JAN2008    PTSEMCOE0006     0.007&lt;BR /&gt;
                 20    100056520533    99.634        14JAN2008    PTSEMCOE0006    -0.006&lt;BR /&gt;
                 21    100056520533    99.676        15JAN2008    PTSEMCOE0006     0.042&lt;BR /&gt;
                 22    100056520533    99.661        16JAN2008    PTSEMCOE0006    -0.015&lt;BR /&gt;
                 23    100056520533    99.671        17JAN2008    PTSEMCOE0006     0.010&lt;BR /&gt;
                 24    100056520533    99.702        18JAN2008    PTSEMCOE0006     0.031&lt;BR /&gt;
                 25    100056520533    99.708        21JAN2008    PTSEMCOE0006     0.006&lt;BR /&gt;
                 26    100056520533    99.719        22JAN2008    PTSEMCOE0006     0.011&lt;BR /&gt;
                 27    100056520533    99.701        24JAN2008    PTSEMCOE0006    -0.018&lt;BR /&gt;
                 28    100056520533    99.691        25JAN2008    PTSEMCOE0006    -0.010&lt;BR /&gt;
                 29    100056520533    99.692        28JAN2008    PTSEMCOE0006     0.001&lt;BR /&gt;
                 30    100056520533    99.692        29JAN2008    PTSEMCOE0006     0.000&lt;BR /&gt;
                 31    100056520533    99.686        30JAN2008    PTSEMCOE0006    -0.006&lt;BR /&gt;
                 32    100056520533    99.686        06FEB2008    PTSEMCOE0006     0.000&lt;BR /&gt;
                 33    100056520533    99.689        07FEB2008    PTSEMCOE0006     0.003&lt;BR /&gt;
                 34    100056520533    99.691        08FEB2008    PTSEMCOE0006     0.002&lt;BR /&gt;
                 35    100056520533    99.688        11FEB2008    PTSEMCOE0006    -0.003</description>
      <pubDate>Tue, 20 May 2008 17:20:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19131#M2947</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-05-20T17:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: Variation between dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19132#M2948</link>
      <description>First, You need to re-read Chuck's post on how the lag function works.  His explaination is right on target, especially with using lag with conditional logic. &lt;BR /&gt;
&lt;BR /&gt;
Second, The first time that the lag function is called is during the 2 observation, since this is the first time the lag function is called there is no value to fetch.  Recall from Chuck's post that lag is not the previous value, lag effectivilty places values in a queue.  So you need to think what value is in the queue at any given record.  &lt;BR /&gt;
&lt;BR /&gt;
Also see&lt;BR /&gt;
15 100019900833 96.869 08MAY2008 XS0195487912 -0.018&lt;BR /&gt;
16 100056520533 99.625 04JAN2008 PTSEMCOE0006 0.000&lt;BR /&gt;
17 100056520533 99.616 08JAN2008 PTSEMCOE0006 2.747&lt;BR /&gt;
&lt;BR /&gt;
Notice that the valor=99.616 for obs(17) and valor=99.625 for obs(16).  I assume that you want the diff to be 99.616-99.625=-0.009.  What is being calculated is  for obs(17) minus valor for obs(15) 99.616-96.869=2.747.  This is because the last value of valor in the lag queue is for obs(15).  Your code skips the lag function for the first.codit.  My guess is that you don't want to do that.&lt;BR /&gt;
&lt;BR /&gt;
I would do something like this.  The below example has the additional benefit of creating another column (prior_valor) that holds the previous example.  You can open up the table and see what values are being used in your calcuations.  Don't be afraid to make as many variables as you need during development, you can always cull them down once you are certain all is well.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data mylib.TAcc_Obriga;&lt;BR /&gt;
set mylib.TAcc_Obri;&lt;BR /&gt;
by codfonte codtit dtalt;&lt;BR /&gt;
&lt;BR /&gt;
prior_valor=lag(valor);&lt;BR /&gt;
&lt;BR /&gt;
if first.codtit then diff=0; &lt;BR /&gt;
if not first.codtit then diff = valor - prior_valor;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
-Darryl</description>
      <pubDate>Tue, 20 May 2008 17:46:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19132#M2948</guid>
      <dc:creator>darrylovia</dc:creator>
      <dc:date>2008-05-20T17:46:06Z</dc:date>
    </item>
    <item>
      <title>Re: Variation between dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19133#M2949</link>
      <description>Hi Darryl,&lt;BR /&gt;
&lt;BR /&gt;
thank you so much for your reply. You just solved my problem.</description>
      <pubDate>Wed, 21 May 2008 09:55:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variation-between-dates/m-p/19133#M2949</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-05-21T09:55:12Z</dc:date>
    </item>
  </channel>
</rss>

