SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
molla
Fluorite | Level 6

Hii ,I have two datasets,I need to compare those two datasets and get the variables list that are common, not common  and get that in a pdf format,

for example I have a dataset1 with the follwing varaibles:

id name manager

dataset2 with the following variables:

id manager managerid

I need the output as

 commonvaraibles      notcommonvariables

   id                                  managerid

   manager                       name

I dont have much knowledge on ODS,Pls helpme regarding this.

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

I think that the following comes awfully close to what you want:

 

data dataset1;
   input id $ name manager  $;
   cards;
1 john joe
2 mary judy
;
   
data dataset2;
  input id manager $ managerid;
  cards;
1 tom 1
2 mary 6
;

proc contents data=dataset1 noprint out=out1(keep=name);
run;
proc contents data=dataset2 noprint out=out2(keep=name);
run;

data commonvars (drop=name);
  merge out1(in=a) out2(in=b);
  by name;
  if a and b then commonvariables=name;
  else notcommonvariables=name;
run;

ods pdf file='/folders/myfolders/test.pdf';
proc print data=commonvars;
run;
ods pdf close;

 

Art, CEO, AnalystFinder.com

 

View solution in original post

3 REPLIES 3
molla
Fluorite | Level 6

I got the common variables :

proc contents data=dataset1 noprint out=out1(keep=name);

run;

proc contents data=dataset2 noprint out=out1(keep=name);

run;

data commonvars;

merge out1(in=a) out2(in=b);

by name;

if a and b then output;

run;

 

art297
Opal | Level 21

I think that the following comes awfully close to what you want:

 

data dataset1;
   input id $ name manager  $;
   cards;
1 john joe
2 mary judy
;
   
data dataset2;
  input id manager $ managerid;
  cards;
1 tom 1
2 mary 6
;

proc contents data=dataset1 noprint out=out1(keep=name);
run;
proc contents data=dataset2 noprint out=out2(keep=name);
run;

data commonvars (drop=name);
  merge out1(in=a) out2(in=b);
  by name;
  if a and b then commonvariables=name;
  else notcommonvariables=name;
run;

ods pdf file='/folders/myfolders/test.pdf';
proc print data=commonvars;
run;
ods pdf close;

 

Art, CEO, AnalystFinder.com

 

Reeza
Super User

You should look into PROC COMPARE for this solution rather than hard code it. This is how your code should look, but you probably want to add some options to PROC COMPARE to limit the output to what you're interested in. 

 

ods pdf file='myfile.pdf';

proc compare data=one compare=two;
run;

ods pdf close;

Here's an example of how I deal with this, when having multiple data sets and need to identify which variables are present in which datasets.

 

https://gist.github.com/statgeek/3b57ae085d9f7a36a2d95c15f04e72e6

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 3 replies
  • 5989 views
  • 0 likes
  • 3 in conversation