DATA Step, Macro, Functions and more

selecting common observation based on all variable names with source dataset name

Accepted Solution Solved
Reply
Contributor
Posts: 60
Accepted Solution

selecting common observation based on all variable names with source dataset name

Hello,

 

data ds1;

input var1 var2 var3 var4 var5;

datalines;

1 2 3 4 5

6 7 8 9 10

;

run;

 

data ds2;

input var1 var2 var3 var4 var5;

datalines;

11 12 13 14 15

1 2 3 4 5

;

run;

 

data want [[common observation from ds1 & ds2, with their source table] ] should be look like : 

 

var1 var2 var3 var4 var5 DS_Name

1       2      3      4       5     ds1

1       2      3      4       5     ds2

 

Thanks in advance. 

 


Accepted Solutions
Solution
‎06-19-2017 05:52 AM
PROC Star
Posts: 552

Re: selecting common observation based on all variable names with source dataset name

data ds1;
input var1 var2 var3 var4 var5;
datalines;
1 2 3 4 5
6 7 8 9 10
;

data ds2;
input var1 var2 var3 var4 var5;
datalines;
11 12 13 14 15
1 2 3 4 5
;

proc sort data = ds1;
	by var1-var5;
run;

proc sort data = ds2;
	by var1-var5;
run;

%let d1 = ds1;
%let d2 = ds2;

data want;
	merge &d1(in=in1) &d2(in=in2);
	by var1-var5;
	if in1 & in2 then do;
		DS_NAME = "&d1"; output;
		DS_NAME = "&d2"; output;
	end;
run;

View solution in original post


All Replies
PROC Star
Posts: 552

Re: selecting common observation based on all variable names with source dataset name

What is the purpose of DS_NAME and having the same observation twice, but with a different DS_NAME, that you already know, since you provided the datasets yourself?

Contributor
Posts: 60

Re: selecting common observation based on all variable names with source dataset name

I want to show duplicate records with their source_name 

Solution
‎06-19-2017 05:52 AM
PROC Star
Posts: 552

Re: selecting common observation based on all variable names with source dataset name

data ds1;
input var1 var2 var3 var4 var5;
datalines;
1 2 3 4 5
6 7 8 9 10
;

data ds2;
input var1 var2 var3 var4 var5;
datalines;
11 12 13 14 15
1 2 3 4 5
;

proc sort data = ds1;
	by var1-var5;
run;

proc sort data = ds2;
	by var1-var5;
run;

%let d1 = ds1;
%let d2 = ds2;

data want;
	merge &d1(in=in1) &d2(in=in2);
	by var1-var5;
	if in1 & in2 then do;
		DS_NAME = "&d1"; output;
		DS_NAME = "&d2"; output;
	end;
run;
Contributor
Posts: 60

Re: selecting common observation based on all variable names with source dataset name

If I want duplicate based on var1-var4, then var5 is coming from second dataset only.. is there any way to get correct value :

 

output is coming like(var5 is coming from ds2 in both the dataset,which should 6 in first observation) : 

 

SAS Output

Obs var1 var2 var3 var4 var5 DS_NAME 1 2
12347ds1
12347ds2

data ds1;
input var1 var2 var3 var4 var5;
datalines;
1 2 3 4 6
6 7 8 9 10
;

data ds2;
input var1 var2 var3 var4 var5;
datalines;
11 12 13 14 15
1 2 3 4 7
;

proc sort data = ds1;
by var1-var4;
run;

proc sort data = ds2;
by var1-var4;
run;

%let d1 = ds1;
%let d2 = ds2;

data want;
merge &d1(in=in1) &d2(in=in2);
by var1-var4;
if in1 & in2 then do;
DS_NAME = "&d1"; output;
DS_NAME = "&d2"; output;
end;
run;

proc print data=want;
run;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 116 views
  • 0 likes
  • 2 in conversation