<?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 rows with condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905661#M357693</link>
    <description>Hi!&lt;BR /&gt;The reason I expected only the first 5 rows in the output is that for id 1, their "type" is not missing for year 2007, 2008, 2009, 2010 and 2011.</description>
    <pubDate>Fri, 01 Dec 2023 18:12:26 GMT</pubDate>
    <dc:creator>zihdonv19</dc:creator>
    <dc:date>2023-12-01T18:12:26Z</dc:date>
    <item>
      <title>select rows with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905598#M357682</link>
      <description>&lt;P&gt;I have data below&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id 	type	visit_date :yymmdd10.;
   format visit_date yymmdd10.;
datalines;
1 	1 	2007-01-01
1 	2 	2008-01-02
1 	2 	2009-01-03
1 	3 	2010-01-04
1 	5 	2011-01-05
2 	1 	2008-01-01
2 	2 	2008-01-02
2 	2 	2009-01-03
2 	3 	2010-01-04
2 	5 	2011-01-05
3 	2 	2007-01-09
3 	. 	2008-01-10
3 	2 	2009-01-11
3 	. 	2010-01-12
4 	. 	2007-01-10
4 	3 	2008-01-11
4 	1 	2009-01-12
5 	2 	2007-01-11
5 	2 	2008-01-12
5 	2 	2009-01-10
5 	2 	2010-01-11
6 	2 	2008-01-12
6 	1 	2009-01-10
6 	. 	2010-01-11
;
proc sort data=have; by id visit_date ;run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to select rows for the id, whose id have non-missing "type" for each "visit_year" of 2007-2011.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, for the dataset above, I would expect I will get row 1-5.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 15:25:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905598#M357682</guid>
      <dc:creator>zihdonv19</dc:creator>
      <dc:date>2023-12-01T15:25:22Z</dc:date>
    </item>
    <item>
      <title>Re: select rows with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905616#M357689</link>
      <description>&lt;P&gt;If you need to see only rows 1-5 in the output, please explain further showing the expected output data as well.&amp;nbsp;&lt;BR /&gt;What I see from your data is each visit (visit_date) is unique per id. If you select only non missing TYPE (where TYPE ^=. ) then you get more observation than 5.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 16:45:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905616#M357689</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-12-01T16:45:14Z</dc:date>
    </item>
    <item>
      <title>Re: select rows with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905661#M357693</link>
      <description>Hi!&lt;BR /&gt;The reason I expected only the first 5 rows in the output is that for id 1, their "type" is not missing for year 2007, 2008, 2009, 2010 and 2011.</description>
      <pubDate>Fri, 01 Dec 2023 18:12:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905661#M357693</guid>
      <dc:creator>zihdonv19</dc:creator>
      <dc:date>2023-12-01T18:12:26Z</dc:date>
    </item>
    <item>
      <title>Re: select rows with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905666#M357695</link>
      <description>&lt;P&gt;Assuming that your data, when 2007 and 2011 years present, will have all years of 2007 -2011 per ID:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table want as
		select*
			from have
		         where type ne . and id in (select id from have 
										        group by id having min(year(visit_date)) eq 2007 and max(year(visit_date)) eq 2011);
quit; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Dec 2023 19:11:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905666#M357695</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-12-01T19:11:12Z</dc:date>
    </item>
    <item>
      <title>Re: select rows with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905705#M357698</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
array _y{2007:2011};
do until (last.id);
  set have;
  by id;
  _y{year(visit_date)} = 1;
end;
do until (last.id);
  set have;
  by id;
  if sum(of _y:) = 5 then output;
end;
drop _y:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Dec 2023 20:51:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905705#M357698</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-12-01T20:51:11Z</dc:date>
    </item>
    <item>
      <title>Re: select rows with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905826#M357721</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id 	type	visit_date :yymmdd10.;
   format visit_date yymmdd10.;
