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

Hello, below is an example of my two datasets:

dataset A:

idenvwatertransportproduction
10000
21010
30001
40010

dataset B:

idenvwatertransportproduction
11000
20000
30000
41011

 

Just a background, why two datasets have the same column names is because I split my original data into two subsamples and let them run through the same codes to achieve this.

 

I want that as long as there is '1' in one of the datasets, the combined table should reflect '1'.

Below is my desired outcome:

idenvwatertransportproduction
11000
21010
30001
41011

I know there are probably time-consuming ways to achieve the desired outcome by renaming one of the table's column names, merging by id, summing the columns, restricting to greater than 1, and so on. However, I want to know if there is a fast and non-tedious way for operation as the reason why I initially split is that the original dataset was too huge. Hopefully, the expert can provide a more efficient method. Thank you. 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Stack the two datasets, then use PROC MEANS:

data stacked,
set a b;
by id;
run;

proc means data=stacked;
by id;
var env water transport production;
output
  out=want (drop=_type _freq_)
  max()=
;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Stack the two datasets, then use PROC MEANS:

data stacked,
set a b;
by id;
run;

proc means data=stacked;
by id;
var env water transport production;
output
  out=want (drop=_type _freq_)
  max()=
;
run;
andreas_lds
Jade | Level 19

A merge could be used, but requires renaming the variables of one dataset:

data want;
   merge have_a have_b(rename=(env= b_env water=b_water transport = b_transport production = b_production));   
   by id;
   
   env = env or b_env;
   water = water or b_water;
   transport = transport or b_transport;
   production = production or b_production;
   
   drop b_:;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 4650 views
  • 1 like
  • 3 in conversation