0
votes

I'm kinda new to SAS.

I have 2 datasets: set1 and set2. I'd like to get a list of variables that's in set2 but not in set1.

I know I can easily see them by doing proc compare and then listvar, however, i wish to copy&paste the whole list of different variables instead of copying one by one from the report generated.

i want either a macro variable containing a list of all different variables separated by space, or printing out all variables in plain texts that I can easily copy everything.

2

2 Answers

2
votes
proc contents data=set1 out=cols1;
proc contents data=set2 out=cols2;

data common;
  merge cols1 (in=a) cols2 (in=b);
  by name;
  if not a and b;
  keep name;
run;

proc sql;
  select name into :commoncols separated by ','
  from work.common;
quit;
1
votes

Get the list of variable names and then compare the lists.

Conceptually the simplest way see what is in a dataset is to use proc contents.

proc contents data=set1 noprint out=content1 ; run;
proc contents data=set2 noprint out=content2 ; run;

Now you just need to find the names that are in one and not the other.

An easy way is with PROC SQL set operations.

proc sql ;
   create table in1_not_in2 as
     select name from content1 
     where upcase(name) not in (select upcase(name) from content2)
   ;
   create table in2_not_in1 as
     select name from content2 
     where upcase(name) not in (select upcase(name) from content1)
   ;
quit;

You could also push the lists into macro variables instead of datasets.

proc sql noprint ;
   select name from content1 
     into :in1_not_in2 separated by ' '
     where upcase(name) not in (select upcase(name) from content2)
   ;
   select name from content2 
     into :in2_not_in1 separated by ' '
     where upcase(name) not in (select upcase(name) from content1)
   ;
quit;

Then you could use the macro variables to generate other code.

data both;
   set set1(drop=&in1_not_in2) set2(drop=&in2_not_in1) ;
run;