05-24-2016 09:26 PM
Good evening, everyone! I hope you can forgive me for the somewhat newbie question, but my Googling abilities have failed to confirm the exact finding for which I'm searching.
I have two datasets which need to be merged specifically by ResponseID. However, they have different variables (the second dataset was recently generated with a new and different set of variables). I had originally been hoping to simply paste the new variables to the older dataset, but as it happens, the ResponseIDs came through in a different order than in the original dataset. Is there a dataset merging function in SAS which would merge the datasets *specifically on ResponseID*. I am thinking that SAS has a merging function which would add the new variables to the existing dataset, and match all values for those variables to the order as they appear in the old dataset. As a note, the datasets have the same ResponseIDs (e.g., 1,2,3,4,5 both exist within the two datasets), but in different orders (i.e., the second dataset was produced in order 4,2,1,5,3). See examples of old dataset, new dataset, and desired dataset.
Old dataset example
|ResponseID||Variable 1||Variable 2||Variable 3||Variable 4||Variable 5|
New dataset example:
|ResponseID||Variable 6||Variable 7|
Desired dataset example (but in the actual, non example dataset, this includes 7000 datapoints)
|ResponseID||Variable 1||Variable 2||Variable 3||Variable 4||Variable 5||Variable 6||Variable 7|
Given the tremendous size of the dataset (as well as a slight time crunch), it is not feasible to execute this task manually (as in, finding each unique ResponseID and matching them to the original dataset's order, then importing the data for the new variables in that matched order).
Thanks so much for your time!
05-24-2016 10:04 PM
This is all you need.
proc sort data=new;
merge old new;
But its important to know how it works. The doco's pretty good.
05-24-2016 10:39 PM
Just a quick note on how SAS handles variables that have the same name, the get overwritten by the last dataset in the MERGE statement.
If you want to keep all variables, rename your variables in each dataset so they're unique.
Otherwise, as indicated, the BY statement will match the observations to merge by responseID
05-25-2016 08:03 AM
Thanks to you both for your prompt and comprehensive replies.
Reeza, you make a good point about ensuring that all the variable labels are unique. If I execute the code that JerryLeBreton supplied on, for example, the exemplary data set I put in the original post, will it preserve all Variables 1, 2, 3, 4, 5, 6, 7?
I'm hoping it can just add the variables to the end of the dataset, such that Variables 6 and 7 appear after 5.