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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.