SAS从表中设置为数组值

时间:2018-06-20 06:00:58

标签: arrays variables sas

感谢user667489。也许我认为不错。。但是现在合并方法出现了问题...

data Dictionary;
   input id_wiersz Labely $20. ;
   datalines;                      
1 Active
2 Gold
3 Prime
;

*Get the array parameter;
proc sql noprint;
select count(1) into :reccount from work.Dictionary ;
quit;

proc transpose data = Dictionary out = Dictionaryout;
    var Labely;
run;

data ArraryLoaded;
    set Dictionaryout (drop=_name_) nobs = nobs;
    array Dictionary _CHAR_;
    format col1-col3 $50.;
    retain col1-col3;
    array t[&reccount] $;
    do i = 1 to dim(t);
        if _N_ = 1 then t[i] = put(Dictionary[i], 8.);
            else t[i] = compress(t[i] || ',' || put(Dictionary[i], 8.));
    end;
    if _N_ = nobs;
    put "var1," t1;
    put "var2," t2;
    put "var3," t3;
run;

data Scinamy;
   input Description $20. ;
   datalines;                      
New old Active
New Active Old
ANother record
Records with Gold
Value with Gold
;

如何lista(&reccount)插入array t[&reccount]

data Scinamy1;
  set Scinamy;
  array lista(&reccount) $8 _temporary_ ('Active','Gold','Prime');
  length AA $30 ;
  do i = 1 to dim(lista);
    if findw(Description,lista(i),,'spit') then AA=catx(' ',AA,lista(i));
  end;
  if missing(AA) then AA='Not Found';
  drop i;
run;

2 个答案:

答案 0 :(得分:1)

我想到了您可以探索的一些选择:

  1. 用不同的整数和define a format to display the corresponding values替换Labely的每个不同值。然后,您可以使用现有的write_array方法。
  2. 使用do循环定义并填充字符数组within a data step
  3. 使用hash object来保存字典而不是数组,并使用哈希迭代器遍历字典。

尝试其中一项,如果遇到问题,再发布一个问题。

答案 1 :(得分:0)

如果要在以后的数据步骤中使用小型表中的数据生成临时数组,可以使用SAS宏处理器来帮助您生成语句。

如果列表足够小以适合单个宏变量(64K个字符),那么您可以使用以下内容:

proc sql noprint;
  select quote(trim(LabelY)) into :Label_list separated by ' '
  from work.dictionary;
%let no_labels=&sqlobs;
quit;

data new_step ;
   array lista(&no_labels) $20 _temporary_ (&label_list) ;
....

请注意,因为dictionary是PROC SQL中的特殊libref。使用work.前缀应该可以使其工作。或者只是将数据集命名为其他名称。

但是使用原始数据集代替ARRAY可能会更容易。

data Scinamy1;
  set Scinamy;
  length AA $30 ;
  do i = 1 to nobs;
    set work.dictionary(keep=LabelY) point=i nobs=nobs;
    if findw(Description,LabelY,,'spit') then AA=catx(' ',AA,LabelY);
  end;
  if missing(AA) then AA='Not Found';
  drop LabelY ;
run;