<?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: Can someone help me with this lag function, where only the first row is known? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Can-someone-help-me-with-this-lag-function-where-only-the-first/m-p/813887#M81810</link>
    <description>&lt;P&gt;The first time your LAG() function call runs the result is always a missing because it has not yet saved any values from the previous times it ran.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do not use addition with missing values, the result is always a missing value.&amp;nbsp; The SUM() function will ignore the missing values and just add up the non-missing values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You do NOT want the NEW variables to already exist in the input dataset. When you read the next observation with the SET statement it will retrieve those missing values and overwrite anything you might have tried to calculate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you have this series of NUM values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input num @@;
cards;
633 450 22 11 10 9 4 5 1 5
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And some initial value for the NEW variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps you want 3,000 as the initial value?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  retain n1 3000;
  n1 = sum(n1,-lag(num));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    num     n1

  1    633    3000
  2    450    2367
  3     22    1917
  4     11    1895
  5     10    1884
  6      9    1874
  7      4    1865
  8      5    1861
  9      1    1856
 10      5    1855
&lt;/PRE&gt;</description>
    <pubDate>Tue, 17 May 2022 17:25:38 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-05-17T17:25:38Z</dc:date>
    <item>
      <title>Can someone help me with this lag function, where only the first row is known?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Can-someone-help-me-with-this-lag-function-where-only-the-first/m-p/813313#M81804</link>
      <description>&lt;P&gt;hi, I have something like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data data;
   infile datalines delimiter='	'; 
   input num	total	N	N1;
   datalines;                      
633	3000	2367	2367
450	3000	.	.
22	3000	.	.
11	3000	.	.
10	3000	.	.
9	3000	.	.
4	3000	.	.
5	3000	.	.
1	3000	.	.
5	3000	.	.

;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This should be really easy but I'm not so good with SAS.. I simply want to populate the next rows by the difference between lagged N (or N1) and num... so I'd want 2367, 1734, 1284, etc.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying N1=lag(N1)-lag(num) but I get this error:&amp;nbsp;&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV&gt;&lt;DIV class=""&gt;Missing values were generated as a result of performing an operation on&lt;/DIV&gt;&lt;DIV class=""&gt;missing values.&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;Any help would be really appreciated!&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&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;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Fri, 13 May 2022 22:49:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Can-someone-help-me-with-this-lag-function-where-only-the-first/m-p/813313#M81804</guid>
      <dc:creator>richart</dc:creator>
      <dc:date>2022-05-13T22:49:33Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone help me with this lag function, where only the first row is known?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Can-someone-help-me-with-this-lag-function-where-only-the-first/m-p/813862#M81808</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I cannot figure out what you want.&lt;/P&gt;
&lt;P&gt;Do you want something like the below? See WANT dataset produced.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   infile datalines delimiter='	'; 
   input num	total	N	N1;
numrow=_N_;
   datalines;                      
633	3000	2367	2367
450	3000	.	.
22	3000	.	.
11	3000	.	.
10	3000	.	.
9	3000	.	.
4	3000	.	.
5	3000	.	.
1	3000	.	.
5	3000	.	.
;
run;

proc timedata data=have out=_NULL_ OUTARRAY=WANT(drop=numrow _season_) 
              print=(scalars arrays);
              id numrow interval=day acc=total format=date9.;
              vars num	total;
              outarrays MyN;

       do t = 1 to dim(num);
        if t=1 then do; MyN[t] = total[t] - num[t]; end;
		else        do; MyN[t] = MyN[t-1] - num[t]; end;
       end;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Tue, 17 May 2022 17:03:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Can-someone-help-me-with-this-lag-function-where-only-the-first/m-p/813862#M81808</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2022-05-17T17:03:51Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone help me with this lag function, where only the first row is known?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Can-someone-help-me-with-this-lag-function-where-only-the-first/m-p/813886#M81809</link>
      <description>&lt;P&gt;I'm not sure what you want to do exactly. I suppose your issue is that when you the lag function the variable in the lag function has to be a variable from the dataset in the set statement. I suppose that is the cause for the missing values message.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try to run the code below and see if that makes sense to you:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   infile datalines delimiter='	'; 
   input num	total	N	N1;
   datalines;                      
