<?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: Finding missing periods within a time sequence in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862930#M340877</link>
    <description>&lt;P&gt;Please provide example data where my code does not give the expected result, and the expected result for it.&lt;/P&gt;</description>
    <pubDate>Wed, 08 Mar 2023 13:01:58 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-03-08T13:01:58Z</dc:date>
    <item>
      <title>Finding missing periods within a time sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862769#M340795</link>
      <description>&lt;P&gt;I have a data set WORKERS. The variables are the worker's ID, maximum number of days on job (MaxDays) and days that the worker showed up to work (DayAtWork). Here's some trivial SAS code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data workers; input id maxDays DayAtWork;&lt;BR /&gt;cards;&lt;BR /&gt;125 8 1&lt;BR /&gt;125 8 2&lt;BR /&gt;125 8 5&lt;BR /&gt;125 8 6&lt;BR /&gt;125 8 8&lt;BR /&gt;150 3 1&lt;BR /&gt;150 3 2&lt;BR /&gt;150 3 3&lt;BR /&gt;162 4 2&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here's the data set I'd like to get:&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp;Y&amp;nbsp; N&lt;/P&gt;&lt;P&gt;125 5 3&lt;/P&gt;&lt;P&gt;150 3 0&lt;/P&gt;&lt;P&gt;162 1 3&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The tricky part comes in because in my real data set each worker could have 100s of days with various combinations of days in which they worked and days that they were absent.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help much appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Andrew&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2023 19:27:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862769#M340795</guid>
      <dc:creator>DocMartin</dc:creator>
      <dc:date>2023-03-07T19:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: Finding missing periods within a time sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862775#M340797</link>
      <description>&lt;P&gt;Y seems to be the frequency for the given ID, but how should N be calculated?&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2023 19:58:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862775#M340797</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-07T19:58:08Z</dc:date>
    </item>
    <item>
      <title>Re: Finding missing periods within a time sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862778#M340799</link>
      <description>&lt;P&gt;I think I see what you want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have (rename=(maxdays=y));
by id;
retain n;
if first.id
then n = 1;
else n + 1;
if last.id;
n = y - n;
keep id y n;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2023 20:05:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862778#M340799</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-07T20:05:09Z</dc:date>
    </item>
    <item>
      <title>Re: Finding missing periods within a time sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862791#M340805</link>
      <description>&lt;P&gt;I don't think so. Take the following two examples:&lt;BR /&gt;One guy works days 1,2, and 5 of 5 days. The other works days 1, 2, 3, 4, 5 of 5 days.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2023 20:29:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862791#M340805</guid>
      <dc:creator>DocMartin</dc:creator>
      <dc:date>2023-03-07T20:29:03Z</dc:date>
    </item>
    <item>
      <title>Re: Finding missing periods within a time sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862875#M340837</link>
      <description>&lt;P&gt;The simplest is probably an SQL solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;     
  create table want as                                                                                                                          
  select id,y,max-y as n                                                                                                                
  from(select id,count(distinct DayAtWork) as y,max(maxDays) as max from workers group by id);                                          
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I used COUNT(DISTINCT Dayatwork) in case the same day is repeated. The MAX(maxDays) is just to get a single value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can do it in a datastep (data must be sorted) like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;                                                                                                                              
  do until(last.id);                                                                                                                    
    set workers;                                                                                                                        
    by id DayAtWork;                                                                                                                    
    y=sum(y,first.DayAtWork);                                                                                                           
    end;                                                                                                                                
  n=maxDays-y;                                                                                                                          
  keep id y n;                                                                                                                          
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Mar 2023 07:52:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862875#M340837</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-03-08T07:52:02Z</dc:date>
    </item>
    <item>
      <title>Re: Finding missing periods within a time sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862930#M340877</link>
      <description>&lt;P&gt;Please provide example data where my code does not give the expected result, and the expected result for it.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Mar 2023 13:01:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862930#M340877</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-08T13:01:58Z</dc:date>
    </item>
    <item>
      <title>Re: Finding missing periods within a time sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862941#M340887</link>
      <description>&lt;P&gt;Ah, we also need to re-calculate y:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have (rename=(maxdays=y));
by id;
retain n;
if first.id
then n = 1;
else n + 1;
if last.id;
n = y - n;
y = y - n;
keep id y n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Mar 2023 13:44:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-missing-periods-within-a-time-sequence/m-p/862941#M340887</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-08T13:44:27Z</dc:date>
    </item>
  </channel>
</rss>

