<?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: Lag Function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/311908#M270733</link>
    <description>&lt;P&gt;IFN and IFC are often the best way to avoid the most commong&amp;nbsp;pitfalls of lags:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
  set have;
   new_smo_flags=ifc(policy_no=lag(policy_no) and cust_id=lag(cust_id),lag(smo_flag),smo_flag);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the benefit of ifn (for numeric results) and ifc (for char results) is tha both possible options are evaluated (i.e.the lag queue&amp;nbsp; is updated) no matter which option is returned.&amp;nbsp; These functions are syntactically like the IF function in excel.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Mark;&lt;/P&gt;</description>
    <pubDate>Wed, 16 Nov 2016 05:52:51 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2016-11-16T05:52:51Z</dc:date>
    <item>
      <title>Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/311762#M270730</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data looks like below&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cust_id_lag and policy_lag are the columns on which i applied lag for the first two columns of the below dataset.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Ploicy_NO Cust_iD Smo_Flag Cust_ID_lag Policy_lag&lt;BR /&gt;1011 36543 Y . .&lt;BR /&gt;1011 36543 N 36542 1011&lt;BR /&gt;1011 45654 N 36543 1011&lt;BR /&gt;2011 77567 Y 45654 1011&lt;BR /&gt;2011 77567 N 77567 2011&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My output should be&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need a new flag accordingly as New_smo_flag&amp;nbsp;&lt;/P&gt;&lt;P&gt;My conditon should be if policy_no=&lt;SPAN&gt;Policy_lag &amp;nbsp; and&amp;nbsp;Cust_iD=Cust_ID_lag &amp;nbsp; then &lt;SPAN&gt;New_Smo_Flag&lt;/SPAN&gt;=lag(Smo_Flag);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Ploicy_NO Cust_iD Smo_Flag &amp;nbsp; Cust_ID_lag &amp;nbsp;Policy_lag &amp;nbsp; New_Smo_Flag&lt;BR /&gt;1011 36543 Y &amp;nbsp; &amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; . &amp;nbsp; &amp;nbsp; Y&lt;BR /&gt;1011 36543 N 36542 1011 Y&lt;BR /&gt;1011 45654 N 36543 1011 N&lt;BR /&gt;2011 77567 Y 45654 1011 Y&lt;BR /&gt;2011 77567 N 77567 2011 Y&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please need the code for this urgently. asap.&lt;/P&gt;&lt;P&gt;Drop comments if need more information&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 16:28:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/311762#M270730</guid>
      <dc:creator>SBRVamsi</dc:creator>
      <dc:date>2016-11-15T16:28:19Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/311793#M270731</link>
      <description>&lt;P&gt;You're using the wrong tool for the job.&amp;nbsp; You could possibly get it to work, but LAG is more complicated than it looks.&amp;nbsp; If you want LAG to retrieve the value from the previous observation, it should never be used inside an IF/THEN statement.&amp;nbsp; A better approach (assuming your data set is sorted):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by policy_no custid;&lt;/P&gt;
&lt;P&gt;if first.custid then new_SMO_flag = SMO_flag;&lt;/P&gt;
&lt;P&gt;retain new_SMO_flag;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is probably what you are after (but if not, these would still be the right tools to apply with perhaps a small amount of tweaking).&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 17:44:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/311793#M270731</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-11-15T17:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/311898#M270732</link>
      <description>&lt;PRE&gt;
CODE NOT TEST

data want;
 set have;
 lag= lag(Smo_Flag)
 if policy_no=Policy_lag   and Cust_iD=Cust_ID_lag   then New_Smo_Flag=lag ;
................

&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Nov 2016 05:03:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/311898#M270732</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-16T05:03:02Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/311908#M270733</link>
      <description>&lt;P&gt;IFN and IFC are often the best way to avoid the most commong&amp;nbsp;pitfalls of lags:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
  set have;
   new_smo_flags=ifc(policy_no=lag(policy_no) and cust_id=lag(cust_id),lag(smo_flag),smo_flag);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the benefit of ifn (for numeric results) and ifc (for char results) is tha both possible options are evaluated (i.e.the lag queue&amp;nbsp; is updated) no matter which option is returned.&amp;nbsp; These functions are syntactically like the IF function in excel.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Mark;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2016 05:52:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/311908#M270733</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2016-11-16T05:52:51Z</dc:date>
    </item>
  </channel>
</rss>

