我在 SAS 中遇到了一个问题。我在单独的 txt 文件中有一堆每月的天气数据。我目前的目标是阅读这些内容并为每个内容创建一个单独的数据集。或者,我可以看到可以跳过此步骤并更接近将所有这些数据集按日期和时间合并到另一个数据集的最终目标。下面是我对这个问题的尝试。我认为一个宏可以遍历文件名并创建匹配的数据集名称,但显然它没有。另外,为了提高效率,我认为可以将 if/else if 语句替换为 DO 循环,但我无法弄清楚。非常感谢您的帮助!
%macro loop;
%do i = 11 %to 13;
%do j = 01 %to 12;
%let year = i;
%let month = j;
data _&year&month ;
infile "&path\hr_pit_&year..&month..txt" firstobs=27;
length Time $ 4 Month $ 3 Day $ 2 Year $ 4 temp 3;
input time $ Month $ 10-13 Day Year temp 32-34;
Date = Day||Month||Year;
if time = '12AM' then time = 2400;
else if time = '1AM ' then time = 100;
else if time = '2AM ' then time = 200;
else if time = '3AM ' then time = 300;
else if time = '4AM ' then time = 400;
else if time = '5AM ' then time = 500;
else if time = '6AM ' then time = 600;
else if time = '7AM ' then time = 700;
else if time = '8AM ' then time = 800;
else if time = '9AM ' then time = 900;
else if time = '10AM' then time = 1000;
else if time = '11AM' then time = 1100;
else if time = '12PM' then time = 1200;
else if time = '1PM ' then time = 1300;
else if time = '2PM ' then time = 1400;
else if time = '3PM ' then time = 1500;
else if time = '4PM ' then time = 1600;
else if time = '5PM ' then time = 1700;
else if time = '6PM ' then time = 1800;
else if time = '7PM ' then time = 1900;
else if time = '8PM ' then time = 2000;
else if time = '9PM ' then time = 2100;
else if time = '10PM' then time = 2200;
else if time = '11PM' then time = 2300;
_time = input(time,4.);
time = _time;
drop month day year;
run;
%end;
%end;
%mend;
%loop; run:
如果有人想知道这是典型 txt 文件的外观:http ://www.erh.noaa.gov/pbz/hourlywx/hr_pit_13.01
以下是相同形状和格式的 txt 文件列表: http ://www.erh.noaa.gov/pbz/hourlyclimate.htm