BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
data table1;
input value1;
datalines;
1
2
3
5
;
run;


data table2;
input value2;
datalines;
1
2
4
;
run;


/*Scenario:1 Join columns having unique values */

/*Merge with Datastep*/

proc sort data=table1;
by value1;
run;


proc sort data=table2;
by value2;
run;



/* inner join with Datastep */
data innerjoin;
merge table1(in=a) table2(in=b);
by value1 value2;
if a and b;
proc print noobs;
run;

/* inner join with proc sql */

proc sql;
select t1.value1, t2.value2
from table1 t1 
inner join 
     table2 t2
on t1.value1=t2.value2;
quit;


/* left join with Datastep */
data leftjoin;
merge table1(in=a) table2(in=b);
by value1 value2;
if a then output;
proc print noobs;
run;


proc sql;
select t1.value1,t2.value2
from table1 t1
left join
     table2 t2
on t1.value1=t2.value2;
quit;

/* right join with Datastep */

data rightjoin;
merge table1(in=a) table2(in=b);
by value1 value2;
if b then output;
proc print noobs;
run;


/* right join proc sql */

proc sql;
select t1.value1 ,t2.value2
from table1 t1
right join 
     table2 t2
on t1.value1=t2.value2;
quit;


/* full join with Datastep */
data fulljoin;
merge table1(in=a) table2(in=b);
by value1 value2;
proc print noobs;
run;

/* full join proc sql */

proc sql;
select t1.value1,t2.value2
from table1 t1
full join
     table2 t2
on t1.value1=t2.value2;     
quit;

using data step merge for joins i am getting below error

Anandkvn_0-1684130874736.png

 

2 REPLIES 2
Oligolas
Barite | Level 11

You need to use the same 'by' variables in your dataset:

/* inner join with Datastep */
data innerjoin;
merge table1(in=a) table2(in=b rename=(value2=value1));
by value1;
if a and b;
run;
________________________

- Cheers -

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 276 views
  • 0 likes
  • 3 in conversation