<?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: retaining calculated values depending on fixed criteria in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476241#M122526</link>
    <description>&lt;P&gt;Please clarify the rules you are using to get lagged values.&lt;/P&gt;</description>
    <pubDate>Sat, 07 Jul 2018 23:16:06 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2018-07-07T23:16:06Z</dc:date>
    <item>
      <title>retaining calculated values depending on fixed criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476240#M122525</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to calculate values based on prior values, either existing or calculated,&amp;nbsp;using the lag operator. The initial calculations are based on lagged values of an existing field but latter calculations are based on lagged values of the initial calculations. So the argument of the lag operator must change.&amp;nbsp;I have included the SAS&amp;nbsp;code I have tried without much success. Also here is what I want to happen using Excel--I want to calculate the variable X with s, h, b, and p as given. My example has a lag of 2 but this may vary.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;A&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;B&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;C&lt;/STRONG&gt;&lt;/TD&gt;&lt;T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;/TD&gt;&lt;TD&gt;145&lt;/TD&gt;&lt;TD&gt;0.10&lt;/TD&gt;&lt;TD&gt;176.827347&lt;/TD&gt;&lt;TD&gt;&amp;lt;-- =EXP(D14)*C12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;15&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;150&lt;/TD&gt;&lt;TD&gt;0.12&lt;/TD&gt;&lt;TD&gt;222.693957&lt;/TD&gt;&lt;TD&gt;&amp;lt;-- =EXP(D15)*E13&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;CODE class=" language-sas"&gt;/* Sample dataset */
data temp1;
  input s	h	b	p;
	datalines;
1	-6	102	0.8
1	-5	110	0.12
1	-4	105	0.15
1	-3	100	0.12
1	-2	104	0.11
1	-1	102	0.15
1	0	110	0.13
1	1	105	0.10
1	2	110	0.15
1	3	100	0.10
1	4	105	0.11
1	5	105	0.15
1	6	110	0.13
2	-6	160	0.11
2	-5	155	0.15
2	-4	140	0.13
2	-3	150	0.13
2	-2	165	0.14
2	-1	170	0.10
2	0	160	0.11
2	1	155	0.15
2	2	145	0.10
2	3	150	0.12
2	4	160	0.13
2	5	170	0.13
2	6	155	0.14
;
run;

proc print data=temp1;
   by s;
run;


/* Here use 2 period lag but lag can vary */
%let d=2;

data temp2;
  set temp1;
  by s h;

	retain x2;
	if h&amp;lt;=&amp;amp;d. then x1=lag&amp;amp;d.(b)*exp(p); if h&amp;lt;=0 then x1=.;
  x2=coalesce(lag&amp;amp;d.(x1)*exp(p),lag&amp;amp;d.(x2)*exp(p)); if h&amp;lt;=&amp;amp;d. then x2=.;
  x=coalesce(x2,x1);

run;

proc print data=temp2;
  by s;
  var s h b p x1 x2 x;
run;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 07 Jul 2018 23:07:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476240#M122525</guid>
      <dc:creator>su17</dc:creator>
      <dc:date>2018-07-07T23:07:47Z</dc:date>
    </item>
    <item>
      <title>Re: retaining calculated values depending on fixed criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476241#M122526</link>
      <description>&lt;P&gt;Please clarify the rules you are using to get lagged values.&lt;/P&gt;</description>
      <pubDate>Sat, 07 Jul 2018 23:16:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476241#M122526</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-07-07T23:16:06Z</dc:date>
    </item>
    <item>
      <title>Re: retaining calculated values depending on fixed criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476242#M122527</link>
      <description>&lt;P&gt;instead of "x1=lag&amp;amp;d.(b)*exp(p);" try: "a=lag&amp;amp;d.(b); x1=a*exp(p);"&lt;/P&gt;</description>
      <pubDate>Sat, 07 Jul 2018 23:41:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476242#M122527</guid>
      <dc:creator>pau13rown</dc:creator>
      <dc:date>2018-07-07T23:41:20Z</dc:date>
    </item>
    <item>
      <title>Re: retaining calculated values depending on fixed criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476243#M122528</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159527"&gt;@su17&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;When you call the lag() function for a specific variable the first time in a data step iteration then two things happen:&lt;/P&gt;
