Creon12
Creon12

Reputation: 35

Comparing two sas datasets

I have two sas datasets

An answer dataset

id   A1 A2 A3 A4  
1    A  B  C  D
2    B  B  C  A
3    A  A  D  D

And a key dataset

A1 A2 A3 A4
A  B  C  D

The question is if there is a way in SAS to loop trough the observations in the first dataset and compare them with the only observation in the second dataset. I am quite new to SAS, so anything would be of help

Upvotes: 0

Views: 1308

Answers (2)

user5693820
user5693820

Reputation: 29

proc sort data=a; by a1 a2 a3 a4; run;
proc sort data=b; by a1 a2 a3 a4; run;

data c;
  merge a(in=ina) b(in=inb)
  by a1 a2 a3 a4;
  if inb then b='Ý';
run;

it creates data set a with indicator b='Ý' for the matching one

Upvotes: 1

Joe
Joe

Reputation: 63424

Looping is handled by the data step - it does that automatically.

To get a single row appended to all rows in another dataset is very simple:

data want;
  if _n_=1 then set have_onerow;
  set have;
run;

That only works if the variable names are different though - but that would append that row from have_onerow to all rows of have (as variables from SET are automatically retained).

This doesn't do the compare for you - you have to do that.

However, there's a way to use this to generate a proc compare dataset. Keep only the ID and do the above: then you have the key repeated once per ID. Then proc compare will do this for you - look at the documentation to get more information on what options give you the right info in your output dataset.

data have;
input id   A1 $ A2 $ A3 $ A4 $ ;
datalines;
1    A  B  C  D
2    B  B  C  A
3    A  A  D  D
;;;;
run;

data key;
input a1 $ a2 $ a3 $ a4 $;
datalines;
A B C D
;;;;
run;

data key_allrows;
  if _n_=1 then set key;
  set have (keep=id);
run;

proc compare base=key_allrows compare=have out=compare;
  by id;
run;

Upvotes: 2

Related Questions