<?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: Proc SQL equivalent for Retain and Lag functinon in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-equivalent-for-Retain-and-Lag-functinon/m-p/809818#M319367</link>
    <description>&lt;P&gt;Your original data step code can be made a lot simpler like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dt;
  set dt;
  by cin master_id card_issue_dte idx;
  if first.card_issue_dte then
    mn=1;
  else 
    mn+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To answer your question: no, there is not a similar facility in SAS SQL. The point being that SQL is a declarative language (you tell the compiler/interpreter what you want, not how to get it and in what order), while the SAS data step is procedural (you tell the compiler/interpreter what to do).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some other SQL dialects have something that may work for what you are doing, e.g. T-SQL (Microsoft):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select *, row_number() over (partition by cin,master_id,card_issue_dt order by idx) as mn from dt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So if you are actually doing some of your stuff on a database server, look up the local SQL syntax. You may get the result you want with pass through SQL (select * from connection to &amp;lt;server&amp;gt;(&amp;lt;SQL expression&amp;gt;).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you are using pure SAS, it is better to stick with the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 26 Apr 2022 07:11:44 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2022-04-26T07:11:44Z</dc:date>
    <item>
      <title>Proc SQL equivalent for Retain and Lag functinon</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-equivalent-for-Retain-and-Lag-functinon/m-p/809807#M319357</link>
      <description>&lt;P&gt;Hello ~&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone know the equivalent code using Proc SQL for the following SAS data step? The lag and retain function seems hard to achieve in Proc SQL&lt;/P&gt;&lt;PRE&gt;data dt;
set dt;
by cin master_id card_issue_dte idx;
retain mn(1);
if cin=lag(cin) and master_id=lag(master_id) and card_issue_dte=lag(card_issue_dte) then mn+1;
if cin=lag(cin) and master_id=lag(master_id) and card_issue_dte ne lag(card_issue_dte) then mn=1;
if cin=lag(cin) and master_id ne lag(master_id) then mn=1;
if cin ne lag(cin) then mn=1;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Apr 2022 06:12:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-equivalent-for-Retain-and-Lag-functinon/m-p/809807#M319357</guid>
      <dc:creator>wbsjd</dc:creator>
      <dc:date>2022-04-26T06:12:44Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL equivalent for Retain and Lag functinon</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-equivalent-for-Retain-and-Lag-functinon/m-p/809818#M319367</link>
      <description>&lt;P&gt;Your original data step code can be made a lot simpler like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dt;
  set dt;
  by cin master_id card_issue_dte idx;
  if first.card_issue_dte then
    mn=1;
  else 
    mn+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To answer your question: no, there is not a similar facility in SAS SQL. The point being that SQL is a declarative language (you tell the compiler/interpreter what you want, not how to get it and in what order), while the SAS data step is procedural (you tell the compiler/interpreter what to do).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some other SQL dialects have something that may work for what you are doing, e.g. T-SQL (Microsoft):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select *, row_number() over (partition by cin,master_id,card_issue_dt order by idx) as mn from dt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So if you are actually doing some of your stuff on a database server, look up the local SQL syntax. You may get the result you want with pass through SQL (select * from connection to &amp;lt;server&amp;gt;(&amp;lt;SQL expression&amp;gt;).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you are using pure SAS, it is better to stick with the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Apr 2022 07:11:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-equivalent-for-Retain-and-Lag-functinon/m-p/809818#M319367</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-04-26T07:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL equivalent for Retain and Lag functinon</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-equivalent-for-Retain-and-Lag-functinon/m-p/809909#M319415</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/308056"&gt;@wbsjd&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello ~&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does anyone know the equivalent code using Proc SQL for the following SAS data step? &lt;STRONG&gt;The lag and retain function seems hard to achieve in Proc SQL&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes it is hard to achieve and significantly more work.&lt;/P&gt;
&lt;P&gt;It's why a data step can be more powerful and faster to process data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Doing something like this in SQL often means joining the data with itself (self join) along some criteria in this case this seems like logic that could also be accomplished with BY processing.&amp;nbsp; In general adding row counters is a pain in SQL. Several SQL languages offer alternatives to support this logic, ie CURSOR logic in T-SQL.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Apr 2022 16:56:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-equivalent-for-Retain-and-Lag-functinon/m-p/809909#M319415</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-04-26T16:56:14Z</dc:date>
    </item>
  </channel>
</rss>

