我想要使用带有存储列的列表检查哪些列不存在于表中,并创建具有特定值的列。是否有一个选项可以这样做,而不必手动命名列?我的意思是从列表中获取名称并使用该信息创建列。应该是这样的:
proc sql;
select name into:varlist separated by ', '
from table
where column like 'name%'; (Every column will have the same name but
different suffix)
quit;
ALTER TABLE table_name
ADD COLUMN IF NOT EXISTS column_name tinyint(1) DEFAULT 0;
发布于 2022-03-28 06:36:45
使用字典表可能更容易做到这一点。
data have;
var_1 = 1;
var_3 = 1;
var_5 = 1;
run;
/* Create a table with all variables that you need */
data need;
array var_[5];
run;
/* Compare the template table variables to actual */
proc sql noprint;
select name
, 0 as init
into :varlist separated by ' '
, :init separated by ' '
from dictionary.columns
where libname = 'WORK'
AND memname = 'NEED'
AND name NOT IN
(select name
from dictionary.columns
where libname = 'WORK'
AND memname = 'HAVE'
AND name LIKE 'var_%'
)
;
quit;
/* Add the new variables to the table */
data want;
set have;
array var[*] &varlist. (&init.);
run;
输出:
var_1 var_3 var_5 var_2 var_4
1 1 1 0 0
https://stackoverflow.com/questions/71640516
复制