<?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: Counting a set of continuous observations in a block in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567822#M159731</link>
    <description>&lt;P&gt;Of course; but it's only dictated by the nature of this particular task. In different situations, one may want to enqueue/dequeue LAG only when certain conditions are met. In any case, the function itself operates the same way regardless of whether it's part of a conditional clause or not.&amp;nbsp;&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;&lt;/P&gt;
&lt;P&gt;p.s. LAG is &lt;EM&gt;not&lt;/EM&gt; implemented as a &lt;EM&gt;stack&lt;/EM&gt; (LIFO) but as a &lt;EM&gt;queue&lt;/EM&gt; (FIFO). Because its size (the number of items) is fixed, the enque and dequeue operations are performed simultaneously, i.e. when a new item is inserted in the rear of the queue, the front item is ejected. The hash object can be used to implement a dynamic queue where (a) the enqueue and dequeue operations are independent and (b) the current number of items is the difference between the number of times the two operations, respectively, have been performed.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 21 Jun 2019 03:52:52 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-06-21T03:52:52Z</dc:date>
    <item>
      <title>Counting a set of continuous observations in a block</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567701#M159674</link>
      <description>&lt;P&gt;Good afternoon fellow SAS users and the overall SAS community.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If anyone has some time to spare, I have a question concerning counting observations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have data in the form of person-weeks and I want to count blocks of continuous weeks without a gap.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is some sample data.&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;claimant&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Week&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Episode_new&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;5&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;6&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&l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&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As a first attempt, I tried this, but it does not get me close.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by claimant;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if first.claimant then do; episode_new=1; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; else if week = lag(week) then do; episode_new=1; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; else if week &amp;gt; lag(week) then do; episode_new+1; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would anyone have any suggestions about how to tackle this question? Thanks, -Carmine&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 16:29:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567701#M159674</guid>
      <dc:creator>Carmine_Rossi</dc:creator>
      <dc:date>2019-06-20T16:29:21Z</dc:date>
    </item>
    <item>
      <title>Re: Counting a set of continuous observations in a block</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567706#M159677</link>
      <description>&lt;P&gt;What would the "answer" look like for these 3 claimants? Claimant 1 has two sets of weeks that are back to back? So would Claimant 1 have a value of 2 for this variable you are trying to compute?&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 16:33:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567706#M159677</guid>
      <dc:creator>svh</dc:creator>
      <dc:date>2019-06-20T16:33:59Z</dc:date>
    </item>
    <item>
      <title>Re: Counting a set of continuous observations in a block</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567709#M159678</link>
      <description>&lt;P&gt;Do not conditionally execute the LAG() function. It will mess up how it works.&amp;nbsp; Instead it is easier if you store the lagged value into a variable that you can then test.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input claimant Week Episode_new ;
cards;
1 1 1
1 2 1
1 3 1
1 5 2
1 6 2
1 8 3
1 10 4
2 1 1
3 1 1
3 2 1
3 4 2
3 5 2
;

data want ;
  set have;
  by claimant;
  lastweek=lag(week);
  if first.claimant then episode=1;
  else if week &amp;gt; lastweek+1 then episode+1;
run;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                           Episode_
Obs    claimant    Week       new      lastweek    episode

  1        1         1         1           .          1
  2        1         2         1           1          1
  3        1         3         1           2          1
  4        1         5         2           3          2
  5        1         6         2           5          2
  6        1         8         3           6          3
  7        1        10         4           8          4
  8        2         1         1          10          1
  9        3         1         1           1          1
 10        3         2         1           1          1
 11        3         4         2           2          2
 12        3         5         2           4          2&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 16:48:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567709#M159678</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-06-20T16:48:57Z</dc:date>
    </item>
    <item>
      <title>Re: Counting a set of continuous observations in a block</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567738#M159694</link>
      <description>&lt;P&gt;Thanks Tom. I will dissect your code in an effort to understand it better and add it to my SAS toolkit.&lt;/P&gt;&lt;P&gt;All the best!&lt;/P&gt;&lt;P&gt;-Carmine&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 19:11:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567738#M159694</guid>
      <dc:creator>Carmine_Rossi</dc:creator>
      <dc:date>2019-06-20T19:11:22Z</dc:date>
    </item>
    <item>
      <title>Re: Counting a set of continuous observations in a block</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567762#M159700</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/181197"&gt;@Carmine_Rossi&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;Your idea of using a queue is sound, only since you need to look at the &lt;EM&gt;difference&lt;/EM&gt; between the current week value and the one from the prior record, the queue function DIF is a more suitable tool than LAG:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                    
  input claimant Week ;                        
  cards ;                                      
