SAS:尝试构建一个用于上传多个文件的循环

时间:2015-07-01 18:30:56

标签: sas sas-macro

我正在尝试在SAS中构建一个循环来上传多个文件,并且遇到了一些问题需要解决。目前的代码:

%Macro Weatherupload(File=, output=);
proc import datafile = &File;
out = &output;
dbms=dlm replace;
delimiter= ",";
getnames=yes;
guessingrows = 1000;
run;
%Mend Weatherupload;

%Macro WeatherPrepare(input=, output=);
data &output (keep=Wban_Number _YearMonthDay DewPoint Temp _Avg_Dew_Pt _Avg_Temp year month day);
set &input;
DewPoint = Input(compress(_Avg_Dew_Pt,"*"), 3.);
Temp = Input(compress(_Avg_Temp,"*"), 3.);
year = (_yearmonthday - mod(_yearmonthday, 10000))/10000;
month = ((_yearmonthday - mod(_yearmonthday, 100)) - (_yearmonthday - mod(_yearmonthday,10000)))/100;
day = mod(_yearmonthday, 100);
drop _Avg_Dew_Pt _Avg_Temp _YearMonthDay;
run;
%Mend WeatherPrepare;

data temperatures;
do i = 1999 to 2015;
do j = 1 to 12;
name = 'C:\Users\DILLON.SAXE\Documents\'||i||j||'.tar'||'   \'||i||j||'daily.txt';
output = i||j||'weather';
final = i||j||'final';
%Weatherupload(File=name, output=output)
%WeatherPrepare(input=output, output=final)
end;
end;
run;

目标是在几个文件夹中运行多个文件,列在月份+日期+剩余标题中,并且(此时)从他们上传两个数据变量。稍后我会想要添加合并文件,并做更多的数据工作,但目前正是宏观问题和上传正在阻止它。

有没有办法在循环中使用proc上传,或者在循环中使用其他数据步骤?

我得到错误“比(某些)更多的位置变量”(我忘记确切的错误,但它列出了位置变量)。我已经尝试在宏中添加和删除逗号,但是无法摆脱这个错误。有什么想法吗?

3 个答案:

答案 0 :(得分:1)

我不认为您可以在数据步骤中调用宏。我认为您打算使用Call Execute

data temperatures;
do i = 1999 to 2015;
do j = 1 to 12;
name = 'C:\Users\DILLON.SAXE\Documents\'||i||j||'.tar'||'   \'||i||j||'daily.txt';
output = i||j||'weather';
final = i||j||'final';
call execute('%Weatherupload(File='||name||', output='||output||')');
call execute('%WeatherPrepare(input='||output||', output='||final||')');
end;
end;
run;

或者,假设您正在尝试读取文件夹中的所有文件,我认为您应该在数据集中创建文件名列表,使用带有filename选项的数据步骤一次性输入所有文件代替。如果单个文件夹中的所有位置都在这里,这是一个简短的方法:https://communities.sas.com/docs/DOC-10426

答案 1 :(得分:0)

这是一个页面,其中包含将文件列表导入数据集的代码

http://www.sascommunity.org/wiki/Making_Lists

因为你的宏既没有条件(%if)也没有循环(%do) 那么我建议你将它们用作参数化%incudes

这是一个读取文件列表数据集并调用程序

的工具

http://www.sascommunity.org/wiki/Call_Execute_Parameterized_Include

注意:在proc import中总是将guessingrows设置为最大值; 在v9.3中,即2147483647;

答案 2 :(得分:0)

根据第一个答案整理出来。最终代码:

%Macro Weatherupload(File=, output=);
proc import datafile = "&File"
out = &output
dbms=dlm replace;
delimiter= ",";
getnames=yes;
guessingrows = 1000;
run;
%Mend Weatherupload;

%Macro WeatherPrepare(input=, output=);
data &output;
set &input;
DewPoint = Input(compress(_Avg_Dew_Pt,"*"), 3.);
Temp = Input(compress(_Avg_Temp,"*"), 3.);
year = (_yearmonthday - mod(_yearmonthday, 10000))/10000;
month = ((_yearmonthday - mod(_yearmonthday, 100)) - (_yearmonthday - mod(_yearmonthday,10000)))/100;
day = mod(_yearmonthday, 100);
keep Wban_Number DewPoint Temp year month day;
run;
%Mend WeatherPrepare; 

%Macro WeatherPrepare2(input=, output=);
data &output;
set &input;
DewPoint = Input(DewPoint, 3.);
Temp = Input(compress(_Avg_Temp,"*"), 3.);
year = (_yearmonthday - mod(_yearmonthday, 10000))/10000;
month = ((_yearmonthday - mod(_yearmonthday, 100)) - (_yearmonthday - mod(_yearmonthday,10000)))/100;
day = mod(_yearmonthday, 100);
Wban_Number = Wban;
keep Wban_Number DewPoint Temp year month day;
run;
%Mend WeatherPrepare; 

%Macro Append(merge=);
data temperatures;
set temperatures &merge;
%Mend Append;

data temperatures;
do i = 1999 to 2015;
do j = 1 to 12;
jzero = put(j, z2.);
name = compress('C:\Users\DILLON.SAXE\Documents\'||i||jzero||'.tar'||'\'||i||jzero||'daily.txt');
name2 = compress('C:\Users\DILLON.SAXE\Documents\'||'QCLCD'||i||jzero||'\'||i||jzero||'daily.txt');
output = compress('weather'||i||j);
final = compress('final'||i||j);
if 1000*i+j < 200708 then
do;
call execute('%Weatherupload(File='||name||', output='||output||')');
call execute('%WeatherPrepare(input='||output||', output='||final||')');
end;
else
do;
call execute('%Weatherupload(File='||name2||', output='||output||')');
call execute('%WeatherPrepare2(input='||output||', output='||final||')');
end;
call execute('%Append(merge='||final||')');
end;
end;
drop i j jzero name name2 output final;
run;