<?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: Using Lag Properly in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Properly/m-p/65762#M14280</link>
    <description>Thank you, this will work.</description>
    <pubDate>Wed, 10 Mar 2010 21:17:32 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2010-03-10T21:17:32Z</dc:date>
    <item>
      <title>Using Lag Properly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Properly/m-p/65760#M14278</link>
      <description>Hello,&lt;BR /&gt;
&lt;BR /&gt;
I have the following data:&lt;BR /&gt;
input_table (ordered by customernumber, code number):&lt;BR /&gt;
&lt;BR /&gt;
customernumber       code_number&lt;BR /&gt;
&lt;BR /&gt;
1		                  	a1&lt;BR /&gt;
1			                  b1&lt;BR /&gt;
1			                  c1&lt;BR /&gt;
2			                  a1	&lt;BR /&gt;
2			                  c1&lt;BR /&gt;
3			                  c1&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
I'd like to use lag to look 2 rows prior, determine if the customernumber is the same for the prior 2 customernumber rows, then look at the code number, and determine if I'm on code number c1, and if so look back at the 2 prior code number rows are a1 and b1. If so, this meets the criteria I'm looking for. As an example, customernumber 1 is the only one above that matches this criteria. I'm using the following SAS code to do this, using lag. Please let me know if this looks good, thx!&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
libname  sassvrtable 'my/sasdatabase/table'&lt;BR /&gt;
data output_data;&lt;BR /&gt;
set sassvrtable.input_table;&lt;BR /&gt;
&lt;BR /&gt;
\*prev custnumber*\&lt;BR /&gt;
lag_data_custnum = lag(customernumber);&lt;BR /&gt;
&lt;BR /&gt;
\*2nd prev custnumber*\&lt;BR /&gt;
lag_data_custnum2 = lag(lag_data_custnum);&lt;BR /&gt;
&lt;BR /&gt;
\*prev codenumber*\&lt;BR /&gt;
lag_code_number = lag(code_number);&lt;BR /&gt;
\*2 prev codenumber*\&lt;BR /&gt;
lag_code_number_2 = lag(lag_code_number);&lt;BR /&gt;
&lt;BR /&gt;
\*IF MEETS MY CRITERIA*\&lt;BR /&gt;
if lag_data_custnum = customernumber&lt;BR /&gt;
and lag_data_custnum2 = customernumber&lt;BR /&gt;
and code_number = c1&lt;BR /&gt;
and lag_code_number = b1&lt;BR /&gt;
and lag_code_number_2 = a1&lt;BR /&gt;
then match = 'TRUE';&lt;BR /&gt;
&lt;BR /&gt;
Proc Print data = output_data;&lt;BR /&gt;
run;</description>
      <pubDate>Tue, 09 Mar 2010 04:50:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Properly/m-p/65760#M14278</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-03-09T04:50:39Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag Properly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Properly/m-p/65761#M14279</link>
      <description>Hi Scott,&lt;BR /&gt;
&lt;BR /&gt;
Try using lag2 instead of lag&lt;BR /&gt;
&lt;BR /&gt;
Milton</description>
      <pubDate>Tue, 09 Mar 2010 06:32:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Properly/m-p/65761#M14279</guid>
      <dc:creator>milts</dc:creator>
      <dc:date>2010-03-09T06:32:42Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag Properly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Properly/m-p/65762#M14280</link>
      <description>Thank you, this will work.</description>
      <pubDate>Wed, 10 Mar 2010 21:17:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-Properly/m-p/65762#M14280</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-03-10T21:17:32Z</dc:date>
    </item>
  </channel>
</rss>

