<?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: Vertically Stack Dataset and Only Keeping Rows with Matching IDs in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640505#M190803</link>
    <description>&lt;P&gt;You can use a double dow loop. One to count and the other to re-read and either output or not.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do _N_=1 by 1 until(last.id);
     set table1-table5;
     by id year;
  end;
  do until(last.id);
    set table1-table5;
    by id year;
    if _n_=5 then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 16 Apr 2020 18:11:03 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-04-16T18:11:03Z</dc:date>
    <item>
      <title>Vertically Stack Dataset and Only Keeping Rows with Matching IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640324#M190700</link>
      <description>&lt;P&gt;I am looking to stack 5 years worth of data: each year is a different table and they all have the same columns/variables. In the final dataset, I only want to keep observations for subjects who were there for the 5 years.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EXAMPLE:&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table 1:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;First Name&lt;/TD&gt;&lt;TD&gt;Count&lt;/TD&gt;&lt;TD&gt;Year&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&g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t;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;Gamma&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;2015&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;Gamma&lt;/TD&gt;&lt;TD&gt;17&lt;/TD&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;Gamma&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2017&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 08:44:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640324#M190700</guid>
      <dc:creator>bcp2122</dc:creator>
      <dc:date>2020-04-16T08:44:40Z</dc:date>
    </item>
    <item>
      <title>Re: Vertically Stack Dataset and Only Keeping Rows with Matching IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640335#M190710</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/293868"&gt;@bcp2122&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an approach to achieve this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
	infile datalines dlm="09"x;
	input ID First_Name $	Count	Year;
	datalines;
1	Alpha	5	2015
2	Beta	12	2015
3	Gamma	4	2015
;
run;
 
data table2;
	infile datalines dlm="09"x;
	input ID First_Name $	Count	Year;
	datalines;
1	Alpha	32	2016
3	Gamma	17	2016
4	Delta	2	2016
;
run;

data table3;
	infile datalines dlm="09"x;
	input ID First_Name $	Count	Year;
	datalines;
1	Alpha	14	2017
2	Beta	3	2017
3	Gamma	1	2017
;
run;

data want;
	set table1 table2 table3;
run;

proc sql;
	select *
	from want
	group by id 
	having count(id) = (select count(distinct year) from want);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 08:52:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640335#M190710</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-04-16T08:52:45Z</dc:date>
    </item>
    <item>
      <title>Re: Vertically Stack Dataset and Only Keeping Rows with Matching IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640339#M190714</link>
      <description>&lt;P&gt;HI,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;here is another approach (double DoW-loop + hash):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
	infile datalines dlm=" ";
	input ID First_Name : $	Count	Year;
	datalines;
1 Alpha 5 2015
2 Beta 12 2015
3 Gamma 4 2015
;
run;
 
data table2;
	infile datalines dlm=" ";
	input ID First_Name : $	Count	Year;
	datalines;
1 Alpha 32 2016
3 Gamma 17 2016
4 Delta 2 2016
;
run;

data table3;
	infile datalines dlm=" ";
	input ID First_Name : $	Count	Year;
	datalines;
1 Alpha 14 2017
2 Beta 3 2017
3 Gamma 1 2017
;
run;

%let n = 3; /* the number of years */
data want;
  declare hash FirstName();
  FirstName.defineKey("First_Name");
  FirstName.defineData("_IORC_");
  FirstName.defineDone();

  do until(EOF);
	 set table1 table2 table3 end=EOF;
   if 0 ne FirstName.find() then _IORC_ = 1;
                            else _IORC_ + 1;  
   _N_ = FirstName.replace();
  end;

  EOF = 0;
  do until(EOF);
	 set table1 table2 table3 end=EOF;
   _N_ = FirstName.find(); 
   if _IORC_ =&amp;gt; &amp;amp;n. then output;
  end;
  
  stop;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 09:09:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640339#M190714</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-04-16T09:09:42Z</dc:date>
    </item>
    <item>
      <title>Re: Vertically Stack Dataset and Only Keeping Rows with Matching IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640340#M190715</link>
      <description>&lt;P&gt;For the pure fun of it, using transpose and merge:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro process;
%do i = 1 %to 3;

proc sort data=table&amp;amp;i.;
by first_name;
run;

proc transpose data=table&amp;amp;i. out=trans&amp;amp;i. (drop=_name_) prefix=_;
var count;
id year;
by first_name;
run;

%end;

data all;
merge
%do i = 1 %to 3;
  trans&amp;amp;i. (in=in&amp;amp;i.)
%end;
;
by first_name;
if
%do i = 1 %to 3;
  %if &amp;amp;i. ne 1 %then and ;
  in&amp;amp;i.
%end;
;
run;

proc transpose data=all out=pre_want (rename=(col1=count));
by first_name;
var _:;
run;

data want;
set pre_want;
year = input(substr(_name_,2),best.);
drop _name_;
run;
%mend;

%process&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 09:14:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640340#M190715</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-16T09:14:41Z</dc:date>
    </item>
    <item>
      <title>Re: Vertically Stack Dataset and Only Keeping Rows with Matching IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640343#M190718</link>
      <description>&lt;P&gt;Kurt,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;following your approach&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  set table1 table2 table3;
run;

proc sort data = have ;
  by ID First_Name Year;
run;

proc transpose data = have out = haveT(drop = _name_) prefix=y_;
  by ID First_Name;
  id year;
  var Count;
run;

data want;
  set haveT;
  array y_[*] y_:;
  if not(. in y_);
  do _N_ = lbound(y_) to hbound(y_);
     year = input(compress(vname(y_[_N_]),,"kd"), best32.);
     Count = y_[_N_];
     output;
  end;
  keep ID First_Name Year Count;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 09:35:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640343#M190718</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-04-16T09:35:26Z</dc:date>
    </item>
    <item>
      <title>Re: Vertically Stack Dataset and Only Keeping Rows with Matching IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640370#M190731</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
	infile datalines expandtabs truncover;
	input ID First_Name $	Count	Year;
	datalines;
1	Alpha	5	2015
2	Beta	12	2015
3	Gamma	4	2015
;
run;
 
data table2;
	infile datalines expandtabs truncover;
	input ID First_Name $	Count	Year;
	datalines;
1	Alpha	32	2016
3	Gamma	17	2016
4	Delta	2	2016
;
run;

data table3;
	infile datalines expandtabs truncover;
	input ID First_Name $	Count	Year;
	datalines;
1	Alpha	14	2017
2	Beta	3	2017
3	Gamma	1	2017
;
run;
proc sql;
create table want as
select * from
(
select * from table1
union all corr
select * from table2
union all corr
select * from table3
)
where id in
(
select id from table1
intersect
select id from table2
intersect
select id from table3
)
order by 1,4;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Apr 2020 10:50:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640370#M190731</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-04-16T10:50:44Z</dc:date>
    </item>
    <item>
      <title>Re: Vertically Stack Dataset and Only Keeping Rows with Matching IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640505#M190803</link>
      <description>&lt;P&gt;You can use a double dow loop. One to count and the other to re-read and either output or not.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do _N_=1 by 1 until(last.id);
     set table1-table5;
     by id year;
  end;
  do until(last.id);
    set table1-table5;
    by id year;
    if _n_=5 then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Apr 2020 18:11:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertically-Stack-Dataset-and-Only-Keeping-Rows-with-Matching-IDs/m-p/640505#M190803</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-16T18:11:03Z</dc:date>
    </item>
  </channel>
</rss>

