数组SAS的动态维度

Dynamic dimension of array SAS

我有一个数据集ADDRESS如下图

data address;
    input fulladdress $char80.;
    datalines;
RM 101A TOWER 607,PALZZ     ,28 ABC ST     ,DISTRICT 14
FLAT 2426 24/F   ,KKL HSE   ,HAPPY ESTATE  ,DISTRICT 10
FLAT 08 18/F     ,BBC HOUSE ,CDEFG COURT   ,DISTRICT 9  , testingAdd5
;
run;

您可能会发现,对于每个观察结果,地址部分由分隔符“,”分隔 因此数组的维数是动态的(前两次观察为 4,最后一次观察为 5)。

我现在试的时候

data addressnew;
    set address;
    count = count(fulladdress,",") + 1;
    array add[5] .;
    do i = 1 to dim(add);
        add[i] = scan(fulladdress,i,",");
    end;
run;

我使用 5 作为 array add 的维度 我使用 count() 找出每行有多少个地址组件。我如何使用它来设置数组的维度?喜欢 array[&count] ?

根据@NEOman 的回答,如果我不知道数组的维度,我可以使用 add[*] 。当我收到以下错误

2252      array add[*] . ;
ERROR: The array add has been defined with zero elements.
2253      do i = 1 to count;
2254          add[i] = scan(fulladdress,i,",");
ERROR: Too many array subscripts specified for array add.

我想要的输出是

如果您不确定元素的数量,请使用array add[*]

或者您可以像下面这样定义 _Temporary_ 数组,其维度大于元素的数量,为了安全起见,我选择了 100。

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000201956.htm

data _null_;
set address;
count = count(fulladdress,",") + 1;
put count=;
array addn{0:999} $ _temporary_;
do i = 1 to count;
    addn[i] = scan(fulladdress,i,",");
    put addn[i]=;
end;

运行;

编辑 1: 据我了解你的问题,如果地址有六个段,你想创建 add1-add6 变量并将段存储在其中。

我尝试使用 dynamic array 进行操作,但由于某些原因,我遇到了奇怪的错误。

data addressnew;
set address;
count = count(fulladdress,",") + 1;
put count=;

array addn[*] addn: ;
do i = 1 to count;
    addn[i] = scan(fulladdress,i,",");
    put addn[i]=;
end;

下面是 TESTED 代码,它可能不是最复杂的(编程方面,但我想它不会对执行时间和 space 明智的)方法,但它正在工作。希望有人能提出更简单的解决方案。

通过扫描整个数据集中的所有记录来选择最大数量的段。

data temp;
set address(keep=fulladdress);
countnew = count(fulladdress,",") + 1;
run;

proc sql noprint;
select max(countnew) into: count_seg from temp;
quit;

%put &count_seg.;

/使用数组/

data _null_;
set address;
count = count(fulladdress,",") + 1;
put count=;

array add{%sysfunc(compress(&count_seg.))} .;
do i = 1 to count;
    add[i] = scan(fulladdress,i,",");
    put add[i]=;
end;
run;

/使用宏/

%macro test();
data _null_;
    set address;
    countnew = count(fulladdress,",") + 1;
       %do i = 1 %to &count_seg.;
        add&i. = scan(fulladdress,&i.,",");
        put add&i.=;
    %end;
run;

%mend;
%test;

数组引用了 SAS 中的其他变量并且大小不是动态的。该数组需要与您的元素列表一样大或更大。每行将具有相同数量的变量,并且最后一个变量将根据需要为空。您可以通过循环计算变量而不是数组的暗淡来使代码工作。

如果您一开始不知道 list/array 的尺寸,则必须先找到它

  *EDIT: Here's a way to find the max size of the array first;

  data _null_;
    set address end=eof;
    retain max_count 0;
    count = count(fulladdress,",") + 1;
    if count>max_count then max_count=count;
    if eof then call symputx('array_size', max_count);
  run;

 data addressnew;
  set address;
  array add[&max_count.] .;
  count = count(fulladdress,",") + 1;

  do i = 1 to count;
    add[i] = scan(fulladdress,i,",");
  end;
 run;