数据结构为:
状态 | 县 | 日期 | 空气质量指数 | 污染物 |
---|---|---|---|---|
阿拉巴马州 | 鲍德温 | 5/2/2017 | 68 | 臭氧 |
阿拉巴马州 | 鲍德温 | 6/4/2017 | 102 | PM2.5 |
阿拉巴马州 | 迪卡尔布 | 6/6/2017 | 105 | PM10 |
我正在尝试按污染物、县和州计算 AQI >100 的天数/年,这样输出看起来像:
状态 | 县 | 臭氧 | PM2.5 | PM10 |
---|---|---|---|---|
阿拉巴马州 | 鲍德温 | |||
阿拉巴马州 | 迪卡尔布 |
表中的值表示该州和县的 AQI>100 的天数/年,按每种污染物分类。每个州和县的组合每行只能出现一次。
有没有一种直接的方法可以在 SAS 中执行此操作而无需对数据进行子集化?我已经尝试了一些 proc sql 和 proc report 的组合,但无法产生我正在寻找的东西。另外,有没有办法评估同一县和州在同一日期是否存在多种 AQI > 100 的污染物?例如,在上面的第一个表中,如果对阿拉巴马州、鲍德温、2017 年 6 月 4 日、107、臭氧进行了观测。
Use 可以使用
Proc TABULATE
与 CLASS Pollutant
或 Proc REPORT
与 DEFINE Pollutant/ACROSS
例子:
Classdata 是每个组合并与制表一起使用以确保每个县都出现在输出中,即使它们从未有 aqi>100
proc sql;
create table everycombo as
select state, county, pollutant
from
( select distinct state, county from have )
cross join
( select distinct pollutant from have )
;
proc tabulate data=have classdata=everycombo;
class state county;
class pollutant / order=internal;
var aqi;
table state*county , pollutant * N ;
where aqi > 100;
run;
报告示例:
为了显示所有县,甚至是 AQI 持续较低的县,计算二进制标志值。值的总和是事件的数量。
Compute 语句用于在all 报告行中传播状态。
data report;
set have;
if aqi > 100 then aqiflag=1;
run;
proc report data=report;
where cats(state) = '1';
columns state rstate county aqiflag,pollutant;
define state / group noprint;
define county / group ;
define rstate / computed;
define pollutant / ' ' across;
define aqiflag / ' ' sum; /* sum the flag */
compute before state;
stateg = state; /* capture value */
endcomp;
compute rstate;
rstate = stateg; /* propagate */
endcomp;
run;
这里足以让你开始。
data have;
infile cards dlm=' ' dsd truncover;
informat state $20. County $20. Date mmddyy10. AQI 8. Pollutant $20.;
input State County Date AQI Pollutant;
cards;
Alabama Baldwin 5/2/2017 68 ozone
Alabama Baldwin 6/4/2017 102 PM2.5
Alabama Baldwin 5/4/2017 75 ozone
Alabama Baldwin 7/4/2017 88 PM2.5
Alabama Dekalb 6/6/2017 105 PM10
Alabama Dekalb 6/6/2017 107 PM10
;;;;
run;
proc means data=have noprint nway;
class state county pollutant date;
var aqi;
output out=daily_data mean(aqi)=;
run;
proc sql;
create table summary as
select state, county, pollutant, sum(aqi>100) as nDaysGT100, count(*) as nDays, calculated nDaysGT100/calculated nDays as pct_over_100
from daily_data
group by state, county, pollutant;
quit;
proc transpose data=summary out=want;
by state county;
id pollutant;
var nDaysGT100;
run;
proc transpose data=summary out=want_pct;
by state county;
id pollutant;
var pct_over_100;
run;