如何在数据步骤中获取数据集标签?

时间:2019-07-12 09:36:43

标签: sas attr

我想获取set语句中的数据集标签,但是我没有找到漂亮的方法。

我已经尝试过:

data test;
    set sashelp.class;
    rc = open("sashelp.class");
    label = attrc(rc,"label");
    rc = close(rc);
run;

它可以工作,但有一个缺点,就是我必须在open()函数中写数据集的名称。
我正在寻找一种更好的方法来代替手动编写,因为我有数十个类似的步骤。

我也尝试过&syslast,但是没有用。可能还有其他方法吗?

2 个答案:

答案 0 :(得分:5)

也许是INDSNAME

18   data _null_;
19      set sashelp.class(obs=2 drop=_all_) sashelp.shoes(obs=2 drop=_all_)indsname=indsname;
20      retain label;
21      if indsname ne lag(indsname) then do;
22         rc = open(indsname); label=attrc(rc,"label"); rc=close(rc);
23         end;
24      put _all_;
25
26      run;

indsname=SASHELP.CLASS label=Student Data rc=0 _ERROR_=0 _N_=1
indsname=SASHELP.CLASS label=Student Data rc=. _ERROR_=0 _N_=2
indsname=SASHELP.SHOES label=Fictitious Shoe Company Data rc=0 _ERROR_=0 _N_=3
indsname=SASHELP.SHOES label=Fictitious Shoe Company Data rc=. _ERROR_=0 _N_=4

答案 1 :(得分:1)

IMPLMAC允许的语句样式宏是否符合SAS​​选美大赛的条件?

SET语句重新用作语句样式宏,该宏发出包含常规SET语句和通过ATTRC检索到的数据集标签的数据步骤变量赋值的源代码。

示例:

%macro SET(data) / STMT;
  options IMPLMAC = 0; %* turn off implmac so following SET is normal token;
  SET &data;
  options IMPLMAC = 1; %* turn on implac so subsequent SET invoke this macro;
  %local id;
  %let id = %sysfunc(open(&data));
  %if (&id) %then %do;
    DSLABEL = %sysfunc(quote(%sysfunc(ATTRC(&ID,LABEL))));
    %let id = %sysfunc(close(&id));
  %end;
%mend;

data have(label="This is the data set I ""have""");
  x=1;
run;


options IMPLMAC=1 MPRINT;

data _null_;
  SET HAVE;
run;

options IMPLMAC=0;