比较 SAS 中两个不同数据集中的变量 [英] Compare a variable in two different data-sets in SAS

查看:27
本文介绍了比较 SAS 中两个不同数据集中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较两个不同数据集中的两个变量,它们的变量名称不同,如果记录匹配,我需要在 SASYES 中写入观察结果,否则将其写入 SASNO.

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.

我正在从 DB2 检索记录并重命名变量.

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

我的SAS代码

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;  

当我提交代码时,即使在两个数据集中几乎没有匹配的观察值,所有观察值都会写入 SASNO.请帮我.

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.

注意:我也用MERGE从两个表中读取数据,结果是一样的.

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

有人可以帮忙吗?

推荐答案

SET 一次处理一行,从不跨数据集进行比较.就像拿一叠卡片,把它叠在另一叠上面,然后一次拿一张卡片.

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 结合了两个数据集,因此您可以将两叠卡片并排放置,然后一次查看每叠卡片的顶部.所以这就是你想要在这里做的:合并它们.

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;

您可能希望合并 by 一个 ID 变量,或者您可能希望在没有 by 语句的情况下合并,以便只比较每个数据集中的第一条记录,第二条来自每个数据集等

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 中两个不同数据集中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