<?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: Check continuous year/date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512846#M138158</link>
    <description>&lt;P&gt;Taking care of every sort of overlap can also be done while reading the data only once&amp;nbsp;with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data myNewData2;
do until (last.id);
    set myData; by id fromYear; /* Check: fromYear is not decreasing */
    if fromYear - 1 &amp;gt; ty then do;
        if not missing(ty) then output;
        fy = fromYear; ty = toYear;
        end;
    else ty = max(toYear, ty);
    end;
output;
drop fromYear toYear;
rename fy=fromYear ty=toYear;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 14 Nov 2018 06:10:32 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2018-11-14T06:10:32Z</dc:date>
    <item>
      <title>Check continuous year/date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512714#M138107</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a simple data here, showing years of eligibility.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA mydata;&lt;BR /&gt;input&amp;nbsp;id fromyear toyear;&lt;BR /&gt;DATALINES;&lt;BR /&gt;1 1965 1966&lt;BR /&gt;1 1967 1968&lt;BR /&gt;1 2000 2001&lt;/P&gt;&lt;P&gt;2 1967 2000&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to crate a data, grouping a continuous eligibility.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, a new data set should look like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;id fromyear toyear&lt;/P&gt;&lt;P&gt;1 1965 1968&lt;/P&gt;&lt;P&gt;1 2000 2001&lt;/P&gt;&lt;P&gt;2 1967 2000&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could anyone tell me how to get this?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yoko&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Nov 2018 19:43:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512714#M138107</guid>
      <dc:creator>Yoko</dc:creator>
      <dc:date>2018-11-13T19:43:19Z</dc:date>
    </item>
    <item>
      <title>Re: Check continuous year/date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512729#M138111</link>
      <description>&lt;P&gt;Try next not tested code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have(rename=(year1=fromyear year2=toyear));
   by id;
        retain year1 year2;
        keep id year1 year2;

        if first.if then do;
           year1 = fromyear;
           year2 = toyear;
       end;
       else do;
          if last.id then do;&lt;BR /&gt;             year2 = toyear;&lt;BR /&gt;             output;  &lt;BR /&gt;          end; else
          if fromyear ^= (year2 + 1)
          then do;
                  output;
                  year1 = fromyear;
                  year2 = toyear;
          end;
          else  year2 = toyear;
      end;
run;

             
              &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Nov 2018 20:21:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512729#M138111</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-11-13T20:21:38Z</dc:date>
    </item>
    <item>
      <title>Re: Check continuous year/date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512764#M138124</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA mydata;
input id fromyear toyear;
DATALINES;
1 1965 1966
1 1967 1968
1 2000 2001
2 1967 2000
;
run;

 

data w;
set mydata;
by id;
retain c1 c2;
lag=lag(toyear);
if first.id then do; c1=fromyear;c2=toyear;end;
else if fromyear-lag in (1,0) then c2=toyear;
else  do; c1=fromyear;c2=toyear;end;
keep id c:;
run;

data want;
set w;
by id c1 notsorted;
if last.c1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Nov 2018 22:00:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512764#M138124</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-13T22:00:45Z</dc:date>
    </item>
    <item>
      <title>Re: Check continuous year/date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512781#M138131</link>
      <description>&lt;P&gt;This works even if your eligibility periods overlap:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA mydata;
input id fromyear toyear;
DATALINES;
1 1965 1966
1 1967 1968
1 2000 2001
2 1967 2000
;

data myNewData;
array y {1950:2050};
do until(last.id);
    set myData; by id;
    do i = fromYear to toYear;
        y{i} = 1;
        end;
    end;
call missing(fromYear, toYear);
do i = lbound(y) to hbound(y);
    if y{i} then do;
        if missing(fromYear) then fromYear = i;
        end;
    else do;
        if not missing(fromYear) then do;
            toYear = i - 1;
            output;
            call missing(fromYear, toYear);
            end;
        end;
    end;
drop i y:;
run;


proc print data=myNewData noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Nov 2018 23:17:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512781#M138131</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-11-13T23:17:13Z</dc:date>
    </item>
    <item>
      <title>Re: Check continuous year/date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512785#M138133</link>
      <description>&lt;P&gt;Sir ProdigyGeniusstats aka PG, shot!!!!!!!!!!&lt;/P&gt;
&lt;P&gt;linear,neat and stroke!!!!!!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Understood the idea. Class act!!!!!!!!!!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would a temp array make it even faster?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array y {1950:2050} _temporary_;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Nov 2018 23:34:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512785#M138133</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-13T23:34:29Z</dc:date>
    </item>
    <item>
      <title>Re: Check continuous year/date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512787#M138134</link>
      <description>&lt;P&gt;Probably, a bit. But you would have to explicitly reset the array to missing on every data step&amp;nbsp;iteration.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Nov 2018 23:40:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512787#M138134</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-11-13T23:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: Check continuous year/date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512788#M138135</link>
      <description>&lt;P&gt;Oh yes true&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Nov 2018 23:40:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512788#M138135</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-13T23:40:52Z</dc:date>
    </item>
    <item>
      <title>Re: Check continuous year/date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512838#M138152</link>
      <description>&lt;P&gt;This solution relies on understanding the "firstobs=2" option, and on how to populate the program data vector:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA mydata;
input id fromyear toyear;
DATALINES;
1 1965 1966
1 1967 1968
1 2000 2001
2 1967 2000
;

