BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
m1986MM
Obsidian | Level 7

Hello everyone,

I want to merge two datasets, having a common variable. However, I don't want them to merge completely. For example, the first dataset is like this:

Table 1
StudentIDDate Test_Score1TEST_score2test_score3
*****

The other dataset contains these variables:

Table 2
StudentIDField_of_interestUndergradute_fieldMaster_field
$$$$

The studentID is the common variable. But I want to add just one variable (Field_of_interest) from table 2 to table one. Is there any way to not merge two tables completely?

I appreciate any suggestion.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

The simplest way is with proc SQL :

proc sql;

create table Table_3 as

select Table_1.*, Table_2.Field_of_interest

from Table_1 left join Table_2

  on Table_1.StudentID=Table_2.StudentID;

quit;

PG

PG

View solution in original post

8 REPLIES 8
PGStats
Opal | Level 21

The simplest way is with proc SQL :

proc sql;

create table Table_3 as

select Table_1.*, Table_2.Field_of_interest

from Table_1 left join Table_2

  on Table_1.StudentID=Table_2.StudentID;

quit;

PG

PG
m1986MM
Obsidian | Level 7

Thanks. But this code will merge all the datasets together.

Reeza
Super User

Then you haven't tried it...

m1986MM
Obsidian | Level 7

Yes, that's right.

m1986MM
Obsidian | Level 7

Hi again,

I have a question about the mechanism of this code and appreciate if you can help me. When I merge the datasets by using proc sql, for example in this specific code, those observations in table 1 with the missing value of table 2 (Field_of _interest) would be deleted or not?

Thanks.

PGStats
Opal | Level 21

All observations from Table 1 will be included, even those without a matching record in Table 2. If you want to keep only matching records, replace left join with inner join.

PG

PG
naveen20jan
Obsidian | Level 7

Hi , you can use below part of  code too.

data want  ;
merge t1  (in = a)
      t2 (in = b  keep = StudentID Field_of_interest );
by StudentID ;
if a ;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 1611 views
  • 7 likes
  • 4 in conversation