DATA Step, Macro, Functions and more

What type of join?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 142
Accepted Solution

What type of join?

I need to join several tables (3 fiscal months...FM01, FM02, FM03) selecting specific variables that are the same in all the tables and combining all the observations within those tables.  

 

proc sql;

create table FM123 as

select patssn, patsex,patrace,patage

from FM01, FM01, FM03  *** not sure what typle of join or where to type the join statement?!

order by patssn;

quit;

 

 


Accepted Solutions
Solution
‎11-03-2015 02:36 PM
Super User
Posts: 19,815

Re: What type of join?

Do you need them side by side or stacked? I would think a union all would work if stacked.

select * from fm01
union all
select * from fm02

If side by side you need a joining variable and you'll need to rename all the variables from each dataset since you can't have multiple variable with the same name.

View solution in original post


All Replies
Solution
‎11-03-2015 02:36 PM
Super User
Posts: 19,815

Re: What type of join?

Do you need them side by side or stacked? I would think a union all would work if stacked.

select * from fm01
union all
select * from fm02

If side by side you need a joining variable and you'll need to rename all the variables from each dataset since you can't have multiple variable with the same name.
Frequent Contributor
Posts: 142

Re: What type of join?

That  worked!  Thank you!!

Frequent Contributor
Posts: 142

Re: What type of join?

Union All does work, however Is there a way to join/ merge several tables without having select all the variables from every table I am combining?  Say I'm working with 30 variables (out of 150) and I only want to select/ type them once because they are going to be the same variables in every table (stacked observations).  Just want to know if there is an easier way to join/ merge the tables. 

Contributor ndp
Contributor
Posts: 61

Re: What type of join?

First you need to understand that joining (commonly known as merging) and stacking tables are 2 different operations.

1) If you are joining them (meaning side by side) then as previously mentioned you need to have one or more id variables to join by and you need to rename varibles that have same name in more than one dataset.

2) If you are stacking them (meaning one after other) then you do not need one or more id variables and you do not need to rename variables that have same name and are of same type.

 

If you do not want to type out variables again and again you can put them in a macro variable eg: %let vartokeep= var1 var2 ...;

 

then if you are stacking them together you can use a simple data step:

 

data want;

 set have1(keep=&vartokeep.) have2(keep=&vartokeep) ...;

run;

Super User
Posts: 19,815

Re: What type of join?

[ Edited ]

I'm with @ndp - using a data step is easier in this case. Especially because you can use variable lists in a data step and you can't in proc sql.

data want;
set fm01 fm02 fm03;

/*some examples of keep statements*/
keep var1-var30; /*all variables numbered from var1-var30*/
keep prefix:; /*all variables that start with prefix*/
keep var1--random20; /*all variables that are between var1 and random20 if you look at the variables in a table*/

run;
Frequent Contributor
Posts: 142

Re: What type of join?

Thank you!!
🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 252 views
  • 2 likes
  • 3 in conversation