&lt;P&gt;1. It writes the value of current observation to a queue&lt;/P&gt;
&lt;P&gt;2. It retrieves the lagged value from the queue&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For this reason: Do not use a lag() function conditionally as else your queue gets "messed up"&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/kb/24/665.html&amp;nbsp;" target="_blank"&gt;http://support.sas.com/kb/24/665.html&amp;nbsp;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/resources/papers/proceedings16/11221-2016.pdf&amp;nbsp;" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings16/11221-2016.pdf&amp;nbsp;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"prior values, either existing or calculated"&lt;/P&gt;
&lt;P&gt;If you need a dynamic look-back use a look-up table. SAS hash tables for example give you a lot of flexibility.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To use prior calculated values: RETAIN such values, or use formulas where you always re-calculate everything (and look-up the required values), or write also calculated values to a SAS hash table so you can look them up anytime as required.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Jul 2018 01:27:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476243#M122528</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-07-08T01:27:12Z</dc:date>
    </item>
    <item>
      <title>Re: retaining calculated values depending on fixed criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476261#M122532</link>
      <description>&lt;P&gt;Since you are testing on the value of&amp;nbsp; variable H, you don't need a BY statement.&amp;nbsp; What you want&amp;nbsp; to do is unconditionally update the lag queues, but conditionally return the lag values.&amp;nbsp; That can be done by embedding LAG functions inside IFN or IFC:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Sample dataset */
data temp1;
  input s	h	b	p;
	datalines;
1	-6	102	0.8
1	-5	110	0.12
1	-4	105	0.15
1	-3	100	0.12
1	-2	104	0.11
1	-1	102	0.15
1	0	110	0.13
1	1	105	0.10
1	2	110	0.15
1	3	100	0.10
1	4	105	0.11
1	5	105	0.15
1	6	110	0.13
2	-6	160	0.11
2	-5	155	0.15
2	-4	140	0.13
2	-3	150	0.13
2	-2	165	0.14
2	-1	170	0.10
2	0	160	0.11
2	1	155	0.15
2	2	145	0.10
2	3	150	0.12
2	4	160	0.13
2	5	170	0.13
2	6	155	0.14
;
run;&lt;BR /&gt;&lt;BR /&gt;%let d=2;   /* edited insertion*/
data want;
   set temp1;
   x1=ifn(h&amp;gt;0,lag&amp;amp;d(b)*exp(p),.);
   x2=ifn(h&amp;gt;&amp;amp;d,lag&amp;amp;d(x1)*exp(p),.);
   x=coalesce(x2,x1);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P class="title"&gt;And I also disagree with the&amp;nbsp; advice to never put a lag function in an IF statement.&amp;nbsp; There are many use cases for this technique.&amp;nbsp; You can see some in my paper &lt;A href=" http://analytics.ncsu.edu/sesug/2017/SESUG2017_Paper-175_Final_PDF.pdf" target="_self"&gt;Leads and Lags: Static and Dynamic Queues in the SAS DATA STEP, 2nd ed.&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Jul 2018 07:39:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476261#M122532</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-07-08T07:39:53Z</dc:date>
    </item>
    <item>
      <title>Re: retaining calculated values depending on fixed criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476266#M122534</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;I'd say using the lag() function ONLY within a condition is an advanced technique. For this reason I normally write to not do it as people get this likely wrong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The OP wrote: &lt;EM&gt;So the argument of the lag operator must change&lt;/EM&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;When it comes to "hoping around" between observations like one can do it in Excel then I believe a look-up table is the way to go as the lag() function won't just give you access to whatever previous record dynamically.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Jul 2018 05:34:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476266#M122534</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-07-08T05:34:39Z</dc:date>
    </item>
    <item>
      <title>Re: retaining calculated values depending on fixed criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476636#M122677</link>
      <description>Thanks for the response. The criteria is exogenous but in any case cannot exceed available data. I simply chose 2 here since the sample data was short. In practice we may have in excess of 100 records and typical lags may be 1, 3, or 12.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 09 Jul 2018 23:29:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/476636#M122677</guid>
      <dc:creator>su17</dc:creator>
      <dc:date>2018-07-09T23:29:15Z</dc:date>
    </item>
    <item>
      <title>Re: retaining calculated values depending on fixed criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/477968#M123188</link>
      <description>&lt;P&gt;Thank you. This is very close but does not calculate correct values for any observation where h is greater than 2*d.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jul 2018 17:49:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-calculated-values-depending-on-fixed-criteria/m-p/477968#M123188</guid>
      <dc:creator>su17</dc:creator>
      <dc:date>2018-07-13T17:49:34Z</dc:date>
    </item>
  </channel>
</rss>

