I have a master dataset, 'Dataset_1' and I would like to add on new values from 'Dataset_2' (note that both datasets only have one variable in them, Code'), so that all of the values from 'Dataset_1' are kept and only new values from 'Dataset_2' are used to get 'Dataset_3' (i.e. all of the values in 'Dataset_3' should be unique and there should not be any duplication e.g. SE6P is in both datasets, but 'Dataset_3' should only list it once). Please can you help to provide some code that would help to achieve what I have shown in my image below?
data dataset_1;
input code $;
datalines;
SE6P
SE7P
SE8P
SE9P
SF2P
SF5P
;
run;
data dataset_2;
input code $;
datalines;
SE6P
SE5P
SE7P
SF1P
SF2P
SF3P
;
run;
proc sort data=dataset_1;
by code;
run;
proc sort data=dataset_2;
by code;
run;
data in1 in2;
merge dataset_1(in=in1) dataset_2(in=in2);
by code;
if in1 then output in1;
if in2 and not in1 then output in2;
run;
data stack;
set in1 in2;
run;
proc print data=stack;
run;
A minor recommended change on @tarheel13's solution.
Instead of merge creating two data sets use UPDATE.
data dataset_3; update dataset_1 dataset_2; by code; run;
When the master data set appears first on the update data set (which only has one record with each value of Code) then when you use update only NEW values, and a single record with those new values would be added to the data.
Update is a specialized form of the Merge statement, or the other way around depending on how you think about it. The main difference is the By varaible(s) in the first data set on the Update, which is the "master" set, cannot be repeated and you only have one other data set to update from.
data dataset_1;
input code $;
datalines;
SE6P
SE7P
SE8P
SE9P
SF2P
SF5P
;
run;
data dataset_2;
input code $;
datalines;
SE6P
SE5P
SE7P
SF1P
SF2P
SF3P
;
run;
proc sql;
create table want as
select * from dataset_1
union
select * from dataset_2;
quit;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: