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

Hi,

 

I have a base SAS 9.4 merge conundrum that is mystifying me. I have two datasets that connect on an 8-digit ID unique to each person in my data. My merge is one to many. When I try to merge these files by ID, SAS fails to find any matches and appends the two files. Both files are sorted on ID only.

 

My data step, which has successfully merged different files that use the same key and come from the same source and were created using the same proc SQL code:

 

data work.temp;

set dataA(in=master) dataB(in=using);

by clt_id;

masteronly=master and not using;

usingonly=using and not master;

match=master=1 and using=1;

run;

 

I have checked both datasets, and the ID is a numeric of length 8 and format 11. in both files. I have visually inspected both files to doublecheck that at least one ID exists in both.

 

And, to make it more baffling, SPSS is able to correctly match the exact same files on the ID.

 

The only potential answer I could find online is decimal instability, but my IDs are 8 digit integers and as far as I can tell SAS is only storing them as such because the type is only length 8.

 

So what could conceivably be causing this?

 

Summary: Two files linked on 8-digit integer ID, one to many merge, same storage type for ID in both files, successfully merged using same code by same ID with other files, visually confirmed at least one match exists, and SPSS matches files successfully.

1 ACCEPTED SOLUTION

Accepted Solutions
Kalind_Patel
Lapis Lazuli | Level 10

You are using set statement, which will append the data,

You have to use merge statement to merge the data in SAS.

 

data work.temp;

merge dataA(in=master) dataB(in=using);

by clt_id;

run;

 

For more example please refer this link:

http://www.ats.ucla.edu/stat/sas/modules/merge.htm

View solution in original post

6 REPLIES 6
Kalind_Patel
Lapis Lazuli | Level 10

You are using set statement, which will append the data,

You have to use merge statement to merge the data in SAS.

 

data work.temp;

merge dataA(in=master) dataB(in=using);

by clt_id;

run;

 

For more example please refer this link:

http://www.ats.ucla.edu/stat/sas/modules/merge.htm

JLcra
Fluorite | Level 6

Ugh, thank you. I knew I had to be making some stupid mistake. Of course my brain kept reading right over the set when I double checked code. Just needed another pair of eyes to see it for me.

Reeza
Super User

IDs should be character not numeric variables. 

Not matching can be due to numerical precision, bit as doubt it. 

 

I don't think this is correct, I would suggest starting with only the MERGE and By and adding your conditions one by one. 

 

masteronly=master and not using;
usingonly=using and not master;
match=master=1 and using=1;

JLcra
Fluorite | Level 6

No, IDs can and often are numerics. And those statements weren't conditions, just creating diagnostic variables to easily show me where records come from.

 

The solution was a blatant dumb typo on my part, I wrote set instead of merge. The rest of the code is fine.

ballardw
Super User

Yes many people use numerics for Ids. Then get bitten by additions to the system where leading zeroes become significant. Or have to merge with another system that has character values. Or a new scheme comes in adding additional information to the id so the length exceeds 16 or 17 digits and starts causing problems with precision.

 

IMNSHO, if you aren't going to do arithmetic on a variable it only looks like a numeric value and should be character.

JLcra
Fluorite | Level 6

When you work with data the size that I do, character storage requirements are a massive cost. That is why numerics are a great ID variable.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2203 views
  • 0 likes
  • 4 in conversation