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

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.

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

@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

View solution in original post

12 REPLIES 12
Reeza
Super User
No BY statement with a data step merge?
GreggB
Pyrite | Level 9

no merge. 

Reeza
Super User

@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
GreggB
Pyrite | Level 9

this works! I always thought a MERGE had to have a BY statement

Reeza
Super User

@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.

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

@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;

GreggB
Pyrite | Level 9

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.

Astounding
PROC Star

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?

GreggB
Pyrite | Level 9

I don't want them matched. Maybe "overlay" is the right word. 

Astounding
PROC Star

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.

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

@GreggB which part of @Reeza answered your needs the merge or datastep?

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 12 replies
  • 1511 views
  • 6 likes
  • 4 in conversation