gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:13:55 24 4
gpt4 key购买 nike

我有一个如下所示的数据集。每行是一个不同的观察值,其值从 1 到 x(在本例中为 x=3)。我想创建一个包含原始信息的数据集,但还有四个附加列(用于数据集中存在的 Bin 的四个值)。如果该行中存在任何 1,则列 freq_Bin_1 的值将为 1,否则为缺失。如果存在任何 2,则列 freq_Bin_2 将为 1,等等。

原始数据集中的 Bins 数量和列数可能会有所不同。

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,因为这是默认设置。

关于arrays - 使用 SAS 检查列是否具有指定的特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30379652/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com