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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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