<?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: Calculation with lag in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719887#M222949</link>
    <description>&lt;P&gt;Thank you, Kurt,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I change just for _N_=1 the code to initialize the missing values:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if _n_=1 then do;
	MT_EVT=0;
	MT_EA_AVMVT=0;
	DeltaEAs=0;
    EAs =max(MT_EA,MT_EVT);
	end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have the 0 in columns, I can't understand why the value EAs=329577,34745 is not retened in EAs_avant.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MarieT_0-1613567317356.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54838i0D08559940C5C144/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MarieT_0-1613567317356.png" alt="MarieT_0-1613567317356.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your help !&lt;/P&gt;</description>
    <pubDate>Wed, 17 Feb 2021 13:10:36 GMT</pubDate>
    <dc:creator>SASdevAnneMarie</dc:creator>
    <dc:date>2021-02-17T13:10:36Z</dc:date>
    <item>
      <title>Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719560#M222812</link>
      <description>&lt;P&gt;Hello Experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My data is :&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data donnees;
	input ID X2 X3;
	cards;
1 10 20 
2 38 50
3 45 60
4 70 80
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would like to calculate my values with lag from the line _N_-1, but this lags is also calculated. My code is :&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data donnees1;
	set donnees;

	/**Calculation of X4 and X5***/
	if ID=1 then
		do;
			X4=.;
			X5=X3;
		end;

	X5_avant=lag(X5);

	if ID=2 then
		do;
			X4=X5_avant+X2;
			X5=X5_avant+X3;
		end;

	X5_avant=lag(X5);

	if ID=3 then
		do;
			X4=X5_avant+X2;
			X5=X5_avant+X3;
		end;

	X5_avant=lag(X5);

	if ID=4 then
		do;
			X4=X5_avant+X2;
			X5=X5_avant+X3;
		end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I wrote a macro where I recreate the data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/****Solution***/
%macro values;
	%do i=1 %to 3;

		data donnees;
			set donnees;

			/**Calculation of X4 and X5***/
			if ID=1 then
				do;
					X4=.;
					X5=X3;
				end;

			X5_avant=lag(X5);

			if ID^=1 then
				do;
					X4=X5_avant+X2;
					X5=X5_avant+X3;
				end;

			X5_avant=lag(X5);
		run;

	%end;
%mend;

%values;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do you know please another more efficient algorithme that takes less time ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much !&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 09:17:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719560#M222812</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-02-16T09:17:35Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719563#M222814</link>
      <description>&lt;P&gt;Is the result of that macro what you expect?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 09:33:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719563#M222814</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-16T09:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719564#M222815</link>
      <description>Yes, but in my example I have only 4 rows, in my real data I have much more observations. I'm wondering if my code is efficient.</description>
      <pubDate>Tue, 16 Feb 2021 09:39:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719564#M222815</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-02-16T09:39:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719571#M222820</link>
      <description>&lt;P&gt;With your example data, this code creates your expected result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set donnees;
retain x4 x5 x5_avant; /* x4 and x5 only to keep order of variables */
x4 = x5_avant + x2; /* creates a missing value in 1st observation */
x5 = sum(x5_avant,x3);
output;
x5_avant = sum(x5_avant,x3);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you wanted x4 to be equal to x2 in the first observation, use a SUM() function to calculate it.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 10:11:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719571#M222820</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-16T10:11:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719581#M222828</link>
      <description>Thank you Kurt !&lt;BR /&gt;&lt;BR /&gt;As I understood, the "Output" allows to calculated the values of x5_avant on line _N_=3 with the data from _N_=2. The SUM() function is ignorign the missing values ?&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 16 Feb 2021 10:37:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719581#M222828</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-02-16T10:37:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719586#M222832</link>
      <description>&lt;P&gt;SUM() ignores missing values; it will only result in a missing value (and write the corresponding NOTE to the log) when all its arguments are missing.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 10:41:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719586#M222832</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-16T10:41:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719590#M222836</link>
      <description>Thank you, Kurt !&lt;BR /&gt;&lt;BR /&gt;The x5_avant=20 from line _N_=2 is calculated as sum(x5_avant,x3) from line _N_=1 because of "output" in code?  I don't understand how we holded the data from the first line for calculating the values for second line. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Tue, 16 Feb 2021 10:51:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719590#M222836</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-02-16T10:51:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719591#M222837</link>
      <description>&lt;P&gt;That happens because of the RETAIN statement. It prevents that x5_avant is set to missing at the start of a data step iteration.&lt;/P&gt;
