BookmarkSubscribeRSS Feed
krg1140
Calcite | Level 5

I have been instructed to:

 

  1. Create a new dataset called “Complete_Data” by merging the datasets “Audit_Data” and “Firm_Data” on the variables GVKEY and FISCAL_YEAR_ENDED.

As always this is my code:
data reg.Complete_Data;
merge reg.Firm_data reg.Audit_data;
by gvkey fiscal_year_ended;
run;

 

but I'm getting an error that reads ERROR: BY variables are not properly sorted on reg.Audit_Data

 

What does this mean and how can I alleviate this problem?

4 REPLIES 4
SASKiwi
PROC Star

The datasets you are merging on aren't sorted in the order you are trying to merge. Sort them first like this:

proc sort data = reg.Firm_data ;
  by gvkey fiscal_year_ended;
run;
mkeintz
PROC Star

When you MERGE datasets, they are typically assumed to be sorted by the merge key(s).  This common usage of merge reads each dataset sequentially, which offers a lot of speed advantages.  This is why @SASKiwi told you to sort the relevant files prior to the merge step.

 

True, merge can also work with unsorted data, but only if the merged datasets are indexed by the single merge key  (or has a compound index on the multiple merge keys).

 

Avoiding pre-sorting is often why many programmers use PROC SQL to join files, but this approach does not have access to some of the tools available in merge (like the first. and last. dummy variables, or lag functions).

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
mkeintz
PROC Star

Sorry, duplicate postings from malfunctioning browser (posting gave false negative, eventually generating "504 Error, request could not be satisfied").

 

When you MERGE datasets, they are typically assumed to be sorted by the merge key(s).  This common usage of merge reads each dataset sequentially, which offers a lot of speed advantages.  This is why @SASKiwi told you to sort the relevant files prior to the merge step.

 

True, merge can also work with unsorted data, but only if the merged datasets are indexed by the single merge key  (or has a compound index on the multiple merge keys).

 

Avoiding pre-sorting is often why many programmers use PROC SQL to join files, but this approach does not have access to some of the tools available in merge (like the first. and last. dummy variables, or lag functions.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
mkeintz
PROC Star

Sorry, duplicate postings from malfunctioning browser user:

 

When you MERGE datasets, they are typically assumed to be sorted by the merge key(s).  This common usage of merge reads each dataset sequentially, which offers a lot of speed advantages.  This is why @SASKiwi told you to sort the relevant files prior to the merge step.

 

True, merge can also work with unsorted data, but only if the merged datasets are indexed by the single merge key  (or has a compound index on the multiple merge keys).

 

Avoiding pre-sorting is often why many programmers use PROC SQL to join files, but this approach does not have access to some of the tools available in merge (like the first. and last. dummy variables, or lag functions.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 551 views
  • 0 likes
  • 3 in conversation