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 | ||||
StudentID | Date | Test_Score1 | TEST_score2 | test_score3 |
* | * | * | * | * |
The other dataset contains these variables:
Table 2 | |||
StudentID | Field_of_interest | Undergradute_field | Master_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.
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
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
Thanks. But this code will merge all the datasets together.
Then you haven't tried it...
Yes, that's right.
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.
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
Thanks.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.