DATA Step, Macro, Functions and more

It is possible work with two datasets with MERGE both with same structure / same variable names?

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

It is possible work with two datasets with MERGE both with same structure / same variable names?

I have 2 similar datasets by different date same structure / same variable names:

DS2016

DS2017

how can I compare or work with variables if i use a MERGE? some like...

 

data NEW_DS;

merge

lib.DS2016(IN=A)

lib.DS2017(IN=B);

by ID;

IF A AND B;

*******************

IN THIS SPACE WORK WITH VARIABLES LIKE WITH PROC SQL:

NEWVAR= DS2016.var1-DS2017.var1

*******************

run;

 

I dont know how it works because I dont know how identify what variable is each for differente dataset


Accepted Solutions
Solution
‎02-14-2017 02:31 PM
Super User
Posts: 10,500

Re: It is possible work with two datasets with MERGE both with same structure / same variable names?


Angel_Saenz wrote:

I have 2 similar datasets by different date same structure / same variable names:

DS2016

DS2017

how can I compare or work with variables if i use a MERGE? some like...

 

 

I dont know how it works because I dont know how identify what variable is each for differente dataset


When you merge that way, since you say that the two sets have the same varaible names then basically you are going to get the second data set data that has matching ids. Merge keeps the variable value from the last listed dataset when the variable names are the same.

Take a look at this brief example:

data one;
   input id $ x;
datalines;
a  13
b  14
c  15
;

data two;
   input id $ x;
datalines;
b  23
c  24
d  25
;

data combined;
   merge
      one
      two
   ;
   by id;
run;

You do not say what kind of comparison you are looking to do. If you need to know which dataset a variable value comes from you either 1) rename all of the variables except id, or 2) SET instead of merge and add a variable to indicate the data set source or something else based on what you intend to follow.

 

 

It helps to post example of your input data and the desired results for you input. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

Descriptions of what type of comparison you need to do would also help. For instance there is PROC compare to examine differences or similarities on a record by record basis for all variables or use VAR and WITH for specific variables.

View solution in original post


All Replies
PROC Star
Posts: 551

Re: It is possible work with two datasets with MERGE both with same structure / same variable names?

Do you want to know about how a merge statement works?

Solution
‎02-14-2017 02:31 PM
Super User
Posts: 10,500

Re: It is possible work with two datasets with MERGE both with same structure / same variable names?


Angel_Saenz wrote:

I have 2 similar datasets by different date same structure / same variable names:

DS2016

DS2017

how can I compare or work with variables if i use a MERGE? some like...

 

 

I dont know how it works because I dont know how identify what variable is each for differente dataset


When you merge that way, since you say that the two sets have the same varaible names then basically you are going to get the second data set data that has matching ids. Merge keeps the variable value from the last listed dataset when the variable names are the same.

Take a look at this brief example:

data one;
   input id $ x;
datalines;
a  13
b  14
c  15
;

data two;
   input id $ x;
datalines;
b  23
c  24
d  25
;

data combined;
   merge
      one
      two
   ;
   by id;
run;

You do not say what kind of comparison you are looking to do. If you need to know which dataset a variable value comes from you either 1) rename all of the variables except id, or 2) SET instead of merge and add a variable to indicate the data set source or something else based on what you intend to follow.

 

 

It helps to post example of your input data and the desired results for you input. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

Descriptions of what type of comparison you need to do would also help. For instance there is PROC compare to examine differences or similarities on a record by record basis for all variables or use VAR and WITH for specific variables.

Super User
Posts: 6,936

Re: It is possible work with two datasets with MERGE both with same structure / same variable names?

To compare 2 datasets with identical structure, use proc compare.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
PROC Star
Posts: 7,363

Re: It is possible work with two datasets with MERGE both with same structure / same variable names?

Using the example data that @ballardw provided, something like the following would work:

 

libname lib 'c:\art';
data lib.one;
   input id $ x;
datalines;
a  13
b  14
c  15
;

data lib.two;
   input id $ x;
datalines;
b  23
c  24
d  25
;

data combined;
   merge
      lib.one (in=a keep=id x rename=(x=_x))
      lib.two (in=b)
   ;
   by id;
   if a and b;
   dif=x-_x;
run;

run;

HTH,

Art, CEO, AnalystFinder.com

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 187 views
  • 2 likes
  • 5 in conversation