datalines;
1 	1 	2007-01-01
1 	2 	2008-01-02
1 	2 	2009-01-03
1 	3 	2010-01-04
1 	5 	2011-01-05
2 	1 	2008-01-01
2 	2 	2008-01-02
2 	2 	2009-01-03
2 	3 	2010-01-04
2 	5 	2011-01-05
3 	2 	2007-01-09
3 	. 	2008-01-10
3 	2 	2009-01-11
3 	. 	2010-01-12
4 	. 	2007-01-10
4 	3 	2008-01-11
4 	1 	2009-01-12
5 	2 	2007-01-11
5 	2 	2008-01-12
5 	2 	2009-01-10
5 	2 	2010-01-11
6 	2 	2008-01-12
6 	1 	2009-01-10
6 	. 	2010-01-11
;
proc sql;
create table want as
select *
 from have
  where type is not missing and year(visit_date) between 2007 and 2011
   group by id
    having count(distinct year(visit_date))=5;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Dec 2023 10:14:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905826#M357721</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-12-02T10:14:55Z</dc:date>
    </item>
    <item>
      <title>Re: select rows with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905873#M357751</link>
      <description>&lt;P&gt;This code reads each ID twice, as in&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;'s example.&amp;nbsp; The first pass reads only non-missing TYPE values and builds a profile of years encountered.&amp;nbsp; But the subsetting IF allows output only during second pass, if the profile is all 1's.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input id 	type	visit_date :yymmdd10.;
   format visit_date yymmdd10.;
datalines;
1 	1 	2007-01-01
1 	2 	2008-01-02
1 	2 	2009-01-03
1 	3 	2010-01-04
1 	5 	2011-01-05
2 	1 	2008-01-01
2 	2 	2008-01-02
2 	2 	2009-01-03
2 	3 	2010-01-04
2 	5 	2011-01-05
3 	2 	2007-01-09
3 	. 	2008-01-10
3 	2 	2009-01-11
3 	. 	2010-01-12
4 	. 	2007-01-10
4 	3 	2008-01-11
4 	1 	2009-01-12
5 	2 	2007-01-11
5 	2 	2008-01-12
5 	2 	2009-01-10
5 	2 	2010-01-11
6 	2 	2008-01-12
6 	1 	2009-01-10
6 	. 	2010-01-11
6 	9 	2013-01-11
run;

data want;
  set have (where=(missing(type)=0) in=firstpass)
      have (in=secondpass);
  by id;

  array years {2007:2011} _temporary_;
  if first.id then call missing(of years{*});

  if firstpass then years{year(visit_date)}=1;

  if secondpass=1 and sum(of years{*})=5;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code assumes that all the data are in the desired years (2007-2011).&amp;nbsp; If earlier or later visit_dates are possible then change the "where" option for the firstpass as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  set have (where=(missing(type)=0 and '01jan2007'd&amp;lt;=visit_date&amp;lt;='31dec2011'd) in=firstpass)
      have (in=secondpass);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Dec 2023 05:42:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905873#M357751</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-12-03T05:42:24Z</dc:date>
    </item>
    <item>
      <title>Re: select rows with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905882#M357754</link>
      <description>&lt;P&gt;To re-formulate your requirement:&lt;BR /&gt;I want to select rows for the id, whose id have &lt;STRONG&gt;NEVER&lt;/STRONG&gt; a missing "type" for "visit_year" between 2007 to 2011 &lt;STRONG&gt;AND for which there is at least one row per year.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I assume there could also be multiple visit dates (rows) for an ID in a single year.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below code basically using what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;proposed with some tweaks to check the type variable and ensuring that there can't be an out of array subscript condition. I've also amended your sample data slightly to make it a bit more representative for how I assume your real data looks like (=multiple rows per year for an id - of which one row could have a missing type).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dsd truncover;
  input id  type visit_date :yymmdd10.;
  format visit_date yymmdd10.;
  datalines;
