Help using Base SAS procedures

How to merge part of a dataset with another dataset

Accepted Solution Solved
Reply
Contributor
Posts: 65
Accepted Solution

How to merge part of a dataset with another dataset

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.


Accepted Solutions
Solution
‎11-24-2014 09:48 PM
Respected Advisor
Posts: 4,919

Re: How to merge part of a dataset with another dataset

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


All Replies
Solution
‎11-24-2014 09:48 PM
Respected Advisor
Posts: 4,919

Re: How to merge part of a dataset with another dataset

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
Contributor
Posts: 65

Re: How to merge part of a dataset with another dataset

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

Super User
Posts: 19,770

Re: How to merge part of a dataset with another dataset

Then you haven't tried it...

Contributor
Posts: 65

Re: How to merge part of a dataset with another dataset

Yes, that's right.

Contributor
Posts: 65

Re: How to merge part of a dataset with another dataset

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.

Respected Advisor
Posts: 4,919

Re: How to merge part of a dataset with another dataset

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
Contributor
Posts: 65

Re: How to merge part of a dataset with another dataset

Thanks.

Contributor
Posts: 30

Re: How to merge part of a dataset with another dataset

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;

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 259 views
  • 7 likes
  • 4 in conversation