Arrays 使用SAS检查列是否具有指定的特征

Arrays 使用SAS检查列是否具有指定的特征,arrays,sas,Arrays,Sas,我有一个看起来像下面的数据集。每行都是一个不同的观测值,其值介于1到x之间(在本例中,x=3)。我想创建一个数据集,其中包含原始信息,但包含四个附加列(用于数据集中存在的四个Bin值)。如果该行中存在任何1,则列freq_Bin_1的值将为1,否则将丢失。如果存在任何2,则列freq_Bin_2将为1,以此类推 原始数据集中的存储箱数和列数可能会有所不同 data have; input Bin_1 Bin_2 Bin_3; cards; 1 . . 3 . . 1 1 . 3 2 1

我有一个看起来像下面的数据集。每行都是一个不同的观测值,其值介于1到x之间(在本例中,x=3)。我想创建一个数据集,其中包含原始信息,但包含四个附加列(用于数据集中存在的四个Bin值)。如果该行中存在任何1,则列freq_Bin_1的值将为1,否则将丢失。如果存在任何2,则列freq_Bin_2将为1,以此类推

原始数据集中的存储箱数和列数可能会有所不同

data have;
    input Bin_1 Bin_2 Bin_3;
cards;
1 . .
3 . .
1 1 .
3 2 1
3 4 .
;
run;
这是我想要的输出:

data want_this;
    input Bin_1 Bin_2 Bin_3 freq_Bin_1 freq_Bin_2 freq_Bin_3 freq_Bin_4;
cards;
1 . . 1 . . .
3 . . . . 1 .
1 1 . 1 . . .
3 2 1 1 1 1 .
3 4 . . . 1 1
;
run;
我有一个阵列解决方案,我认为非常接近,但我不能完全得到它。我也对其他方法持开放态度

data want;
    set have;
    array Bins {&max_freq.} Bin:; 
    array freq_Bin {&num_bin.} freq_Bin_1-freq_Bin_&num_bin.;
    do j=1 to dim(Bins);
        freq_Bin(j)=.;
    end;
    do k=1 to dim(freq_Bin);
        if Bins(k)=1 then freq_Bin(1)=1;
        else if Bins(k)=2 then freq_Bin(2)=1;
        else if Bins(k)=3 then freq_Bin(3)=1;
        else if Bins(k)=4 then freq_Bin(4)=1;
    end;
    drop j k;
run;
这应该起作用:

data want;
    set have;
    array Bins{*} Bin:; 
    array freq_Bin{4};
    do k=1 to dim(Bins);
        if Bins(k) ne . then freq_Bin(Bins(k))=1;
    end;
    drop k;
run;
我对代码做了一些调整,但实际上唯一的问题是,在尝试使用它索引数组之前,需要检查
Bins(k)
是否丢失。另外,没有必要将值初始化为missing,因为这是默认值