Compare a variable in two different data-sets in SAS

强颜欢笑 提交于 2020-01-06 19:06:03

问题


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.



来源:https://stackoverflow.com/questions/29747187/compare-a-variable-in-two-different-data-sets-in-sas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!