<?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: How do i get a variable to evaluate using Lag or Retain in an If-Then statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-get-a-variable-to-evaluate-using-Lag-or-Retain-in-an-If/m-p/617296#M180862</link>
    <description>&lt;P&gt;Thank you for the tip. The code works as intended now.&lt;/P&gt;</description>
    <pubDate>Tue, 14 Jan 2020 20:01:10 GMT</pubDate>
    <dc:creator>icatNYC</dc:creator>
    <dc:date>2020-01-14T20:01:10Z</dc:date>
    <item>
      <title>How do i get a variable to evaluate using Lag or Retain in an If-Then statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-get-a-variable-to-evaluate-using-Lag-or-Retain-in-an-If/m-p/617281#M180851</link>
      <description>&lt;P&gt;Hello SAS Experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Possibly a very simple solution to my problem but please advise. I cannot get the Prior_Check variable to evaluate with the proper value. Any help would be appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Sample;
input Date_order Time_order Curr_Value	Level1 Level2;
datalines;
0 1 100 125 110
0 2	103.5	124 109
;
run;

Data Sample_Evaluation;
set Sample;
Length Live_Check Prior_Check Decision $35.;
Live_Check='0000';
Prior_Check='0000';
Decision='0000';
run;

Data Sample_Evaluation;
/*retain Decision;*//* this statement did not work for me*/ 
set Sample_Evaluation;
/*by Date_order Time_order Decision;*/ /* this statement did not work for me*/ 

if Date_order=0 and Time_order=1 then do;
	if Curr_Value&amp;gt;  Level1 then do;
		Live_Check='Active';
		Decision='Active - High';
	end;
	if Curr_Value&amp;lt;  Level2 then do;
		Live_Check='Active';
		Decision='Active-Low';
	end;
end;

if Date_order=0 and Time_order=2 then do;
	if Curr_Value&amp;gt;  Level1 then do;
		Live_Check='Active';
		Prior_Check=lag(Decision);
			if Live_Check='Active' and Prior_Check='0000' then do;
			Decision='Active - High';
			end;
			else if Live_Check='Active' and Prior_Check ne '0000' then do;
			Decision='Already Active - High';
			end;
	end;
	if Curr_Value&amp;lt;  Level2 then do;
		Live_Check='Active';
		Prior_Check=lag(Decision);/*want this to evaluate to 'Active - Low'*/
			if Live_Check='Active' and Prior_Check='0000' then do;
			Decision='Active - Low';
			end;
			else if Live_Check='Active' and Prior_Check ne '0000' then do;
			Decision='Already Active - Low';
			end;
	end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Jan 2020 18:56:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-get-a-variable-to-evaluate-using-Lag-or-Retain-in-an-If/m-p/617281#M180851</guid>
      <dc:creator>icatNYC</dc:creator>
      <dc:date>2020-01-14T18:56:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do i get a variable to evaluate using Lag or Retain in an If-Then statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-get-a-variable-to-evaluate-using-Lag-or-Retain-in-an-If/m-p/617288#M180857</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/283996"&gt;@icatNYC&lt;/a&gt;&amp;nbsp;Hi and welcome to the SAS Communtiy! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you read the &lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=n0l66p5oqex1f2n1quuopdvtcjqb.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;Lag Function Doc&lt;/A&gt; carefully you will understand why.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Shortly put, you should not consider the Lag Function a 'Lookback' function. Rather, the LagN Function creates a queue with N elements. Each time the function is executed, the current value of the lagged variable is inserted into the bottom of the queue and the top element of the queue is pushed out of the queue and returned from the function. This is the reason that calling the function conditionally can produce undesirable results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See this small example to understand the Lag Function&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 have;
input id x;
datalines;
1 2
3 4
1 3
2 4
2 5
2 6
;

data want;
   set have;
   lag_x = lag2(x);
run;

