我有一个项目,我经常使用数据合并数据集并设置即
data want;
set have1 have2;
run;
问题是,这些数据集通常是相同的,但观察值具有不同的长度,并且会发生数据截断。因此,我开始编写一个宏,该宏允许基于字符变量最长长度的输入数据集进行动态长度更改。到目前为止,我已经构建了这段代码,非常简单
%Macro Formatting;
proc contents data = engdata.assets2 out = Assets1 noprint;
run;
data Assets2;
set Assets1;
keep NAME LENGTH;
if FORMAT = "$";
run;
proc contents data = historic.assets2016 out = HAssets1 noprint;
run;
data HAssets2;
set HAssets1;
keep NAME LENGTH;
if FORMAT = "$";
run;
proc contents data = engdata.Liabilities2 out = Liabilities1 noprint;
run;
data Liabilities2;
set Liabilities1;
keep NAME LENGTH;
if FORMAT = "$";
run;
proc contents data = historic.Liabilities2016 out = HLiabilities1 noprint;
run;
data HLiabilities2;
set HLiabilities1;
keep NAME LENGTH;
if FORMAT = "$";
run;
proc contents data = engdata.bonds2 out = bonds1 noprint;
run;
data bonds2;
set bonds1;
keep NAME LENGTH;
if FORMAT = "$";
run;
proc contents data = engdata.Irswaps2 out = Irswaps1 noprint;
run;
data Irswaps2;
set Irswaps1;
keep NAME LENGTH;
if FORMAT = "$";
run;
proc contents data = historic.Money_Market_2016 out = MoneyMarket1 noprint;
run;
data MoneyMarket2;
set MoneyMarket1;
keep NAME LENGTH;
if FORMAT = "$";
run;
proc sql;
create table AllLength as
select a.*
,a.Length as Length1
,b.Length as Length2
,c.Length as Length3
,d.Length as Length4
,e.Length as Length5
,f.Length as Length6
,g.Length as Length7
from Liabilities2 as a
left join Assets2 as b
on a.Name = b.Name
left join Bonds2 as c
on a.Name = c.Name
left join Irswaps2 as d
on a.Name = d.Name
left join HLiabilities2 as e
on a.Name = e.Name
left join HAssets2 as f
on a.Name = f.Name
left join MoneyMarket2 as g
on a.Name = g.Name
order by Name;
quit;
data AllLength2;
set AllLength;
array LengthVar Length1-Length7;
largest = max(of LengthVar[*]);
index = whichn(largest, of LengthVar[*]);
Varname = vname(LengthVar[index]);
keep name largest;
run;
proc sql noprint;
select name into: Var1 separated by " " from AllLength2;
select largest into: Var2 separated by " " from AllLength2;
quit;
%put &var1;
%put &var2;
%let index = 1;
%do %until (%Scan(&Var1,&index," ")=);
%let Varr1 = %Scan(&Var1,&index," ");
%let Varr2 = %Scan(&Var2,&index," ");
data engdata.liabilities2;
length &Varr1 $&Varr2..;
set engdata.liabilities2;
format &Varr1 $&Varr2..;
informat &Varr1 $&Varr2..;
run;
data engdata.assets2;
length &Varr1 $&Varr2..;
set engdata.assets2;
format &Varr1 $&Varr2..;
informat &Varr1 $&Varr2..;
run;
data engdata.bonds2;
length &Varr1 $&Varr2..;
set engdata.bonds2;
format &Varr1 $&Varr2..;
informat &Varr1 $&Varr2..;
run;
data engdata.irswaps2;
length &Varr1 $&Varr2..;
set engdata.irswaps2;
format &Varr1 $&Varr2..;
informat &Varr1 $&Varr2..;
run;
data historic.liabilities2016;
length &Varr1 $&Varr2..;
set historic.liabilities2016;
format &Varr1 $&Varr2..;
informat &Varr1 $&Varr2..;
run;
data historic.assets2016;
length &Varr1 $&Varr2..;
set historic.assets2016;
format &Varr1 $&Varr2..;
informat &Varr1 $&Varr2..;
run;
data Historic.moneymarket2016;
length &Varr1 $&Varr2..;
set Historic.moneymarket2016;
format &Varr1 $&Varr2..;
informat &Varr1 $&Varr2..;
run;
%let index = %eval(&Index + 1);
%end;
%mend;
%Formatting;
有时,我正在查看的变量格式在某些数据集中不存在,我在日志中得到以下信息
NOTE: There were 0 observations read from the data set HISTORIC.MONEYMARKET2016.
NOTE: The data set HISTORIC.MONEYMARKET2016 has 0 observations and 11 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.01 seconds
当我查看数据集时,一切都在那里?我会丢失任何工作吗,有没有办法使用这个循环,当变量不存在时,跳过它?