<?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: Select subjects who have records between specified range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901322#M356208</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Subject  $   start_yr            end_yr    yr;
cards;
A                 2015                 2017        2015
A                 2015                 2017        2016   
A                 2015                 2017        2017
B                 2011                  2016        2012 
B                 2011                  2016        2014 
B                 2011                  2016        2015 
B                 2011                  2016        2016
;

proc sql;
create table want as
select Subject ,start_yr,end_yr,(start_yr=min(yr) and max(yr)=end_yr and count(distinct yr)=end_yr-start_yr+1) as flag
 from have
  group by Subject ,start_yr,end_yr;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 03 Nov 2023 01:45:25 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2023-11-03T01:45:25Z</dc:date>
    <item>
      <title>Select subjects who have records between specified range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901310#M356200</link>
      <description>&lt;P&gt;I want to create a data set with subjects who have records for all years between START_YR and END_YR and create a flag (1=Yes 0 = No).&lt;/P&gt;&lt;P&gt;Data set:&lt;/P&gt;&lt;P&gt;Subject&amp;nbsp; &amp;nbsp; &amp;nbsp;start_yr&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end_yr&amp;nbsp; &amp;nbsp; yr&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2015&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2017&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2011&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2012&amp;nbsp;&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2011&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2014&amp;nbsp;&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2011&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2015&amp;nbsp;&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2011&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Want:&lt;/P&gt;&lt;P&gt;Subject&amp;nbsp; &amp;nbsp; &amp;nbsp;start_yr&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end_yr&amp;nbsp; &amp;nbsp;flag&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2011&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Subject B is flag = 0 because they do not have a record for 2011 and 2013.&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>Thu, 02 Nov 2023 21:43:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901310#M356200</guid>
      <dc:creator>joreoh</dc:creator>
      <dc:date>2023-11-02T21:43:30Z</dc:date>
    </item>
    <item>
      <title>Re: Select subjects who have records between specified range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901321#M356207</link>
      <description>&lt;P&gt;For given data and output, this code works. And might need modification depending on unspecified requirements;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Subject $ start_yr end_yr  yr;
cards; 
A  2015  2017  2015
A  2015  2017  2016
A  2015  2017  2017
B  2011  2016  2012
B  2011  2016  2014
B  2011  2016  2015
B  2011  2016  2016
;
proc print; run; 

data have1;
	set have (drop=yr);
	by subject;
	do year=start_yr to end_yr;
	output;
	end; 
run;  

proc sort data=have1 noduprecs; by _all_ ; run; 

data want;
	merge have have1;
	by subject start_yr end_yr;
	retain flag;
	if yr ne year then flag=0;
	if flag eq . then flag=1; 
	if last.subject;&lt;BR /&gt;    drop year;
proc print; run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Nov 2023 01:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901321#M356207</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-11-03T01:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: Select subjects who have records between specified range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901322#M356208</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Subject  $   start_yr            end_yr    yr;
cards;
A                 2015                 2017        2015
A                 2015                 2017        2016   
A                 2015                 2017        2017
B                 2011                  2016        2012 
B                 2011                  2016        2014 
B                 2011                  2016        2015 
B                 2011                  2016        2016
;

proc sql;
create table want as
select Subject ,start_yr,end_yr,(start_yr=min(yr) and max(yr)=end_yr and count(distinct yr)=end_yr-start_yr+1) as flag
 from have
  group by Subject ,start_yr,end_yr;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Nov 2023 01:45:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901322#M356208</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-11-03T01:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: Select subjects who have records between specified range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901328#M356214</link>
      <description>&lt;P&gt;You can do it with COUNT(DISTINCT YR).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Subject $ start_yr end_yr yr;
cards; 
A  2015  2017  2015
A  2015  2017  2016
A  2015  2017  2017
B  2011  2016  2012
B  2011  2016  2014
B  2011  2016  2015
B  2011  2016  2016
;

proc sql;
  create table want as
  select subject,start_yr,end_yr
       , end_yr - start_yr + 1 as years
       , count(*) as nobs
       , count(distinct yr) as nyears
       , count(distinct yr) = (end_yr-start_yr+1) as flag
  from have
  group by 1,2,3
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    Subject    start_yr    end_yr    years    nobs    nyears    flag

 1        A         2015       2017       3        3        3        1
 2        B         2011       2016       6        4        4        0

&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Nov 2023 03:06:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901328#M356214</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-11-03T03:06:01Z</dc:date>
    </item>
    <item>
      <title>Re: Select subjects who have records between specified range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901329#M356215</link>
      <description>Tom,&lt;BR /&gt;You didn't take int account of  max and min of YR for start_yr and end_yr .</description>
      <pubDate>Fri, 03 Nov 2023 03:10:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901329#M356215</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-11-03T03:10:27Z</dc:date>
    </item>
    <item>
      <title>Re: Select subjects who have records between specified range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901334#M356216</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Tom,&lt;BR /&gt;You didn't take int account of max and min of YR for start_yr and end_yr .&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That was not part of the problem.&amp;nbsp; The start and end years are part of the INPUT, not something that needed calculation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Nov 2023 03:24:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901334#M356216</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-11-03T03:24:29Z</dc:date>
    </item>
    <item>
      <title>Re: Select subjects who have records between specified range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901348#M356222</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by subject start_yr end_yr;
array f {2000:2099} _temporary_;
if first.end_yr
then do flag = 2000 to 2099;
  f{flag} = 0;
end;
f{yr} = 1;
if last.end_yr;
flag = 1;
do yr = start_yr to end_yr;
  if f{yr} = 0 then flag = 0;
end;
drop yr;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The BY will also take care if there are more than one start_yr/end_yr combinations per subject.&lt;/P&gt;
&lt;P&gt;Code is untested, posted from my tablet.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Nov 2023 09:51:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901348#M356222</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-11-03T09:51:53Z</dc:date>
    </item>
    <item>
      <title>Re: Select subjects who have records between specified range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901357#M356223</link>
      <description>&lt;P&gt;Some aspects need clarification:&lt;/P&gt;
&lt;P&gt;- Are start_yr and end_yr constant for a subject?&lt;/P&gt;
&lt;P&gt;- Is the combination of Subject and yr unique?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the answer to both questions is yes, try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have(keep= Subject start_yr end_yr);
   by Subject;

   length n_start 8;
   retain n_start;

   if first.Subject then do;
      n_start = _n_;
   end;

   if last.Subject then do;
      flag = (_n_ - n_start) = end_yr - start_yr;
      output;
   end;

   drop n_start;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Nov 2023 10:17:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-subjects-who-have-records-between-specified-range/m-p/901357#M356223</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-11-03T10:17:03Z</dc:date>
    </item>
  </channel>
</rss>