1  1                                           
1  2                                           
1  3                                           
1  5                                           
1  6                                           
1  8                                           
1 10                                           
2  1                                           
3  1                                           
3  2                                           
3  4                                           
3  5                                           
run ;                                          
                                               
data want ;                                    
  set have ;                                   
  by claimant ;                                
  if first.claimant      then episode_new = 1 ;
  else if dif (week) &amp;gt; 1 then episode_new + 1 ;
run ;                                          
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;On the other hand, you are saying that&amp;nbsp; you want is to "&lt;SPAN&gt;count blocks of continuous weeks without a gap" for each claimant. Of course, the last record in each BY group in the file WANT gives you what you need. However, if you are only interested in the pure count rather than creating the variable EPISODE_NEW, you can either add the statement:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;if last.claimant ;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;to the code above or do it differently from the outset:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep = claimant count) ;
  do until (last.claimant) ;       
    set have ;                     
    by claimant ;                  
    length _s $ 32767 ;            
    substr (_s, week, 1) = "x" ;   
  end ;                            
  Count = countw (_s) ;            
run ;                              
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Kind regards&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Paul D.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 20:16:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567762#M159700</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-06-20T20:16:52Z</dc:date>
    </item>
    <item>
      <title>Re: Counting a set of continuous observations in a block</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567776#M159707</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;"&lt;SPAN&gt;Do not conditionally execute the LAG() function. It will mess up how it works."&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Could you please explain how calling the LAG function (or DIF function, for that matter) conditionally can "mess up how it works"?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If the condition is true, it is executed, and if it is false, it is not executed; that's all. From what I've seen, any problems with calling the queue functions conditionally arise from misunderstanding how they really work. In particular, it stems from the idea that LAG&lt;EM&gt;n&lt;/EM&gt;&amp;nbsp;is designed to "return the value from the &lt;EM&gt;n&lt;/EM&gt;th previous observation"; and I've been amazed to encounter this bizarre concept somehow firmly planted even in the heads of truly stellar SAS programmers, especially since the SAS documentation clearly states that LAG "returns values from a queue". And indeed, LAG&lt;EM&gt;n&lt;/EM&gt; is a simple static FIFO queue with &lt;EM&gt;n&lt;/EM&gt; fixed items, returning the item from its front when an item is inserted into its rear, and its action has nothing to do with any "observations" whatsoever.&amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Of course, LAG&lt;EM&gt;n&lt;/EM&gt; can be &lt;EM&gt;used&lt;/EM&gt; to return the value from the &lt;EM&gt;n&lt;/EM&gt;th previous observation, and perhaps the fact that it's used for this purpose most often has given rise to the idea that that is what the function is &lt;EM&gt;designed&lt;/EM&gt; to do. However, it can be also used for other purposes whenever utilizing a fixed queue as a data structure suits one's needs. For example, LAG (a 1-item queue) can be used in lieu of a temporary variable. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Also, methinks the name of the function itself, LAG, is chosen awry and adds to the misunderstanding. If it were called, for example, just using the word describing what it really does, i.e. QUEUE, it could make one pause and think of it in a less deviated direction.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Kind regards&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Paul D.&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 21:11:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567776#M159707</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-06-20T21:11:45Z</dc:date>
    </item>
    <item>
      <title>Re: Counting a set of continuous observations in a block</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567808#M159724</link>
      <description>&lt;P&gt;The way that LAG (and DIF) are implemented is by making a "stack" of the values when the function executes.&amp;nbsp; So if you skip executing the function on one observation then the value from that observation is never put onto the stack and so that value can never be recalled.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jun 2019 00:28:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567808#M159724</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-06-21T00:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: Counting a set of continuous observations in a block</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567822#M159731</link>
      <description>&lt;P&gt;Of course; but it's only dictated by the nature of this particular task. In different situations, one may want to enqueue/dequeue LAG only when certain conditions are met. In any case, the function itself operates the same way regardless of whether it's part of a conditional clause or not.&amp;nbsp;&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;&lt;/P&gt;
&lt;P&gt;p.s. LAG is &lt;EM&gt;not&lt;/EM&gt; implemented as a &lt;EM&gt;stack&lt;/EM&gt; (LIFO) but as a &lt;EM&gt;queue&lt;/EM&gt; (FIFO). Because its size (the number of items) is fixed, the enque and dequeue operations are performed simultaneously, i.e. when a new item is inserted in the rear of the queue, the front item is ejected. The hash object can be used to implement a dynamic queue where (a) the enqueue and dequeue operations are independent and (b) the current number of items is the difference between the number of times the two operations, respectively, have been performed.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jun 2019 03:52:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-a-set-of-continuous-observations-in-a-block/m-p/567822#M159731</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-06-21T03:52:52Z</dc:date>
    </item>
  </channel>
</rss>