/*

Content of queue 

id x  Queue content
1  1  [ 1 | . ] Returned value: .
1  2  [ 2 | 1 ] Returned value: .
1  3  [ 3 | 2 ] Returned value: 1
2  4  [ 4 | 3 ] Returned value: 2
2  5  [ 5 | 4 ] Returned value: 3
2  6  [ 6 | 5 ] Returned value: 4

*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Jan 2020 19:28:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-get-a-variable-to-evaluate-using-Lag-or-Retain-in-an-If/m-p/617288#M180857</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-01-14T19:28:28Z</dc:date>
    </item>
    <item>
      <title>Re: How do i get a variable to evaluate using Lag or Retain in an If-Then statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-get-a-variable-to-evaluate-using-Lag-or-Retain-in-an-If/m-p/617291#M180859</link>
      <description>&lt;P&gt;Thanks for the welcome note! I understand the behavior better now, however is there a "lookback" function that I can use to give me the desired results? This code is intended for a long loop and I would like to process everything SAS. I am exploring a VBA option to give me the desired results but I prefer to keep everything in one "system"&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 19:39:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-get-a-variable-to-evaluate-using-Lag-or-Retain-in-an-If/m-p/617291#M180859</guid>
      <dc:creator>icatNYC</dc:creator>
      <dc:date>2020-01-14T19:39:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do i get a variable to evaluate using Lag or Retain in an If-Then statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-get-a-variable-to-evaluate-using-Lag-or-Retain-in-an-If/m-p/617294#M180861</link>
      <description>&lt;P&gt;Since your Lag Function is identical in the two if-blocks, you can simply pre-compute the lagged variable like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Sample_Evaluation;
/*retain Decision;*//* this statement did not work for me*/ 
set Sample_Evaluation;
/*by Date_order Time_order Decision;*/ /* this statement did not work for me*/ 

if Date_order=0 and Time_order=1 then do;
	if Curr_Value&amp;gt;  Level1 then do;
		Live_Check='Active';
		Decision='Active - High';
	end;
	if Curr_Value&amp;lt;  Level2 then do;
		Live_Check='Active';
		Decision='Active-Low';
	end;
end;

Prior_Check=lag(Decision);

if Date_order=0 and Time_order=2 then do;
	if Curr_Value&amp;gt;  Level1 then do;
		Live_Check='Active';
			if Live_Check='Active' and Prior_Check='0000' then do;
			Decision='Active - High';
			end;
			else if Live_Check='Active' and Prior_Check ne '0000' then do;
			Decision='Already Active - High';
			end;
	end;
	if Curr_Value&amp;lt;  Level2 then do;
		Live_Check='Active';
			if Live_Check='Active' and Prior_Check='0000' then do;
			Decision='Active - Low';
			end;
			else if Live_Check='Active' and Prior_Check ne '0000' then do;
			Decision='Already Active - Low';
			end;
	end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Jan 2020 19:53:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-get-a-variable-to-evaluate-using-Lag-or-Retain-in-an-If/m-p/617294#M180861</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-01-14T19:53:20Z</dc:date>
    </item>
    <item>
      <title>Re: How do i get a variable to evaluate using Lag or Retain in an If-Then statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-get-a-variable-to-evaluate-using-Lag-or-Retain-in-an-If/m-p/617296#M180862</link>
      <description>&lt;P&gt;Thank you for the tip. The code works as intended now.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 20:01:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-get-a-variable-to-evaluate-using-Lag-or-Retain-in-an-If/m-p/617296#M180862</guid>
      <dc:creator>icatNYC</dc:creator>
      <dc:date>2020-01-14T20:01:10Z</dc:date>
    </item>
    <item>
      <title>Re: How do i get a variable to evaluate using Lag or Retain in an If-Then statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-get-a-variable-to-evaluate-using-Lag-or-Retain-in-an-If/m-p/617297#M180863</link>
      <description>&lt;P&gt;Anytime, glad to help &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 20:04:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-get-a-variable-to-evaluate-using-Lag-or-Retain-in-an-If/m-p/617297#M180863</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-01-14T20:04:25Z</dc:date>
    </item>
  </channel>
</rss>

