<?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 LAG Function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33220#M6454</link>
    <description>Good Afternoon;&lt;BR /&gt;
&lt;BR /&gt;
I have table with following columns:&lt;BR /&gt;
&lt;BR /&gt;
Yr        qtr         rep            sales&lt;BR /&gt;
2010    1          100            $1000&lt;BR /&gt;
2010    2          100            $2000&lt;BR /&gt;
&lt;BR /&gt;
I would like to create datastep to add previous yrqtr sales column using LAG function.  So i would have&lt;BR /&gt;
&lt;BR /&gt;
Yr        qtr         rep            sales       previousyrqtrsales&lt;BR /&gt;
2010    1          100            $1000            $0&lt;BR /&gt;
2010    2          100            $2000            $0&lt;BR /&gt;
2011    1          100            $3000            $1000&lt;BR /&gt;
2011    2          100            $4000            $2000&lt;BR /&gt;
&lt;BR /&gt;
Data Work.Sales (keep=year qtr rep sales PreviousYrQtr_Sales);&lt;BR /&gt;
Set work.Sales_new;&lt;BR /&gt;
Format PreviousYrQtr_Sales 8.;&lt;BR /&gt;
PreviousYrQtr_Sales = LAG(sales);&lt;BR /&gt;
Run;&lt;BR /&gt;
&lt;BR /&gt;
This gave me the value from the previous row in previousYrQtr_sales column&lt;BR /&gt;
&lt;BR /&gt;
Yr        qtr         rep            sales       previousyrqtrsales&lt;BR /&gt;
2010    1          100            $1000            $0&lt;BR /&gt;
2010    2          100            $2000            $1000&lt;BR /&gt;
2011    1          100            $3000            $2000&lt;BR /&gt;
2011    2          100            $4000            $3000&lt;BR /&gt;
&lt;BR /&gt;
What function or code i need to get previous year qtr sales listed ?&lt;BR /&gt;
&lt;BR /&gt;
Thanks</description>
    <pubDate>Fri, 18 Mar 2011 16:19:13 GMT</pubDate>
    <dc:creator>newbi</dc:creator>
    <dc:date>2011-03-18T16:19:13Z</dc:date>
    <item>
      <title>LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33220#M6454</link>
      <description>Good Afternoon;&lt;BR /&gt;
&lt;BR /&gt;
I have table with following columns:&lt;BR /&gt;
&lt;BR /&gt;
Yr        qtr         rep            sales&lt;BR /&gt;
2010    1          100            $1000&lt;BR /&gt;
2010    2          100            $2000&lt;BR /&gt;
&lt;BR /&gt;
I would like to create datastep to add previous yrqtr sales column using LAG function.  So i would have&lt;BR /&gt;
&lt;BR /&gt;
Yr        qtr         rep            sales       previousyrqtrsales&lt;BR /&gt;
2010    1          100            $1000            $0&lt;BR /&gt;
2010    2          100            $2000            $0&lt;BR /&gt;
2011    1          100            $3000            $1000&lt;BR /&gt;
2011    2          100            $4000            $2000&lt;BR /&gt;
&lt;BR /&gt;
Data Work.Sales (keep=year qtr rep sales PreviousYrQtr_Sales);&lt;BR /&gt;
Set work.Sales_new;&lt;BR /&gt;
Format PreviousYrQtr_Sales 8.;&lt;BR /&gt;
PreviousYrQtr_Sales = LAG(sales);&lt;BR /&gt;
Run;&lt;BR /&gt;
&lt;BR /&gt;
This gave me the value from the previous row in previousYrQtr_sales column&lt;BR /&gt;
&lt;BR /&gt;
Yr        qtr         rep            sales       previousyrqtrsales&lt;BR /&gt;
2010    1          100            $1000            $0&lt;BR /&gt;
2010    2          100            $2000            $1000&lt;BR /&gt;
2011    1          100            $3000            $2000&lt;BR /&gt;
2011    2          100            $4000            $3000&lt;BR /&gt;
&lt;BR /&gt;
What function or code i need to get previous year qtr sales listed ?&lt;BR /&gt;
&lt;BR /&gt;
Thanks</description>
      <pubDate>Fri, 18 Mar 2011 16:19:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33220#M6454</guid>
      <dc:creator>newbi</dc:creator>
      <dc:date>2011-03-18T16:19:13Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33221#M6455</link>
      <description>Are you lookin for PROC PRINT perhaps;&lt;BR /&gt;
