DATA Step, Macro, Functions and more

How to compare 2 datasets observations and execute a list of command based on the result

Reply
Contributor
Posts: 43

How to compare 2 datasets observations and execute a list of command based on the result

Hi

 

I have 2 data sets (Current and Prev month).

 

I need to compare the 2 datasets no. of observations and if the current month data set has more no. of records then I need to run or call a macro?

 

How do I achieve that?

 

 

Frequent Contributor
Posts: 100

Re: How to compare 2 datasets observations and execute a list of command based on the result

Try this....

 

proc sql noprint;
  select nobs into: curr from dictionary.tables
  where upcase(libname)="MYLIB" and upcase(memname)='MY_CURR';

  select nobs into: PREV from dictionary.tables
  where upcase(libname)="MYLIB" and upcase(memname)='MY_PREV';
QUIT;

%macro HasMore;
%if %eval(&prev<curr) %then %do;
  %MyMacro;
%end;

%mend

%HasMore;

Hope this helps.

PROC Star
Posts: 1,760

Re: How to compare 2 datasets observations and execute a list of command based on the result

Like this?

data _null_;
  if NOBS1 ne NOBS2 then call execute('%mymacro;');
  set TABLE1 nobs=NOBS1;
  set TABLE2 nobs=NOBS2;
  stop;
run;

Simple since there is no macro language, and fast since we don't use the dictionary.

 

Contributor
Posts: 43

Re: How to compare 2 datasets observations and execute a list of command based on the result

[ Edited ]

Thanks Chris it did the job for me....

 

Any specific reason why the if logic before reading the data sets?

Super Contributor
Posts: 271

Re: How to compare 2 datasets observations and execute a list of command based on the result

nobs are obtained during compile time so they can be used before SET statement.

PROC Star
Posts: 1,760

Re: How to compare 2 datasets observations and execute a list of command based on the result

[ Edited ]

If you put it after the SET statements and one of the data sets is empty, it will never be executed.

Contributor
Posts: 29

Re: How to compare 2 datasets observations and execute a list of command based on the result

start using Proc Compare ..

Ask a Question
Discussion stats
  • 6 replies
  • 261 views
  • 6 likes
  • 5 in conversation