BookmarkSubscribeRSS Feed
sasDave
Calcite | Level 5

SAS 9.4

I'm trying to merge two datasets and one column is giving me issues. This column ("county") is named the same in both datasets, but in one of them it has a length of 8 and in the other a length of 60. I believe this is causing issues with the merge. Any ideas on how I can change the 60 to 8 or 8 to 60?sascap.PNG

3 REPLIES 3
mkeintz
PROC Star

When you are using the MERGE statement for multiple datasets with common variables, the earliest (i.e. leftmost) discovery of the common variable will determine the attributes, like length, of the variable - even though the rightmost value will prevail.  So if you want the length of 60 to be propagated to the new dataset, make sure the incoming dataset with the length of 60 is mentioned first in the merge statement.

 

Let's say you want to merge have1 and have2 with variables ID, A, B, C, X, Y, and Z in common.  But you want the length of variables X, Y, and Z from have2 to prevail, but you want the lengths of A, B, and C to be inherited from have1.  Then you could:

 

data want;
  if 0 then set have1 (drop=x y z) have2;
  merge have1 have2;
  by id;
run;

The "if 0" statement is enountered by the sas compiler prior to the merge statement.  The compiler resolves it to produce lengths as I described above.  But since "if 0" is never true, the "then set ..." action is never performed.  Instead the MERGE statement does the actual work, without the lengths of X Y and Z determined by have2.  

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

--------------------------
andreas_lds
Jade | Level 19

You should check why the same variable has been defined with different lengths during import and fix the problem in that stage of your project. If you can't change the importing code, ask the one who created it, there must be a reason, maybe you have state-codes in one dataset an state names in the other.

Ksharp
Super User
data want;
length county $ 60;
merge have1 have2;
by county ;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 1217 views
  • 1 like
  • 4 in conversation