<?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 Corresponding to LEAD and PARTITION BY in PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Corresponding-to-LEAD-and-PARTITION-BY-in-PROC-SQL/m-p/601859#M174154</link>
    <description>&lt;P&gt;Hi SAS community,&lt;BR /&gt;&lt;BR /&gt;Need little help with a code . As LEAD and partition by does not works in PROC SQL . Not able to get the desired output.&lt;BR /&gt;&lt;BR /&gt;Have a table like below&lt;BR /&gt;&lt;BR /&gt;Id. Date Amount&lt;BR /&gt;1. 1-jan. 100&lt;BR /&gt;1. 2-jan. 100&lt;BR /&gt;1. 3-jan. 500&lt;BR /&gt;1. 4-jan. 100&lt;BR /&gt;1. 5-jan. 200&lt;BR /&gt;1. 6-jan. 200&lt;BR /&gt;&lt;BR /&gt;We need to create a separate table which will have start date and end date based on the change of amount. So the desired output needed is&lt;BR /&gt;&lt;BR /&gt;Id. Start Date End Date Amount&lt;BR /&gt;1. 1-jan. 2-jan. 100&lt;BR /&gt;1. 3-jan. 3-jan. 500&lt;BR /&gt;1. 4-jan. 4-jan. 100&lt;BR /&gt;1. 5-jan. 6-jan. 200&lt;BR /&gt;&lt;BR /&gt;We only need to use PROC SQL to get the desired result.Thanks for the help in advance.&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Wed, 06 Nov 2019 09:56:51 GMT</pubDate>
    <dc:creator>vaibhavpratap31</dc:creator>
    <dc:date>2019-11-06T09:56:51Z</dc:date>
    <item>
      <title>Corresponding to LEAD and PARTITION BY in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Corresponding-to-LEAD-and-PARTITION-BY-in-PROC-SQL/m-p/601859#M174154</link>
      <description>&lt;P&gt;Hi SAS community,&lt;BR /&gt;&lt;BR /&gt;Need little help with a code . As LEAD and partition by does not works in PROC SQL . Not able to get the desired output.&lt;BR /&gt;&lt;BR /&gt;Have a table like below&lt;BR /&gt;&lt;BR /&gt;Id. Date Amount&lt;BR /&gt;1. 1-jan. 100&lt;BR /&gt;1. 2-jan. 100&lt;BR /&gt;1. 3-jan. 500&lt;BR /&gt;1. 4-jan. 100&lt;BR /&gt;1. 5-jan. 200&lt;BR /&gt;1. 6-jan. 200&lt;BR /&gt;&lt;BR /&gt;We need to create a separate table which will have start date and end date based on the change of amount. So the desired output needed is&lt;BR /&gt;&lt;BR /&gt;Id. Start Date End Date Amount&lt;BR /&gt;1. 1-jan. 2-jan. 100&lt;BR /&gt;1. 3-jan. 3-jan. 500&lt;BR /&gt;1. 4-jan. 4-jan. 100&lt;BR /&gt;1. 5-jan. 6-jan. 200&lt;BR /&gt;&lt;BR /&gt;We only need to use PROC SQL to get the desired result.Thanks for the help in advance.&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2019 09:56:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Corresponding-to-LEAD-and-PARTITION-BY-in-PROC-SQL/m-p/601859#M174154</guid>
      <dc:creator>vaibhavpratap31</dc:creator>
      <dc:date>2019-11-06T09:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Corresponding-to-LEAD-and-PARTITION-BY-in-PROC-SQL/m-p/601872#M174162</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/298352"&gt;@vaibhavpratap31&lt;/a&gt;&amp;nbsp;and welcome to the SAS Community &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;Does it have to be done in PROC SQL? Seems easier in a data step like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id Date $ Amount;
datalines;
1 1-jan 100
1 2-jan 100
1 3-jan 500
1 4-jan 100
1 5-jan 200
1 6-jan 200
;

data want(drop=Date);
    format Id Start_Date Last_Date Amount;
    do _N_=1 by 1 until (last.Amount);
        set have;
        by Amount notsorted;
        if _N_=1 then Start_Date=Date;
    end;
    Last_Date=Date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Id  Start_Date  End_Date  Amount
1   1-jan       2-jan     100
1   3-jan       3-jan     500
1   4-jan       4-jan     100
1   5-jan       6-jan     200&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Nov 2019 06:45:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Corresponding-to-LEAD-and-PARTITION-BY-in-PROC-SQL/m-p/601872#M174162</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-11-06T06:45:45Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Corresponding-to-LEAD-and-PARTITION-BY-in-PROC-SQL/m-p/601880#M174168</link>
      <description>&lt;P&gt;SQL is not suited for look-back/look-ahead issues, the data step is the tool of choice (see Maxim 14). See another approach using mechanisms supplied by the data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id amount notsorted;
retain start_date;
if first.amount then start_date = date;
if last.amount
then do;
  end_date = date;
  output;
end;
keep id start_date end_date amount;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/298352"&gt;@vaibhavpratap31&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi SAS community,&lt;BR /&gt;&lt;BR /&gt;Need little help with a code . As LEAD and partition by does not works in PROC SQL . Not able to get the desired output.&lt;BR /&gt;&lt;BR /&gt;Have a table like below&lt;BR /&gt;&lt;BR /&gt;Id. Date Amount&lt;BR /&gt;1. 1-jan. 100&lt;BR /&gt;1. 2-jan. 100&lt;BR /&gt;1. 3-jan. 500&lt;BR /&gt;1. 4-jan. 100&lt;BR /&gt;1. 5-jan. 200&lt;BR /&gt;1. 6-jan. 200&lt;BR /&gt;&lt;BR /&gt;We need to create a separate table which will have start date and end date based on the change of amount. So the desired output needed is&lt;BR /&gt;&lt;BR /&gt;Id. Start Date End Date Amount&lt;BR /&gt;1. 1-jan. 2-jan. 100&lt;BR /&gt;1. 3-jan. 3-jan. 500&lt;BR /&gt;1. 4-jan. 4-jan. 100&lt;BR /&gt;1. 5-jan. 6-jan. 200&lt;BR /&gt;&lt;BR /&gt;We only need to use PROC SQL to get the desired result.Thanks for the help in advance.&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2019 07:02:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Corresponding-to-LEAD-and-PARTITION-BY-in-PROC-SQL/m-p/601880#M174168</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-06T07:02:23Z</dc:date>
    </item>
  </channel>
</rss>

