【问题标题】:Is it possible to merge two datasets where a variable's value in the first is used to select a variable in the second?是否可以合并两个数据集,其中第一个变量的值用于选择第二个变量?
【发布时间】:2015-10-19 14:58:51
【问题描述】:

我想知道如何使用第一个数据集中的变量值在 SAS 中合并两个数据集,以选择和测试第二个数据集中的变量。

以两个数据集为例。第一个数据集包含四个婴儿的名字和他们出生的日子。第二个数据集包含三位医生和一组指示变量,记录每位医生是否在特定日期工作。例如,史密斯博士仅在第 2 天和第 3 天工作。我想创建一个数据集,列出婴儿出生当天医生正在工作的所有可能的婴儿医生组合。

data babies;
input baby_name $ birth_day;
datalines;
Jake 1
Sonny 4
North 5
Apple 6
;
run;

data doctors;
input  DrLastname $ day1 day2 day3 day4 day5 day6;
datalines;
Jones 1 0 0 1 1 1 
Smith 0 1 1 0 0 0 
Lewis 1 1 1 0 0 0 
;
run;

解决方案似乎应该是这样的

proc sql;
 create table merged as
 select babies.*, doctors.* 
 from babies, doctors
 where doctors.day(babies.birth_day) = 1; *<--- incorrect;
quit;

输出应该是:

baby_name birth_day DrLastName
Jake      1         Jones
Jake      1         Lewis
Sonny     4         Jones
North     5         Jones
Apple     6         Jones

我遇到过几次这个问题,很想知道在 SAS 中是否可以进行这种合并。感谢您提供的任何帮助。

【问题讨论】:

    标签: merge sas proc-sql


    【解决方案1】:

    我可能会转移第二个数据集,然后在当天合并。

    类似(未经测试的伪代码):

        data new_1-new_6;
        set doctor;
        array day_1-day_6 day_{6}
        for i in 1 to 6:
          if day_{i} = 1 then do;
             day = i;
             output new_{i};
          end;
        end;
        run;
    
        data stacked;
          set day_1-day_6;
        run;
    

    然后根据现场日期简单地合并。

    【讨论】:

      【解决方案2】:

      虽然我可能也会转置数据集,但也可以不转置。

      data babies_doctors;
        set babies;
        do _i = 1 to nobs_doctors;
          set doctors point=_i nobs=nobs_doctors;
          array days day1-day6;
          if days[birth_Day] then output;
        end;
      run;
      

      这不会快速,因为它会检查数据集中的所有行,但这是可能的。

      最快的可能是将其加载到垂直哈希表(您可以轻松完成)或临时数组中。

      data babies_doctors_array;
        array drnames[32767] $80 _temporary_;
        array drdays[32767,6] _temporary_;
        if _n_=1 then do;
          do _i = 1 to nobs_doctors;
            set doctors point=_i nobs=nobs_doctors;
            array days day1-day6;
            drnames[_i]=DrLastname;
            do _j = 1 to dim(days);
              drdays[_i,_j]=days[_j];
            end;
          end;
        end;
        set babies;
        do _k = 1 to nobs_doctors;
          if drdays[_k,birth_day]=1 then do;
              baby_drlastname = drnames[_k];
              output;
          end;
        end;
      run;
      

      【讨论】:

      • 感谢您提供这些解决方案!我是否正确假设没有办法使用 proc sql 执行这种类型的动态(因为没有更好的词)合并?
      • 总有办法在 sql 中做所有事情,但我不认为它会那么容易。但是,您不能在 sql 中使用 vname,这是您真正必须要做的(并且没有数组概念),因此需要大量输入。
      猜你喜欢
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 2019-08-16
      • 2019-09-19
      相关资源
      最近更新 更多