Obs | var1 | var2 | var3 | var4 |
---|---|---|---|---|
1 | This1 | That1 | This2 | That2 |
SAS data set 1 columns: Code Grade DocType SecNum
SAS data set 2 columns: Last First ID form
Data set I want: Code Grade DocType SecNum Last First ID form
Basically, a cut/paste if these were Excel files...but I keep getting the Cartesian product.
@GreggB wrote:
no merge.
Why not?
data this;
var1 = 'This1';
var2 = 'That1';
run;
data that;
var3 = 'This2';
var4 = 'That2';
run;
data want1;
merge this that;
run;
data want2;
set this that;
run;
proc print data=want1;
proc print data=want2;
run;
Results:
via MERGE
Obs | var1 | var2 | var3 | var4 |
---|---|---|---|---|
1 | This1 | That1 | This2 | That2 |
Via SET
Obs | var1 | var2 | var3 | var4 |
---|---|---|---|---|
1 | This1 | That1 | ||
2 | This2 | That2 |
no merge.
@GreggB wrote:
no merge.
Why not?
data this;
var1 = 'This1';
var2 = 'That1';
run;
data that;
var3 = 'This2';
var4 = 'That2';
run;
data want1;
merge this that;
run;
data want2;
set this that;
run;
proc print data=want1;
proc print data=want2;
run;
Results:
via MERGE
Obs | var1 | var2 | var3 | var4 |
---|---|---|---|---|
1 | This1 | That1 | This2 | That2 |
Via SET
Obs | var1 | var2 | var3 | var4 |
---|---|---|---|---|
1 | This1 | That1 | ||
2 | This2 | That2 |
this works! I always thought a MERGE had to have a BY statement
@GreggB wrote:
this works! I always thought a MERGE had to have a BY statement
Nope, that's what my first answer attempted to say, but it was worded badly.
@GreggB you have no variable in both of the datasets so merging will not work.
you results are dataset1 and appended dataset2.
you have to have something in common in both datasets to merge and retrieve your wants.
data this;
var1 = 'This';
var2 = 'That';
run;
data that;
var3 = 'This';
var4 = 'That';
run;
data results;
set this that;
run;
I don't want to merge them. I want columns A, B and C in set 1 and columns D, E and F in set 2 to be put in a new data set that has columns A, B, C, D, E, F.
This is a possibility:
data want;
set dataset1;
set dataset2;
run;
However, it's not clear how many observations you are working with in each data set. Would it be possible that one data set has 3 observations, and the other has 5 observations? If that's the case, how would you like them to be matched up?
I don't want them matched. Maybe "overlay" is the right word.
You should actually try the program I suggested. If you have one observation in each data set, it does exactly what you are asking for.
merge
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.