BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mitrakos
Obsidian | Level 7

Hello!

I am trying to merge two datasets together that have the same variable names, but different observations. I would like to summate one of the columns. Example is given below. 

One dataset:

date1COUNTPERCENT
19W4244
19W4322
19W4466
19W453

3

 

Second dataset:

date1COUNTPERCENT
19W4311.041
19W4455.20
19W4566.25
19W4600

 

I would like to combine them so that the resulting dataset adds the COUNT column as so: 

date1COUNT
19W424
19W433
19W4411
19W459
19W460

 

The code I'm currently using merges the two datasets but doesn't add the COUNT column. I suspect my count=count+count portion does not do anything and I'm missing something. Is anyone able to lend a hand? 

data merging;
	merge alldates dash.onezerotwo;
	by date1;
	output;
	count=count+count;
run;

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
2 REPLIES 2
Kurt_Bremser
Super User

First, combine the datasets, then run proc summary:

data all;
set
  one
  two
;
by date1;
run;

proc summary data=all;
by date1;
var count;
output out=want sum()=;
run;
ballardw
Super User

@mitrakos wrote:

Hello!

I am trying to merge two datasets together that have the same variable names, but different observations. I would like to summate one of the columns. Example is given below. 

One dataset:

date1 COUNT PERCENT
19W42 4 4
19W43 2 2
19W44 6 6
19W45 3

3

 

Second dataset:

date1 COUNT PERCENT
19W43 1 1.041
19W44 5 5.20
19W45 6 6.25
19W46 0 0

 

I would like to combine them so that the resulting dataset adds the COUNT column as so: 

date1 COUNT
19W42 4
19W43 3
19W44 11
19W45 9
19W46 0

 

The code I'm currently using merges the two datasets but doesn't add the COUNT column. I suspect my count=count+count portion does not do anything and I'm missing something. Is anyone able to lend a hand? 

data merging;
	merge alldates dash.onezerotwo;
	by date1;
	output;
	count=count+count;
run;

Thank you!


It can be done but you have three strikes to over come. First is merge yields only get one resulting variable when both sets contribute like named variables other than the BY variable. The second is that some would have missing count and when you use + with missing the result is missing. Third is that with an OUTPUT statement before the sum step the summed result is not sent to the output data set.

 

data merging;
	merge alldates dash.onezerotwo (rename=(count=count102));
	by date1;

	newcount=sum(count,count102);
run;

I created an entirely new variable so you could check the results. After verifying this works then change newcount to count and drop cout102.

Possible but if you have 20 of these sets to combine you'll find that @Kurt_Bremser's will be much simpler to code.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1586 views
  • 0 likes
  • 3 in conversation