BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Madhav4114
Calcite | Level 5
Table 1.
Newkey period
1. 201701
2. 201701
3. 201701
1. 201801

Table 2
Newkey. Period
1. 201701
2. 201701
1. 201801

Compare two tables and find out accounts(newkey) which are unique or not present in another table on each period.
1 ACCEPTED SOLUTION

Accepted Solutions
MayurJadhav
Quartz | Level 8

You can try this and adjust the code according to your requirement. @Madhav4114 

 

data Table1;
	input Newkey period;
	datalines;
1 201701
2 201701
3 201701
1 201801
;

data Table2;
	input Newkey Period;
	datalines;
1 201701
2 201701
1 201801
;
run;

proc sort data=Table1;
	by Newkey;
run;

proc sort data=Table2;
	by Newkey;
run;

data in_Table1 in_Table2 in_Both;
	merge Table1(in=a) Table2(in=b);
	by Newkey;

	if a and not b then
		output in_Table1;

	if b and not a then
		output in_Table2;

	if a and b then
		output in_Both;
run;

proc print data=in_Table1;
proc print data=in_Table2;
proc print data=in_Both;
run;
Mayur Jadhav
BI Developer. Writer. Creative Educator.

SAS Blog → https://learnsascode.com
YouTube Channel: → https://www.youtube.com/@imayurj

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Try this

 

data Table1;
input Newkey period;
datalines;
1 201701
2 201701
3 201701
1 201801
;

data Table2;
input Newkey Period;
datalines;
1 201701
2 201701
1 201801
;

data notpresent;

   if _N_ = 1 then do;
      dcl hash h(dataset : 'Table2');
	  h.definekey('Newkey', 'period');
	  h.definedone();
   end;
   
   set Table1;
   
   if h.check();

run;
yabwon
Onyx | Level 15

1) what did you try so far? 

2) what is your expected result? 

3) if you wan us to solve your homework at least prepare test data in form we could use them (i.e., data step with datalines) 

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Madhav4114
Calcite | Level 5
Tried with fuller outer join but unsuccessful with results
MayurJadhav
Quartz | Level 8

You can try this and adjust the code according to your requirement. @Madhav4114 

 

data Table1;
	input Newkey period;
	datalines;
1 201701
2 201701
3 201701
1 201801
;

data Table2;
	input Newkey Period;
	datalines;
1 201701
2 201701
1 201801
;
run;

proc sort data=Table1;
	by Newkey;
run;

proc sort data=Table2;
	by Newkey;
run;

data in_Table1 in_Table2 in_Both;
	merge Table1(in=a) Table2(in=b);
	by Newkey;

	if a and not b then
		output in_Table1;

	if b and not a then
		output in_Table2;

	if a and b then
		output in_Both;
run;

proc print data=in_Table1;
proc print data=in_Table2;
proc print data=in_Both;
run;
Mayur Jadhav
BI Developer. Writer. Creative Educator.

SAS Blog → https://learnsascode.com
YouTube Channel: → https://www.youtube.com/@imayurj
Madhav4114
Calcite | Level 5
@MayurJadhav solution looks fine but taking a lot of time to run
P.s dataset size more than 5gb

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 939 views
  • 1 like
  • 4 in conversation