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
@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.
Do you want to know about how a merge statement works?
@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.
To compare 2 datasets with identical structure, use proc compare.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.