&lt;BR /&gt;
Proc print data=&lt;YOUR data="https://communities.sas.com/" set="" name=""&gt;;&lt;BR /&gt;
var Yr qtr rep sales previousyrqtr_sales;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Depending on how your dollar signs are getting displayed you probably want to use a DOLLARw.d format.&lt;BR /&gt;
&lt;BR /&gt;
The first value will be missing for previousyrqtr_sales as there isn't a previous one to read.&lt;/YOUR&gt;</description>
      <pubDate>Fri, 18 Mar 2011 16:47:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33221#M6455</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2011-03-18T16:47:19Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33222#M6456</link>
      <description>Thanks for the resonse.  I actully  need to create the "Previour YrQtr Sales" column.</description>
      <pubDate>Fri, 18 Mar 2011 16:51:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33222#M6456</guid>
      <dc:creator>newbi</dc:creator>
      <dc:date>2011-03-18T16:51:00Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33223#M6457</link>
      <description>Will this work?&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data sales;&lt;BR /&gt;
input year qtr rep sales:dollar4.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
2010 1 100 $1000 &lt;BR /&gt;
2010 2 100 $2000 &lt;BR /&gt;
2011 1 100 $3000 &lt;BR /&gt;
2011 2 100 $4000 &lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data sales;&lt;BR /&gt;
	if _n_=1 then do while(not eof);&lt;BR /&gt;
		set sales end=eof;&lt;BR /&gt;
		array sales_array[2010:2011, 1:2] _temporary_;&lt;BR /&gt;
		sales_array[year, qtr]=sales;&lt;BR /&gt;
	end;&lt;BR /&gt;
	set sales;&lt;BR /&gt;
	retain sales_array;&lt;BR /&gt;
	if year &amp;gt; 2010 then previousyrqtrsales = sales_array[year-1, qtr];&lt;BR /&gt;
	else previousyrqtrsales = 0;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Alternatively, you could sort by qtr and year and then use the lag function.</description>
      <pubDate>Fri, 18 Mar 2011 16:51:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33223#M6457</guid>
      <dc:creator>polingjw</dc:creator>
      <dc:date>2011-03-18T16:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33224#M6458</link>
      <description>Thanks for all your help.  I was able to use Array function.&lt;BR /&gt;
&lt;BR /&gt;
One more question.  With in the array function how can i create additional column that would give me a "Yearly" total for previous year ?</description>
      <pubDate>Fri, 18 Mar 2011 19:09:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33224#M6458</guid>
      <dc:creator>newbi</dc:creator>
      <dc:date>2011-03-18T19:09:22Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33225#M6459</link>
      <description>Hi.&lt;BR /&gt;
I think you need firstly to sort your dataset.Such as:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sort data=have;&lt;BR /&gt;
 by qtr yr;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Sat, 19 Mar 2011 03:42:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33225#M6459</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-03-19T03:42:54Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33226#M6460</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input Yr $&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; qtr $&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rep $&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sales $;&lt;BR /&gt;pre_sales=lag2(sales);&lt;BR /&gt;cards;&lt;BR /&gt;2010&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 100&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $1000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;2010&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 100&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $2000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;2011&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 100&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $3000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;2011&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 100&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $4000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;;&lt;BR /&gt;proc print;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 May 2015 12:07:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33226#M6460</guid>
      <dc:creator>bharathtuppad</dc:creator>
      <dc:date>2015-05-08T12:07:52Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33227#M6461</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What if the number of Qtr is dynamic ?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 May 2015 13:20:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/33227#M6461</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2015-05-08T13:20:20Z</dc:date>
    </item>
  </channel>
</rss>