1,1,2007-01-01
1,1,2007-05-01
1,2,2008-01-02
1,2,2009-01-03
1,3,2010-01-04
1,5,2011-01-05
9,1,2007-01-01
9,1,2007-05-01
9,.,2007-06-01
9,2,2008-01-02
9,2,2009-01-03
9,3,2010-01-04
9,5,2011-01-05
2,1,2008-01-01
2,2,2008-01-02
2,2,2009-01-03
2,3,2010-01-04
2,5,2011-01-05
2,5,2012-01-05
3,2,2007-01-09
3,.,2008-01-10
3,2,2009-01-11
3,.,2010-01-12
3,.,2011-01-12
4,.,2007-01-10
4,3,2008-01-11
4,1,2009-01-12
5,2,2007-01-11
5,2,2008-01-12
5,2,2009-01-10
5,2,2010-01-11
6,2,2008-01-12
6,1,2009-01-10
6,.,2010-01-11
;

proc sort data=have;
  by id;
run;

data want;
  array _y{2007:2011};
  call missing(of _y[*]);
  do until (last.id);
    set have;
    by id;
    if 2007&amp;lt;=year(visit_date)&amp;lt;=2011 then
      do;
        if missing(type) then _y{year(visit_date)} = 0;
        else _y{year(visit_date)} = _y{year(visit_date)} ne 0;
      end;
  end;
  do until (last.id);
    set have;
    by id;
    if sum(of _y:) = 5 then output;
  end;
  drop _y:;
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Dec 2023 08:38:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/905882#M357754</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-12-03T08:38:31Z</dc:date>
    </item>
    <item>
      <title>Re: select rows with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/906407#M357906</link>
      <description>&lt;P&gt;Hi Patrick,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Indeed, you have a valid point! I didn't explain myself clearly before I asked this question, but you made me rethink what kind of data I really have.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;May I ask a follow-up question? What if I want the year range to be &lt;STRONG&gt;the first year of visit to 4 years after the first year of visit&lt;/STRONG&gt;, instead of a fixed range 2007-2011? For example, for id1 the year range I want to look at should be 2007-2011, id2 2008-2012, id3 2007-2011, id4 2007-2011 etc.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 09:02:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/906407#M357906</guid>
      <dc:creator>zihdonv19</dc:creator>
      <dc:date>2023-12-06T09:02:04Z</dc:date>
    </item>
    <item>
      <title>Re: select rows with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/906413#M357909</link>
      <description>&lt;P&gt;Code like below should work. I've now also added parameter &amp;amp;n_years (&lt;CODE class=" language-sas"&gt;%let n_years=5&lt;/CODE&gt;) which lets you select in a single place how many years you want to include into the selection window.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
  by id visit_date;
run;

%let n_years=5;
data want;
  array _y{&amp;amp;n_years};
  call missing(of _y[*]);
  do until (last.id);
    set have;
    by id visit_date;

    visit_year=year(visit_date);
    if first.id then _first_year=visit_year;

    _ind=visit_year - _first_year +1;

    if 1&amp;lt;=_ind&amp;lt;=&amp;amp;n_years then
      do;
        if missing(type) then _y{_ind} = 0;
        else _y{_ind} = _y{_ind} ne 0;
      end;
  end;
  do until (last.id);
    set have;
    by id visit_date;
    if sum(of _y:) = &amp;amp;n_years then output;
  end;
  drop _:;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1701863833971.png" style="width: 289px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90855i01FC6DAE0517C2C8/image-dimensions/289x340?v=v2" width="289" height="340" role="button" title="Patrick_0-1701863833971.png" alt="Patrick_0-1701863833971.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 11:58:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/906413#M357909</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-12-06T11:58:25Z</dc:date>
    </item>
    <item>
      <title>Re: select rows with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/906446#M357925</link>
      <description>&lt;P&gt;thanks! this is magic &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 14:06:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-with-condition/m-p/906446#M357925</guid>
      <dc:creator>zihdonv19</dc:creator>
      <dc:date>2023-12-06T14:06:49Z</dc:date>
    </item>
  </channel>
</rss>

