BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mlogan
Lapis Lazuli | Level 10

Hi all,

I always merge tables with the following method which require me to sort both tables by BY field. Is there a better way where I don't have to sort the table before merging?

 

This is what I had been doing:

 

DATA Want;
MERGE have1(in=a) 2ave2(in=b);
BY common_variable;
IF a=1;
RUN;

 

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

You could do the same thing in SQL. Behind the scenes SQL still sorts your data but you avoid having to sort it yourself. SQL isn't necessarrily better, just different:

 

proc sql;
  create table want as 
  select *
  from have1 as a
  left join have2 as b
  on a.common_variable = b.common_variable;
  ;
quit; 

View solution in original post

2 REPLIES 2
Astounding
PROC Star

The most common replacement would be a SQL left join.  Here's a paper that talks about more variations than you might want to see:

 

http://www2.sas.com/proceedings/sugi30/249-30.pdf

 

There are other, more complex methods, including creating an index, hashing, creating a format.  But SQL would definitely the place to begin. 

 

If you are concerned about getting identical results to the MERGE, you have to consider whether any data sets might have more than one observation per value of COMMON_VARIABLE.  If both data sets might have more than one, you have to ask yourself what the result should be ... not an easy topic.

SASKiwi
PROC Star

You could do the same thing in SQL. Behind the scenes SQL still sorts your data but you avoid having to sort it yourself. SQL isn't necessarrily better, just different:

 

proc sql;
  create table want as 
  select *
  from have1 as a
  left join have2 as b
  on a.common_variable = b.common_variable;
  ;
quit; 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 2 replies
  • 638 views
  • 2 likes
  • 3 in conversation