【问题标题】:label variables in one data set with values from another?用另一个数据集中的值标记一个数据集中的变量?
【发布时间】:2017-01-10 23:06:23
【问题描述】:

如何获取元数据数据集中包含的标签并将它们应用到 set1 数据集中的变量?

想要的结果是“set1”仍然包含变量 a-h,并且适当的变量现在有标签。例如,“set1”将继续有一个没有标签的变量“a”,但变量“b”现在将有标签“Label1”等。

我下面的代码可以工作,但效率很低,因为它为每个变量运行宏。因此,对于每个标签,它必须读取“set1”,应用标签并保存“set1”。使用大型“set1”和“元数据”数据集执行此操作时,速度非常慢。

/**********************************************************
Reads metadata - in the real case it comes from a large 
csv file
***********************************************************/
data metadata;
input var $ labels $;
datalines;
b Label1
d Label2
f Label3
;
run;

/**********************************************************
Reads 'set1' in the real case it comes from many 
even larger csv files.
***********************************************************/
data set1;
input a b c d e f g h;
datalines;
1 1 0 5 6 4 0 4
2 3 4 5 3 5 0 1
3 2 1 9 6 5 8 1
;
run;

/**********************************************************
Macro to relabel one by one
***********************************************************/
%Macro relabel(var,label);
DATA set1;
    set set1;
    label %quote(&var) = %quote(&label);
RUN;
%Mend relabel;

/**********************************************************
Steps through 'metadata' and individually calls the macro
for each obs
***********************************************************/
data _null_;
    set metadata;
    call execute('%relabel('||var||','||labels||')');
run;

proc print;
run;

/**********************************************************
Shows labels applied correctly.
***********************************************************/
proc contents;
run;

【问题讨论】:

  • 您必须使用元数据生成一些代码,但您不需要重新创建 SET1,只需使用 PROC DATASETS。基本上你有什么,但使用 PROC DATASETS。
  • 感谢 data_null_!这肯定会加快速度。

标签: sas


【解决方案1】:

如果您的元数据足够小,那么您可以使用宏变量来保存它。宏变量的大小有 65K 的限制。

proc sql noprint;
  select catx('=',var,quote(trim(labels))) 
    into :labels separated by ' '
    from metadata
  ;
quit;

proc datasets nolist lib=work ;
  modify set1;
  label &labels;
  run;
quit;

【讨论】:

    猜你喜欢
    • 2019-06-17
    • 2017-11-22
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多