替代宏变量

时间:2014-06-03 10:26:13

标签: sql macros sas

我写了下面的脚本,见下文,它应该删除我的表中有大约70000个变量的列。

在这种情况下,我只给了你一小部分SQL脚本。通常在放入所有条件后,查询将检索我 6000变量-ish * 6.

宏变量droplist_u太小,无法保留所有内容。你有替代使用宏变量和避免 错误“表达式长度(65535)超过最大长度(65534)”?

编辑:一旦我获取了droplist中的所有变量,我就将其转换为具有该命令行的%put droplist_u: &droplist_u;

的宏变量
%let pays1=IK ;

%do nopays=1 %to 1;

%do l=0 %to 5;

/*one off*/
proc sql;
select catt(nomvar,"_t&&l") into : droplist_u separated by ' ' from dico&&pays&nopays
where (intitule like 'RT1%' or intitule like '%RT12%'
or intitule like 'IS-PPI%'
or intitule like 'IS-IP%'
or intitule like 'IS-IP-SA%'
or intitule like 'IS-IMPR%'
or intitule like 'IS-IMPX%'
or intitule like 'IS-IMPZ%'
or intitule like 'B-E36%'
or intitule like 'B-D_F%'
or intitule like 'B-D%'
or intitule like 'B_C%'
or intitule like 'MIG_ING%'
or intitule like 'MIG_NRG%'
or intitule like 'MIG_CAG%'
or intitule like 'MIG_COG%'
or intitule like 'MIG_DCOG%'
or intitule like '%C191%'
or intitule like '%C192%'
or intitule like '%C20_C21%'
or intitule like '%C20%'
or intitule like '%C201%'
or intitule like '%C2011%'
or intitule like '%C2012%'
or intitule like '%C2013%'
or intitule like '%C2014%'
or intitule like '%C2015%'
or intitule like '%C2016%'
or intitule like '%C2017%'
or intitule like '%C202%'
or intitule like '%C203%'
or intitule like '%C204%'
or intitule like '%C2041%'
or intitule like '%C2042%'
or intitule like '%C205%'
or intitule like '%C2051%'
or intitule like '%C2052%'
or intitule like '%C2053%'
or intitule like '%C2059%'
or intitule like '%C206%'
or intitule like '%C21%'
or intitule like '%C211%'
or intitule like '%PCH_SM%');
quit;

%put droplist_u: &droplist_u;

data a&&pays&nopays;
set a&&pays&nopays (drop=&droplist_u);
run;

%end;
%end;
%mend;


%testsql;
run;`

2 个答案:

答案 0 :(得分:2)

您可以尝试将datastep(代码)写入文本文件,然后%包含该文本文件 - 请参阅下面的伪代码(未经测试):

proc sql;
create table vars as
select catt(nomvar,"_t&&l") as var 
from dico&&pays&nopays
where (intitule like 'RT1%' or intitule like '%RT12%'
or intitule like 'IS-PPI%'
xxx
xxx
xxx
;quit;

filename tempf temp;
data _null_;
   file tempf;
   set vars end=lastobs;
   if _n_=1 then put "data a&&pays&nopays;set a&&pays&nopays (drop=";
   put var /;
   if lastobs then put ");run;";
run;

%inc tempf;

filename tempf clear;

请注意,您可以通过处理如此大的数据集(显然取决于您的操作环境)来点击memory limits

答案 1 :(得分:0)

什么可以为每个要删除序列号的列创建单独的宏变量,并使用循环将它们全部放入保留。

%macro dropColumns;
%let pays1=IK ;
%do nopays=1 %to 1;
data _null_;
 set dico&&pays&nopays 
 (where=(intitule like 'RT1%' 
  ...
  or intitule like '%PCH_SM%')) 
 end=last;
 %do l=0 %to 5;
  count + 1;
  call symput(cats('todelete',count),catt(nomvar,"_t&&l"));
 %end;
 if last then call symput('todeleteobs',count);
run;

data a&&pays&nopays;
 data a&&pays&nopays (drop=
 %do i = 1 %to &todeleteobs.;
  &&todelete&i.
 %end;
 );
run;

%end;
%mend;

%dropColumns;