BookmarkSubscribeRSS Feed
Justin9
Obsidian | Level 7

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?

 

Justin9_0-1624837358044.png

 

 

3 REPLIES 3
tarheel13
Rhodochrosite | Level 12
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;
ballardw
Super User

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.

 

Ksharp
Super User
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;

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 585 views
  • 4 likes
  • 4 in conversation