633	3000	2367	2367
450	3000	.	.
22	3000	.	.
11	3000	.	.
10	3000	.	.
9	3000	.	.
4	3000	.	.
5	3000	.	.
1	3000	.	.
5	3000	.	.
;

data want;
   set have;
   retain help;
   if _n_ eq 1 then do; N1new = N1; help = N1; end;
   lagnum = lag(num);
   if lagnum ne . then do; N1new = help - lagnum; help = N1new; end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Best regards, Jos&lt;/P&gt;</description>
      <pubDate>Tue, 17 May 2022 17:13:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Can-someone-help-me-with-this-lag-function-where-only-the-first/m-p/813886#M81809</guid>
      <dc:creator>JosvanderVelden</dc:creator>
      <dc:date>2022-05-17T17:13:37Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone help me with this lag function, where only the first row is known?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Can-someone-help-me-with-this-lag-function-where-only-the-first/m-p/813887#M81810</link>
      <description>&lt;P&gt;The first time your LAG() function call runs the result is always a missing because it has not yet saved any values from the previous times it ran.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do not use addition with missing values, the result is always a missing value.&amp;nbsp; The SUM() function will ignore the missing values and just add up the non-missing values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You do NOT want the NEW variables to already exist in the input dataset. When you read the next observation with the SET statement it will retrieve those missing values and overwrite anything you might have tried to calculate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you have this series of NUM values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input num @@;
cards;
633 450 22 11 10 9 4 5 1 5
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And some initial value for the NEW variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps you want 3,000 as the initial value?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  retain n1 3000;
  n1 = sum(n1,-lag(num));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    num     n1

  1    633    3000
  2    450    2367
  3     22    1917
  4     11    1895
  5     10    1884
  6      9    1874
  7      4    1865
  8      5    1861
  9      1    1856
 10      5    1855
&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 May 2022 17:25:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Can-someone-help-me-with-this-lag-function-where-only-the-first/m-p/813887#M81810</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-17T17:25:38Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone help me with this lag function, where only the first row is known?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Can-someone-help-me-with-this-lag-function-where-only-the-first/m-p/813926#M81812</link>
      <description>&lt;P&gt;thanks that worked! I guess if I understand correctly you must make the new lagged variable in the datastep&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 May 2022 19:26:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Can-someone-help-me-with-this-lag-function-where-only-the-first/m-p/813926#M81812</guid>
      <dc:creator>richart</dc:creator>
      <dc:date>2022-05-17T19:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone help me with this lag function, where only the first row is known?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Can-someone-help-me-with-this-lag-function-where-only-the-first/m-p/813933#M81813</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240551"&gt;@richart&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;thanks that worked! I guess if I understand correctly you must make the new lagged variable in the datastep&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;There is a difference between retaining a variable (which just means it is not set to missing at the start of each iteration) and the LAG() function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The LAG() function basically just builds a stack/queue of values. Each time it executes the current value of its argument is pushed onto the stack and the request lagged instance is returned as the result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To see what is happening for your simple data step adds some PUT or PUTLOG statement at different places in your data step to see how the values are changing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  put (_n_ num total N N1 n1new help lagnum) (=);
   set have;
  put (_n_ num total N N1 n1new help lagnum) (=);
   retain help;
   if _n_ eq 1 then do; N1new = N1; help = N1; end;
  put (_n_ num total N N1 n1new help lagnum) (=);
   lagnum = lag(num);
  put (_n_ num total N N1 n1new help lagnum) (=);
   if lagnum ne . then do; N1new = help - lagnum; help = N1new; end;
  put (_n_ num total N N1 n1new help lagnum) (=);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 May 2022 19:50:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Can-someone-help-me-with-this-lag-function-where-only-the-first/m-p/813933#M81813</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-17T19:50:07Z</dc:date>
    </item>
  </channel>
</rss>

