Compare a variable in two different data-sets in S

2019-09-02 06:17发布

问题:

I need to compare two variables in two different data-sets which has different names for the variables, then i need to write the observations in SASYES if the records match else write it to SASNO.

I am retrieving the records from DB2 and renaming the variables.

my sas code

DATA _NULL_;
   SET WORKLIST;
   SET UNITFUNC;
   IF PRIMNUM=CORRPMNM AND MODELCD=MCMODEL THEN DO; 
      FILE SASYES; 
      PUT @01  ANSFACT1 $CHAR7. 
          @09  CORRPMNM $CHAR12.
          @21  MCMODEL  $CHAR8. 
      OUTPUT SASYES; 
   END; 
   ELSE DO; 
      FILE SASNO; 
      PUT  @01  ANSFACT1 $CHAR7. 
           @09  CORRPMNM $CHAR12. 
           @21  MCMODEL  $CHAR8. 
      OUTPUT SASNO;
   END;
RUN;  

When i submit the code, all the observations are written to SASNO even when they are few matching observations in both the data-sets. Please help me.

Note: I have had used MERGE also to read the data from two tables, the result is same.

Can anyone help ?

回答1:

SET takes one row at a time, never comparing across datasets. Like taking one stack of cards and setting it on top of another, then taking one card at a time.

MERGE combines two datasets, so you take two stacks of cards and place them side by side and look at the top from each pile at one time. So that's what you want to do here: merge them.

data compare;
  merge ds1 ds2;
  by commonID;
run;

You might want to merge by an ID variable, or you might want to merge without a by statement to just compare the first record from each dataset, the second from each dataset, etc.

data compare;
  merge ds1 ds2;
run;

You can add code in that data step to further limit what is output, or you can just merge into one dataset then look at it visually and create additional code afterwards.



标签: sas