从数据集中批量应用标签

问题描述 投票:0回答:1

我有一个数据集

labels
,有两列:
variable
label
,第二个数据集
data
是我的实际数据集,其列对应于
variables
中的
labels
labels中的n行) 
=
data
中的 ncol)。我想使用某种宏来应用每个标签,而不是单独键入每个标签。

在小数据集中,可以通过以下方式实现:

proc sql;
    select "label " || variable|| " = '" || label || "';"
    into :labels separated by " "
    from labels;
quit;

data data;
    set data;

    &labels.;
run;

但是,在这种情况下,由于数据集的大小,我最终得到了一个巨大的宏变量

&labels.
,它被截断了。还有其他方法可以做到这一点吗?

sas
1个回答
0
投票

使用 PROC 数据集并调用 EXECUTE。

*Create label data set;
data label_data_set;
length name label $25.;
name="Sex"; label="Gender"; output;
name="height"; label="Height (in)"; output;
name="weight"; label="Weight (lbs)"; output;
run;
 
 
*Create sample dataset to apply label;
data class;
set sashelp.class;
run;



*change to reflect your data set;
%let lib2update = work; /*library where data set to be changed is stored*/
%let data2update = class; /*dataset to be modified*/
%let label_data = label_data_set; /*dataset with the labels*/


*********************************************************;
*DO NOT CHANGE AFTER THIS LINE;
**********************************************************;
title 'Data set before updates';
ods select variables;
proc contents data=&data2update;
run;


*proc datasets implementation of changes;
data _null_;
set &label_data. end=eof;

if _n_ = 1 then call execute(catt("proc datasets lib= &lib2update nolist nodetails; modify &data2update; label "));

str = catx("=", name, quote(label));
call execute (str);
call execute(" ");

if eof then call execute('; run;quit;');
run;

*check results;
title 'Data set after updates';
ods select variables;
proc contents data=&data2update;
run;
© www.soinside.com 2019 - 2024. All rights reserved.