【问题标题】:sas: how to create a variable containing a list of different variables in 2 datasetssas:如何创建一个包含 2 个数据集中不同变量列表的变量
【发布时间】:2019-07-20 12:39:45
【问题描述】:

我对 SAS 有点陌生。

我有 2 个数据集:set1 和 set2。 我想获取 set2 中但不在 set1 中的变量列表。

我知道我可以通过 proc compare 然后 listvar 轻松查看它们, 但是,我希望复制和粘贴不同变量的整个列表,而不是从生成的报告中一一复制。

我想要一个包含由空格分隔的所有不同变量的列表的宏变量,或者以我可以轻松复制所有内容的纯文本打印出所有变量。

【问题讨论】:

    标签: sas


    【解决方案1】:
    proc contents data=set1 out=cols1;
    proc contents data=set2 out=cols2;
    
    data common;
      merge cols1 (in=a) cols2 (in=b);
      by name;
      if not a and b;
      keep name;
    run;
    
    proc sql;
      select name into :commoncols separated by ','
      from work.common;
    quit;
    

    【讨论】:

      【解决方案2】:

      获取变量名列表,然后比较列表。

      从概念上讲,查看数据集中内容的最简单方法是使用proc contents

      proc contents data=set1 noprint out=content1 ; run;
      proc contents data=set2 noprint out=content2 ; run;
      

      现在你只需要找到一个名字而不是另一个名字。

      一个简单的方法是使用 PROC SQL 集合操作。

      proc sql ;
         create table in1_not_in2 as
           select name from content1 
           where upcase(name) not in (select upcase(name) from content2)
         ;
         create table in2_not_in1 as
           select name from content2 
           where upcase(name) not in (select upcase(name) from content1)
         ;
      quit;
      

      您还可以将列表推送到宏变量而不是数据集。

      proc sql noprint ;
         select name from content1 
           into :in1_not_in2 separated by ' '
           where upcase(name) not in (select upcase(name) from content2)
         ;
         select name from content2 
           into :in2_not_in1 separated by ' '
           where upcase(name) not in (select upcase(name) from content1)
         ;
      quit;
      

      然后你可以使用宏变量来生成其他代码。

      data both;
         set set1(drop=&in1_not_in2) set2(drop=&in2_not_in1) ;
      run;
      

      【讨论】:

        猜你喜欢
        • 2015-08-25
        • 2019-03-17
        • 2019-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多