Hi, The way you have presanted your query, possible solution is as follow...I hope this works... In this example, i have created dataset containing households with sales and units...that is table_A contains hhd,sales and units for one period and table_B contains hhd,sales and units for second period...and compare both period households and assign your mentioned conditions...In my code, HHD is unique key in both the datasets... data table_A; input hhd $ sales units; cards; A 10 4 B 20 5 C 30 2 D 40 3 F 60 1 ; data table_B; input hhd $ sales units; cards; A 10 4 B 20 5 C 30 2 D 40 3 E 50 4 D 60 . ; proc sort data = table_A; by hhd; run; proc sort data = table_B; by hhd; run; %macro compare; proc contents data = table_A out = list_A(keep = name) noprint; run; proc contents data = table_B out = list_B(keep = name) noprint; run; proc sql noprint; select count(name) into :count_A from list_A; select count(name) into :count_B from list_B; select name into :name_A separated by " " from list_A; select name into :name_B separated by " " from list_B; quit; proc datasets nolist; modify table_B; rename %do i = 1 %to &count_B.; %scan(&name_B.,&i.) = %scan(&name_B.,&i.)_Y2 %end;; quit; proc datasets nolist; modify table_A; rename %do i = 1 %to &count_A.; %scan(&name_A.,&i.) = %scan(&name_A.,&i.)_Y1 %end;; quit; data both(drop = _temp: in_A in_B); retain hhd sales_Y1 units_Y1 sales_Y2 units_Y2 item; length item $10.; merge table_A(in = a rename = (hhd_Y1 = hhd)) table_B(in = b rename = (hhd_Y2 = hhd)); by hhd; in_A = a; in_B = b; %do i = 2 %to &count_B.; _temp&i. = %scan(&name_B.,&i.)_Y2 - %scan(&name_A.,&i.)_Y1; if (in_A = 1 and in_B = 1) and _temp&i. = 0 then item = "Equal"; if in_a = 0 and in_B = 1 then item = "New"; if in_a = 1 and in_B = 0 then item = "Deleted"; if (in_a = 1 and in_B = 1) and (_temp&i. NE 0 or _temp&i. = .) then item = "Updated"; %end; run; %mend; %compare; -Urvish
... View more