分离SAS数据集中的变量

时间:2014-07-16 16:02:57

标签: sql sas

我有一个关于以特定方式分离数据集中的变量的问题。当我们进行现场工作时,我们必须采用如下方法收集数据:

Range Row  HGT  V   HGT2  V2  HGT3  V3  HGT4  V4
1     2    151  15  127   22  114   16  97    12

实际上,变量不是不同类型的测量,而是距起点不同的距离。因此,我希望将数据转换为如下形式:

Range Row  HGT  V   HGT2  V2  HGT3  V3  HGT4  V4
1     2    151  15  .     .   .     .   .     .
1     2    .    .   127   22  .     .   .     .
1     2    .    .   .     .   114   16  .     .
1     2    .    .   .     .   .     .   97    12

这样,我可以使用一堆if-then语句为每行数据输入真行,因为range / row是我们识别所有内容的方式,并通过coalesce将数据压缩回2个变量sql中的语句。我知道这可以很容易地用excel手工完成,但由于我们犯错的风险,我们的实验室负责人强烈反对。

3 个答案:

答案 0 :(得分:1)

如果变量的数量是确定的,那么您可以遵循此方法。

假设:源数据为“Range_data”

data Range_data_1(keep=Range Row HGT V);
    set Range_data;
run;

data Range_data_2(keep=Range Row HGT2 V2);
    set Range_data;
run;

data Range_data_3(keep=Range Row HGT3 V3);
    set Range_data;
run;

data Range_data_4(keep=Range Row HGT4 V4);
    set Range_data;
run;

data All_Range;
    set Range_data_1 Range_data_2 Range_data_3 Range_data_4;
run;

建议:

  1. 如果HGT和V变量的数量不确定,则尝试编写宏。

  2. 一次检查表设计。

答案 1 :(得分:0)

这有帮助吗?

SELECT RANGE, ROW, HGT, V,  NULL AS HGT2, NULL AS  V2, NULL AS  HGT3, NULL AS  V3, NULL AS  HGT4, NULL AS  V4    
UNION
SELECT RANGE, ROW,  NULL AS HGT, NULL AS  V, HGT2, V2, NULL AS  HGT3, NULL AS  V3, NULL AS  HGT4, NULL AS  V4
UNION
SELECT RANGE, ROW,  NULL AS HGT, NULL AS  V,  NULL AS HGT2, NULL AS  V2, HGT3, V3, NULL AS  HGT4, NULL AS  V4
UNION
SELECT RANGE, ROW,  NULL AS HGT, NULL AS  V,  NULL AS HGT2, NULL AS  V2, NULL AS  HGT3, NULL AS  V3, HGT4, V4

答案 2 :(得分:0)

这是前两个索引的示例。

data newData (drop=i j Vold V2old HGTold HGT2old);
  format HGT V HGT2 V2;
  set oldData (rename=(V=Vold V2=V2old HGT=HGTold HGT2=HGT2old));
  array arrVold[2] Vold V2old;
  array arrVnew[2] V V2;
  array arrHold[2] HGTold HGT2old;
  array arrHnew[2] HGT HGT2;
  do i=1 to dim(arrVold);
    do j=1 to dim(arrVold);
      arrHnew[j]=.;
      arrVnew[j]=.;
    end;
    arrHnew[i]=arrHold[i];
    arrVnew[i]=arrVold[i];
    output;
  end;
run;