BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
GeorgeBonanza
Obsidian | Level 7

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.

The below macro works except it doesn't stop execution, I am not sure how to do that.  Also I am curious to see if anyone has a more elegant solution (I am sure there are several).

%MACRO COMPARE_OBS(TBL1, TBL2);
	data _null_;
		if 0 then do; set &TBL1 nobs= obs1; set &TBL2 nobs= obs2; end;
		if obs1 ne obs2 then do;
			put "ERROR: &TBL1 and &TBL2 have different number of observations!";
		end;
		stop;
	run;
%MEND COMPARE_OBS;

Thanks in advance for your time. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@GeorgeBonanza wrote:
Thank you for taking the time to respond.  What I am doing is joining two tables and it should be a 1 to 1 match.  I want to use the macro to check that the number of observations before and after the join are the same.  If they are not the same then don't do any of the subsequent steps that follow the macro call.  The rest of the steps, proc print, data step, etc., are not part of the macro.

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?  So make sure the macro can return the result of its test to the calling environment.

%macro compare_obs(tbl1, tbl2,mvar);
%if not %symexist(&mvar) %then %global &mvar;
data _null_;
  call symputx("&mvar",obs1=obs2);
  stop;
  set &tbl1(drop=_all_) nobs= obs1;
  set &tbl2(drop=_all_) nobs= obs2;
run;
%mend compare_obs;

Then in the calling program test the value of the macro variable to control whether or not to execute future steps.

%compare_obs(tbl1=ds1, tbl2=ds2,mvar=same)
%if &same %then %do;
  * other steps here ;
%end;

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Are you implying that the REAL macro is actually much longer than what you are showing, and anything after this DATA _NULL_; step should not execute?

--
Paige Miller
GeorgeBonanza
Obsidian | Level 7
Thank you for taking the time to respond.  What I am doing is joining two tables and it should be a 1 to 1 match.  I want to use the macro to check that the number of observations before and after the join are the same.  If they are not the same then don't do any of the subsequent steps that follow the macro call.  The rest of the steps, proc print, data step, etc., are not part of the macro.
Tom
Super User Tom
Super User

@GeorgeBonanza wrote:
Thank you for taking the time to respond.  What I am doing is joining two tables and it should be a 1 to 1 match.  I want to use the macro to check that the number of observations before and after the join are the same.  If they are not the same then don't do any of the subsequent steps that follow the macro call.  The rest of the steps, proc print, data step, etc., are not part of the macro.

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?  So make sure the macro can return the result of its test to the calling environment.

%macro compare_obs(tbl1, tbl2,mvar);
%if not %symexist(&mvar) %then %global &mvar;
data _null_;
  call symputx("&mvar",obs1=obs2);
  stop;
  set &tbl1(drop=_all_) nobs= obs1;
  set &tbl2(drop=_all_) nobs= obs2;
run;
%mend compare_obs;

Then in the calling program test the value of the macro variable to control whether or not to execute future steps.

%compare_obs(tbl1=ds1, tbl2=ds2,mvar=same)
%if &same %then %do;
  * other steps here ;
%end;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 853 views
  • 1 like
  • 3 in conversation