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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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

4 REPLIES 4
ballardw
Super User

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

art297
Opal | Level 21

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

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 829 views
  • 2 likes
  • 5 in conversation