<?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: Compare number of records between two datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compare-number-of-records-between-two-datasets/m-p/833214#M329370</link>
    <description>&lt;P&gt;Are you implying that the REAL macro is actually much longer than what you are showing, and anything after this &lt;FONT face="courier new,courier"&gt;DATA _NULL_;&lt;/FONT&gt; step should not execute?&lt;/P&gt;</description>
    <pubDate>Tue, 13 Sep 2022 19:01:33 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2022-09-13T19:01:33Z</dc:date>
    <item>
      <title>Compare number of records between two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-number-of-records-between-two-datasets/m-p/833211#M329367</link>
      <description>&lt;P&gt;I would like to create a macro that I can use to compare the number of records between two datasets and if the number of records doesn't match then send an error to the log and stop execution of the rest of the submitted code.&lt;/P&gt;&lt;P&gt;The below macro works except it doesn't stop execution, I am not sure how to do that.&amp;nbsp; Also I am curious to see if anyone has a more elegant solution (I am sure there are several).&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%MACRO COMPARE_OBS(TBL1, TBL2);
	data _null_;
		if 0 then do; set &amp;amp;TBL1 nobs= obs1; set &amp;amp;TBL2 nobs= obs2; end;
		if obs1 ne obs2 then do;
			put "ERROR: &amp;amp;TBL1 and &amp;amp;TBL2 have different number of observations!";
		end;
		stop;
	run;
%MEND COMPARE_OBS;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks in advance for your time.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2022 18:52:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-number-of-records-between-two-datasets/m-p/833211#M329367</guid>
      <dc:creator>GeorgeBonanza</dc:creator>
      <dc:date>2022-09-13T18:52:15Z</dc:date>
    </item>
    <item>
      <title>Re: Compare number of records between two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-number-of-records-between-two-datasets/m-p/833214#M329370</link>
      <description>&lt;P&gt;Are you implying that the REAL macro is actually much longer than what you are showing, and anything after this &lt;FONT face="courier new,courier"&gt;DATA _NULL_;&lt;/FONT&gt; step should not execute?&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2022 19:01:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-number-of-records-between-two-datasets/m-p/833214#M329370</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-09-13T19:01:33Z</dc:date>
    </item>
    <item>
      <title>Re: Compare number of records between two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-number-of-records-between-two-datasets/m-p/833217#M329373</link>
      <description>Thank you for taking the time to respond.&amp;nbsp; What I am doing is joining two tables and it should be a 1 to 1 match.&amp;nbsp; I want to use the macro to check that the number of observations before and after the join are the same.&amp;nbsp; If they are not the same then don't do any of the subsequent steps that follow the macro call.&amp;nbsp; The rest of the steps, proc print, data step, etc., are not part of the macro.</description>
      <pubDate>Tue, 13 Sep 2022 19:08:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-number-of-records-between-two-datasets/m-p/833217#M329373</guid>
      <dc:creator>GeorgeBonanza</dc:creator>
      <dc:date>2022-09-13T19:08:52Z</dc:date>
    </item>
    <item>
      <title>Re: Compare number of records between two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-number-of-records-between-two-datasets/m-p/833222#M329378</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/100491"&gt;@GeorgeBonanza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you for taking the time to respond.&amp;nbsp; What I am doing is joining two tables and it should be a 1 to 1 match.&amp;nbsp; I want to use the macro to check that the number of observations before and after the join are the same.&amp;nbsp; If they are not the same then don't do any of the subsequent steps that follow the macro call.&amp;nbsp; The rest of the steps, proc print, data step, etc., are not part of the macro.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So you want to create a macro variable that indicts if the two tables have the same number of observations and then use that macro variable to control later steps in the program that called the macro?&amp;nbsp; So make sure the macro can return the result of its test to the calling environment.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro compare_obs(tbl1, tbl2,mvar);
%if not %symexist(&amp;amp;mvar) %then %global &amp;amp;mvar;
data _null_;
  call symputx("&amp;amp;mvar",obs1=obs2);
  stop;
  set &amp;amp;tbl1(drop=_all_) nobs= obs1;
  set &amp;amp;tbl2(drop=_all_) nobs= obs2;
run;
%mend compare_obs;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then in the calling program test the value of the macro variable to control whether or not to execute future steps.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%compare_obs(tbl1=ds1, tbl2=ds2,mvar=same)
%if &amp;amp;same %then %do;
  * other steps here ;
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Sep 2022 19:31:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-number-of-records-between-two-datasets/m-p/833222#M329378</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-13T19:31:50Z</dc:date>
    </item>
  </channel>
</rss>

