<?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: Lag in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Lag/m-p/578590#M164108</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16600"&gt;@SASPhile&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;It can done only if VALUE is a character variable. Is it? If not, it can be set a a missing value, not a blank. But you can set the system option missing="" and then see numeric missing values as a blank through whatever UI you're using. The code below should work regardless of the data type of VALUE.&lt;/P&gt;
&lt;P&gt;First, if stat="n" always on the first record in each ID group, then:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                               
  input ID Stat $ value ;                                                 
  cards ;                                                                 
11   n           20                                                       
11   mean        12                                                       
11   median    12.5                                                       
12   n            0                                                       
12   mean         0                                                       
12   median       0                                                       
;                                                                         
run ; &lt;BR /&gt;                                                                   
option missing = "" ;                                                                          
&lt;BR /&gt;data want (drop = _:) ;                                                   
  do until (last.id) ;                                                    
    set have ;                                                            
    by id ;                                                               
    if first.id and put (cats (value), $1.) = "0" then _setnull = 1 ;     
    if _setnull and stat in ("mean", "median") then call missing (value) ;
    output ;                                                              
  end ;                                                                   
run ;                                                                     
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If stat="n" (and also "mean" or "median") can be anywhere within an ID group, then, more generally:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop = _:) ;                                                   
  do _n_ = 1 by 1 until (last.id) ;                                       
    set have ;                                                            
    by id ;                                                               
    if stat = "n" and put (cats (value), $1.) = "0" then _setnull = 1 ;   
  end ;                                                                   
  do _n_ = 1 to _n_ ;                                                     
    set have ;                                                            
    if _setnull and stat in ("mean", "median") then call missing (value) ;
    output ;                                                              
  end ;                                                                   
run ;                                                                     
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Either way, CATS and CALL MISSING are used to make the code independent of the data type of VALUE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 01 Aug 2019 22:47:52 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-08-01T22:47:52Z</dc:date>
    <item>
      <title>Lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag/m-p/578567#M164099</link>
      <description>&lt;P&gt;There are about 100o records with below data structure&lt;/P&gt;&lt;P&gt;ID Stat&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;value&lt;BR /&gt;11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;n&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;BR /&gt;11&amp;nbsp; &amp;nbsp;mean&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12&lt;BR /&gt;11&amp;nbsp; &amp;nbsp;median&amp;nbsp; &amp;nbsp; 12.5&lt;BR /&gt;12&amp;nbsp; &amp;nbsp; &amp;nbsp; n&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;12&amp;nbsp; &amp;nbsp;mean&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;12&amp;nbsp; &amp;nbsp;median&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;if the stat n =0 mean and median should be set to blanks as below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID Stat&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;value&lt;BR /&gt;11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;n&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;BR /&gt;11&amp;nbsp; &amp;nbsp;mean&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12&lt;BR /&gt;11&amp;nbsp; &amp;nbsp;median&amp;nbsp; &amp;nbsp; 12.5&lt;BR /&gt;12&amp;nbsp; &amp;nbsp; &amp;nbsp; n&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;12&amp;nbsp; &amp;nbsp;mean&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;12&amp;nbsp; &amp;nbsp;median&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2019 20:36:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag/m-p/578567#M164099</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2019-08-01T20:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: Lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag/m-p/578588#M164107</link>
      <description>&lt;P&gt;Perhaps:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by id;
   retain nflag;
   if first.id then nflag=0;
   if stat='n' and value=0 then nflag=1;
   if nflag and stat in ('mean','median') then call missing (value);
   drop nflag;
end;&lt;/PRE&gt;
&lt;P&gt;Assumes the data is sorted by ID and that the N statistic always comes before the mean and median.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2019 22:45:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag/m-p/578588#M164107</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-08-01T22:45:31Z</dc:date>
    </item>
    <item>
      <title>Re: Lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag/m-p/578590#M164108</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16600"&gt;@SASPhile&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;It can done only if VALUE is a character variable. Is it? If not, it can be set a a missing value, not a blank. But you can set the system option missing="" and then see numeric missing values as a blank through whatever UI you're using. The code below should work regardless of the data type of VALUE.&lt;/P&gt;
&lt;P&gt;First, if stat="n" always on the first record in each ID group, then:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                               
  input ID Stat $ value ;                                                 
  cards ;                                                                 
11   n           20                                                       
11   mean        12                                                       
11   median    12.5                                                       
12   n            0                                                       
12   mean         0                                                       
12   median       0                                                       
;                                                                         
run ; &lt;BR /&gt;                                                                   
option missing = "" ;                                                                          
&lt;BR /&gt;data want (drop = _:) ;                                                   
  do until (last.id) ;                                                    
    set have ;                                                            
    by id ;                                                               
    if first.id and put (cats (value), $1.) = "0" then _setnull = 1 ;     
    if _setnull and stat in ("mean", "median") then call missing (value) ;
    output ;                                                              
  end ;                                                                   
run ;                                                                     
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If stat="n" (and also "mean" or "median") can be anywhere within an ID group, then, more generally:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop = _:) ;                                                   
  do _n_ = 1 by 1 until (last.id) ;                                       
    set have ;                                                            
    by id ;                                                               
    if stat = "n" and put (cats (value), $1.) = "0" then _setnull = 1 ;   
  end ;                                                                   
  do _n_ = 1 to _n_ ;                                                     
    set have ;                                                            
    if _setnull and stat in ("mean", "median") then call missing (value) ;
    output ;                                                              
  end ;                                                                   
run ;                                                                     
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Either way, CATS and CALL MISSING are used to make the code independent of the data type of VALUE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2019 22:47:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag/m-p/578590#M164108</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-01T22:47:52Z</dc:date>
    </item>
  </channel>
</rss>