&lt;P&gt;And the explicit OUTPUT statement keeps the previous value of x5_avant in the dataset, as it happens before the calculation.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 10:58:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719591#M222837</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-16T10:58:56Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719595#M222838</link>
      <description>Thank you, Kurt !</description>
      <pubDate>Tue, 16 Feb 2021 11:00:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719595#M222838</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-02-16T11:00:57Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719643#M222857</link>
      <description>Kurt, &lt;BR /&gt;Is it possible to introduce the condition in your code ? For example, &lt;BR /&gt;fo first line (_N_=1) I would like to calculate the values like :&lt;BR /&gt;x4 = x5_avant + x2; &lt;BR /&gt;x5 = sum(x5_avant,x3);&lt;BR /&gt;but for another lines the values are :&lt;BR /&gt;x4 = x5_avant + x2/10; &lt;BR /&gt;x5 = sum(x5_avant,x2/10); I suppose that we can't, beacause of Retain statement.            Thank you for your answer !</description>
      <pubDate>Tue, 16 Feb 2021 14:34:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719643#M222857</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-02-16T14:34:04Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719647#M222859</link>
      <description>&lt;P&gt;Using x5_avant in a simple calculation in observation 1 makes no sense, as it is not set at that time.&lt;/P&gt;
&lt;P&gt;But you can expand my code to create non-missing values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set donnees;
retain x4 x5 x5_avant;
x4 = sum(x5_avant,ifn(_n_ = 1,x2,x2/10));
x5 = sum(x5_avant,ifn(_n_ = 1,x3,x3/10);
output;
x5_avant = sum(x5_avant,x3);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Feb 2021 15:01:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719647#M222859</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-16T15:01:45Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719650#M222860</link>
      <description>Thank you, Kurt !</description>
      <pubDate>Tue, 16 Feb 2021 15:05:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719650#M222860</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-02-16T15:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719824#M222924</link>
      <description>&lt;P&gt;Hello Kurt,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry, for insisting &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I adopted the algorithm to my data, but I don't have the right results, I'm wondering if I understand your proposition of code.&amp;nbsp; My code is :&lt;/P&gt;
&lt;P&gt;I attached the data Variation3 and file with the right results.&lt;/P&gt;
&lt;P&gt;Thank you very much for your help !&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data Variation4;
	set Variation3;
	by NO_POLICE;
	retain DeltaEAs EAs EAs_avant;

	if _n_=1 then do;
	DeltaEAs=MT_EVT + 5;
    EAs =max(MT_EA,MT_EVT);
	end;

	if _n_^=1 then do;
	if MT_EVT=0 then DeltaEAs=0;
	if MT_EVT&amp;gt;0 then DeltaEAs=MT_EVT;
	if MT_EVT&amp;lt;0 then DeltaEAs=EAs_avant*(MT_EVT/MT_EA_AVMVT);
    EAs=sum(EAs_avant,DeltaEAs);
	end;

	output;
	EAs_avant = sum(EAs_avant,DeltaEAs);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2021 08:49:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719824#M222924</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-02-17T08:49:01Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719878#M222945</link>
      <description>&lt;P&gt;MT_EVT is missing in the first observation, so DeltaEAs will also be missing, causing EAs_avant to be missing &amp;nbsp;also (all arguments of the SUM function are missing).&lt;/P&gt;
&lt;P&gt;Since all subsequent MT_EVT are negative, this calculation is done&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DeltaEAs=EAs_avant*(MT_EVT/MT_EA_AVMVT)&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and will always result in a missing value for DeltaEAs (as long as EAs_avant is missing), which then causes EAs_avant to stay missing.&lt;/P&gt;
&lt;P&gt;All this is clearly reflected in the log (Maxim 2!):&lt;/P&gt;
&lt;PRE&gt; 73         data Variation4;
 74           set Variation3;
 75           by NO_POLICE;
 76           retain DeltaEAs EAs EAs_avant;
 77         
 78           if _n_=1 then do;
 79           DeltaEAs=MT_EVT + 5;
 80             EAs =max(MT_EA,MT_EVT);
 81           end;
 82         
 83           if _n_^=1 then do;
 84           if MT_EVT=0 then DeltaEAs=0;
 85           if MT_EVT&amp;gt;0 then DeltaEAs=MT_EVT;
 86           if MT_EVT&amp;lt;0 then DeltaEAs=EAs_avant*(MT_EVT/MT_EA_AVMVT);
 87             EAs=sum(EAs_avant,DeltaEAs);
 88           end;
 89         
 90           output;
 91           EAs_avant = sum(EAs_avant,DeltaEAs);
 92         run;
 
 NOTE: Missing values were generated as a result of performing an operation on missing values.
       Each place is given by: (Number of times) at (Line):(Column).
       1 bei 79:19    12 bei 86:38   12 bei 87:9    13 bei 91:15   
 NOTE: There were 14 observations read from the data set WORK.VARIATION3.
 NOTE: The data set WORK.VARIATION4 has 14 observations and 9 variables.
 NOTE:  Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit):
       real time           0.02 seconds
       cpu time            0.01 seconds
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2021 12:41:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719878#M222945</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-17T12:41:09Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719887#M222949</link>
      <description>&lt;P&gt;Thank you, Kurt,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I change just for _N_=1 the code to initialize the missing values:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if _n_=1 then do;
	MT_EVT=0;
	MT_EA_AVMVT=0;
	DeltaEAs=0;
    EAs =max(MT_EA,MT_EVT);
	end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have the 0 in columns, I can't understand why the value EAs=329577,34745 is not retened in EAs_avant.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MarieT_0-1613567317356.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54838i0D08559940C5C144/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MarieT_0-1613567317356.png" alt="MarieT_0-1613567317356.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your help !&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2021 13:10:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719887#M222949</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-02-17T13:10:36Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719894#M222953</link>
      <description>&lt;P&gt;In the first observation, DeltaEAs is set to zero, and the SUM function in the last statement of the data step will also set EAs_avant to zero (sum of a missing value and zero).&lt;/P&gt;
&lt;P&gt;Then, in all observations where MT_EVT is negative (which it is in your example dataset), this formula is calculated:&lt;/P&gt;
&lt;PRE&gt;DeltaEAs=EAs_avant*(MT_EVT/MT_EA_AVMVT)&lt;/PRE&gt;
&lt;P&gt;Since EAs_avant is zero, the result of this is also zero.&lt;/P&gt;
&lt;P&gt;So the SUM of EAs_avant (0) and DeltaEAs (0) will still be zero.&lt;/P&gt;
&lt;P&gt;And so the zero perpetuates throughout your dataset.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2021 13:27:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719894#M222953</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-17T13:27:04Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation with lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719901#M222955</link>
      <description>Thank you Kurt ! I understood.&lt;BR /&gt;Have I nice day !&lt;BR /&gt;&lt;BR /&gt;Marie</description>
      <pubDate>Wed, 17 Feb 2021 13:58:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-with-lag/m-p/719901#M222955</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-02-17T13:58:02Z</dc:date>
    </item>
  </channel>
</rss>