data want (drop=_:);
  set mydata (keep=id);
  by id;

  retain fromyear;

  merge mydata (rename=(fromyear=_curr_fromyear))
        mydata (firstobs=2 keep=fromyear rename=(fromyear=_nxt_fromyear));

  if first.id or _curr_fromyear&amp;gt;sum(lag(toyear),1) then fromyear=_curr_fromyear;
  if last.id or _nxt_fromyear&amp;gt;toyear+1;

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The "set mydata (keep=id); by id;" statements are there simply to provide access to the first.id and last.id dummies.&lt;/LI&gt;
&lt;LI&gt;The merge of mydata with itself has the second instance of mydata with "firstobs=2".&amp;nbsp; So you're reading obs 1 and 2 together, then obs&amp;nbsp;2 and&amp;nbsp;3 together, then 3 and 4, etc.&amp;nbsp; But of course they have the same variables, so to avoid collisions the current record (without the firstobs=2) renames fromyear to _curr_fromyear, and the following record (with firstobs=2) renames it to _nxt_curryear.&lt;/LI&gt;
&lt;LI&gt;Whenever you begin a new id, of have a record with _curr_fromyear more than a year after the prior toyear, [specified as lag(toyear)], then you're at the beginning of a time span, so assign a value to fromyear, which is retained across observations.&lt;/LI&gt;
&lt;LI&gt;And whenever you encounter the end of an id, or find the next fromyear more than a year after the current toyear, then allow the observation through the subsetting IF statement for output to want.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Editted note.&amp;nbsp; This will also work if periods overlap, as long as (1) data is sorted by ID/FROMYEAR, and (2) no TOYEAR precedes TOYEAR from a prior record.&amp;nbsp; (i.e. data is also sorted by ID/TOYEAR).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2018 05:15:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512838#M138152</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-11-14T05:15:23Z</dc:date>
    </item>
    <item>
      <title>Re: Check continuous year/date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512846#M138158</link>
      <description>&lt;P&gt;Taking care of every sort of overlap can also be done while reading the data only once&amp;nbsp;with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data myNewData2;
do until (last.id);
    set myData; by id fromYear; /* Check: fromYear is not decreasing */
    if fromYear - 1 &amp;gt; ty then do;
        if not missing(ty) then output;
        fy = fromYear; ty = toYear;
        end;
    else ty = max(toYear, ty);
    end;
output;
drop fromYear toYear;
rename fy=fromYear ty=toYear;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Nov 2018 06:10:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512846#M138158</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-11-14T06:10:32Z</dc:date>
    </item>
    <item>
      <title>Re: Check continuous year/date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512935#M138187</link>
      <description>&lt;P&gt;Thank you for your suggestion.&amp;nbsp; Unfortunately, it did not work.&amp;nbsp; But, your suggestion seems to be similar to the one suggested by&amp;nbsp;novinosrin.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2018 13:34:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512935#M138187</guid>
      <dc:creator>Yoko</dc:creator>
      <dc:date>2018-11-14T13:34:55Z</dc:date>
    </item>
    <item>
      <title>Re: Check continuous year/date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512936#M138188</link>
      <description>&lt;P&gt;Thank you for your suggestions.&amp;nbsp; Both worked!&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2018 13:36:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512936#M138188</guid>
      <dc:creator>Yoko</dc:creator>
      <dc:date>2018-11-14T13:36:09Z</dc:date>
    </item>
    <item>
      <title>Re: Check continuous year/date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512937#M138189</link>
      <description>&lt;P&gt;Thank you for your suggestion.&amp;nbsp; It worked!&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2018 13:36:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512937#M138189</guid>
      <dc:creator>Yoko</dc:creator>
      <dc:date>2018-11-14T13:36:49Z</dc:date>
    </item>
    <item>
      <title>Re: Check continuous year/date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512943#M138193</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA mydata;
input id fromyear toyear;
DATALINES;
1 1965 1966
1 1967 1968
1 2000 2001
2 1967 2000
;
data temp;
 set mydata;
 by id;
 if first.id or fromyear-lag(toyear)&amp;gt;1 then group+1;
run;
data want;
 set temp;
 by group;
 retain _fromyear;
 if first.group then _fromyear=fromyear;
 if last.group;
 drop fromyear group;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Nov 2018 13:51:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/512943#M138193</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-11-14T13:51:40Z</dc:date>
    </item>
    <item>
      <title>Re: Check continuous year/date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/513016#M138207</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree that "reading" a data set twice risks excessive use of disk input activity, and therefore should be avoided, as your program does.&amp;nbsp; But just to be clear, the presence of&amp;nbsp;both a SET and a MERGE statement in the program I suggested should not represent much increased disk input activity, because of how the operating system satisfies disk input requests.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because the SET and MERGE are tightly synchronized in this program, they are reading from the same disk page.&amp;nbsp; So once a page of data is put in memory by the operating system,&amp;nbsp;both the SET and MERGE will populate the program data vector from that memory page.&amp;nbsp; So yes, there are two streams of data established in the program, but the impact on actual resource use should be minimal.&amp;nbsp;&amp;nbsp;I haven't tested this understanding for quite a while, but I'd be surprised if there would be much increase in disk transfers in the example I suggested.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I appreciate you raising the issue.&amp;nbsp; I should have put a caveat with my program, because I'm sure that many sas users would see my code as having the same resource impact as two independent complete loops through the data - one for the SET, and one for the MERGE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2018 17:31:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-continuous-year-date/m-p/513016#M138207</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-11-14T17:31:03Z</dc:date>
    </item>
  </channel>
</rss>

