BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Fluvio1
Calcite | Level 5

I am using Proc Compare to try and select the observations in one dataset, but not in the other, and output the observations.  There does not seem to be an out=   option that does this ?  Please advise.  Here is my code:

 


proc contents data=work.import_163;
run;
*proc print;
*run;
proc sort data=work.import_163;
by Wrs_acres;
run;

data Merge_148_163;
merge work.import_148 work.import_163 ;
by WRS_acres;
run;

Proc print data=Merge_148_163;
run;
proc contents data=Merge_148_163;
run;


proc compare base=work.import_163 compare=work.import_148
out=diff outnoequal noprint;
id wrs_acres;
run;


proc print data=diff ;
by wrs_acres;
id wrs_acres;
title 'The Output Data Set RESULT';
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you want to match the observations on the key variables then just use a data step to split the data into matching and non matching datasets.

So something like this.

data both left_only right_only;
  merge left(in=inleft) right(in=inright);
  by keys ;
  if inleft and inright then output both;
  else if inleft then output left_only;
  else output right_only;
run;

If you want EXACT matches then make the BY statement include ALL of the variables.

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

If you want to match the observations on the key variables then just use a data step to split the data into matching and non matching datasets.

So something like this.

data both left_only right_only;
  merge left(in=inleft) right(in=inright);
  by keys ;
  if inleft and inright then output both;
  else if inleft then output left_only;
  else output right_only;
run;

If you want EXACT matches then make the BY statement include ALL of the variables.

ballardw
Super User

The options would be OUTBASE if you want the observation from the BASE data set or OUTCOMP if you want them from the COMPARE set.

Note that a variable telling you which data set the observations come from is added:

data work.base;
   set sashelp.class;
run;

data work.comp;
   set sashelp.class;
   if sex='M' and name not in ('Alfred' 'Robert')then height=height+1;
run;

proc compare base=work.base compare=work.comp noprint
   out=work.different outnoequal outcomp;
run;

proc compare base=work.base compare=work.comp noprint
   out=work.different2 outnoequal outbase;
run;

You can run this as you should have the SASHELP.CLASS data set.

Since you did not provide any actual data or what you want to see from the result it is hard to tell if this actually matches your expectations.

Fluvio1
Calcite | Level 5

I tried both programs but they do not provide the results I am looking for.  The attached dataset "Compare_dataset_148_obsv" contains 148 observations. The dataset "Compare_dataset_214_obsv" contains 214 observations.  I would like to identify the observations in  "Compare_dataset_214_obsv" that are not in  "Compare_dataset_148_obsv" and output the observations to a file.  While the datasets contain different variables, they have in common:  WRS_acres.

Reeza
Super User
proc sql;
create table data_want as 
select * from compare_dataset_214_obsv
where wrs_acres not in (select distinct wrs_acres from compare_dataset_148_obs);
quit;

@Fluvio1 wrote:

I tried both programs but they do not provide the results I am looking for.  The attached dataset "Compare_dataset_148_obsv" contains 148 observations. The dataset "Compare_dataset_214_obsv" contains 214 observations.  I would like to identify the observations in  "Compare_dataset_214_obsv" that are not in  "Compare_dataset_148_obsv" and output the observations to a file.  While the datasets contain different variables, they have in common:  WRS_acres.


 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 899 views
  • 2 likes
  • 4 in conversation