<?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: Is there a LEAD() function like in SQL? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/602970#M174642</link>
    <description>&lt;P&gt;To me the simplest (and fastest) is simply to use :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data T2;
  merge T1
        T1 (firstobs=2 keep=VAR rename=(VAR=LEAD_VAR) );
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 09 Nov 2019 11:04:59 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2019-11-09T11:04:59Z</dc:date>
    <item>
      <title>Is there a LEAD() function like in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/602857#M174596</link>
      <description>&lt;P&gt;Right now the best thing I can find is reversing the order of my dataset and using LAG. Is there an alternative to this?&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 20:19:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/602857#M174596</guid>
      <dc:creator>Krueger</dc:creator>
      <dc:date>2019-11-08T20:19:28Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a LEAD() function like in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/602858#M174597</link>
      <description>If you have SAS ETS then PROC EXPAND can do that. &lt;BR /&gt;If not, here's some approaches. &lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-simulate-the-LEAD-function-opposite-of-LAG/ta-p/232151" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-simulate-the-LEAD-function-opposite-of-LAG/ta-p/232151&lt;/A&gt;</description>
      <pubDate>Fri, 08 Nov 2019 20:20:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/602858#M174597</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-11-08T20:20:46Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a LEAD() function like in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/602959#M174635</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/295821"&gt;@Krueger&lt;/a&gt;;&lt;/P&gt;
&lt;P&gt;There're many. IMO, the simplest (which I don't find in the link by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, nor in the old link within the link) is reading the Kth record downstream from the current using SET POINT=K.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example for the most common scenario of creating K=1 lead values in every BY group:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                                                                                                                                                     
  input id var ;                                                                                                                                                                                                                                                
  cards ;                                                                                                                                                                                                                                                       
1  1                                                                                                                                                                                                                                                            
2  2                                                                                                                                                                                                                                                            
2  3                                                                                                                                                                                                                                                            
3  4                                                                                                                                                                                                                                                            
3  5                                                                                                                                                                                                                                                            
3  6                                                                                                                                                                                                                                                            
;                                                                                                                                                                                                                                                               
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
data lead ;                                                                                                                                                                                                                                                     
  set have ;                                                                                                                                                                                                                                           
  by id ;                                                                                                                                                                                                                                                       
  _n_ + 1 ;                                                                                                                                                                                                                                                     
  if not last.id then set have (keep=var rename=var=lead_var) point = _n_ ;                                                                                                                                                                                              
  else call missing (lead_var) ;                                                                                                                                                                                                                                
run ;                                                                  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;id    var    lead_var                                                                                                                                                                                                                                           
---------------------                                                                                                                                                                                                                                           
 1     1         .                                                                                                                                                                                                                                              
 2     2         3                                                                                                                                                                                                                                              
 2     3         .                                                                                                                                                                                                                                              
 3     4         5                                                                                                                                                                                                                                              
 3     5         6                                                                                                                                                                                                                                              
 3     6         .
&lt;/PRE&gt;
&lt;P&gt;If the leads are to be created throughout the file with no regard to BY grouping, the idea is the same, and the code is virtually identical:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lead ;                                                                                                                                                                                                                                                     
  set have end = z ;                                                                                                                                                                                                                                            
  _n_ + 1 ;                                                                                                                                                                                                                                                     
  if not z then set have (keep=var rename=var=lead_var) point = _n_ ;                                                                                                                                                                                           
  else call missing (lead_var) ;                                                                                                                                                                                                                                
run ;                          
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Nov 2019 05:05:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/602959#M174635</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-11-09T05:05:06Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a LEAD() function like in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/602970#M174642</link>
      <description>&lt;P&gt;To me the simplest (and fastest) is simply to use :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data T2;
  merge T1
        T1 (firstobs=2 keep=VAR rename=(VAR=LEAD_VAR) );
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 09 Nov 2019 11:04:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/602970#M174642</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-11-09T11:04:59Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a LEAD() function like in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/603005#M174663</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;It's simplest and fastest in this simple case. In the more common BY case it gets less simple since the records in each BY group have to be enumerated first, i.e., for example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                                                                                                                                                     
  input id var ;                                                                                                                                                                                                                                                
  cards ;                                                                                                                                                                                                                                                       
1  1                                                                                                                                                                                                                                                            
2  2                                                                                                                                                                                                                                                            
2  3                                                                                                                                                                                                                                                            
3  4                                                                                                                                                                                                                                                            
3  5                                                                                                                                                                                                                                                            
3  6                                                                                                                                                                                                                                                            
;                                                                                                                                                                                                                                                               
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
data v (keep=id var seq) / view = v ;                                                                                                                                                                                                                                             
  set have ;                                                                                                                                                                                                                                                    
  by id ;                                                                                                                                                                                                                                                       
  if first.id then seq = 1 ;                                                                                                                                                                                                                                    
  else             seq + 1 ;                                                                                                                                                                                                                                    
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
data lead (drop = seq) ;                                                                                                                                                                                                                                        
  merge have v (rename=var=lead_var where=(seq &amp;gt; 1)) ;                                                                                                                                                                                                          
  by id ;                                                                                                                                                                                                                                                       
  if last.id then call missing (lead_var) ;                                                                                                                                                                                                                     
run ;                       
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;As far as the fastest goes, with this method the file ends up being read twice even when generating a lead is needed only for certain records upon a specific condition. E.g., picture a situation where one needs to replace VAR with its lead only when VAR is missing and such records constitute but a tiny fraction of the whole file. In such a case, it would be faster to grab the needed leads via POINT= only for those specific records, would it not?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D. &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Nov 2019 18:34:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/603005#M174663</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-11-09T18:34:10Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a LEAD() function like in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/603075#M174705</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt; For a BY group, I'd probably do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data LEAD2;
  set HAVE;
  by ID;
  if ^LASTOBS then 
    set HAVE(firstobs=2 keep=VAR rename=(VAR=LEAD_VAR)) end=LASTOBS;
  if last.ID then call missing (LEAD_VAR) ;       
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If a small fraction of the values has to be retrieved, the losses due to direct POINT= access would be small, and as you point out this method would probably be preferable.&lt;/P&gt;
&lt;P&gt;In any case, since we are reading the next observation, losses of either method are probably limited since we are always reading from the cache. One of the reads will be made from RAM, but . I random access logic has some overheads that sequential read does not.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE ; do VAR=1 to 1e7; output; end;

data WANT;
  merge HAVE
        HAVE (firstobs=2 keep=VAR rename=(VAR=LEAD_VAR) );
run;
/*    real time           2.48 seconds
      user cpu time       1.95 seconds  */

data lead ;      
  set have end = z ;                                                                                                                                                                                                                                            
  _n_ + 1 ;                                                                                                                                                                                                                                                     
  if not z then set have (keep=var rename=var=lead_var) point = _n_ ;                                                                                                                                                                                           
  else call missing (lead_var) ;                                                                                                                                                                                                                                
run ;      
/*    real time           3.54 seconds
      user cpu time       2.71 seconds  */

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Nov 2019 21:13:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/603075#M174705</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-11-10T21:13:39Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a LEAD() function like in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/603091#M174710</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;: Amen to all that.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Nov 2019 22:07:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-LEAD-function-like-in-SQL/m-p/603091#M174710</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-11-10T22:07:23Z</dc:date>
    </item>
  </channel>
</rss>

