问题描述
data period;
set output.Sample_Y_n_deln;
if delnum >= 181 and delnum <= 184;
run;
data period2;
set output.Sample_Y_n_deln;
if delnum >= 185 and delnum <= 188;
run;
data period3;
set output.Sample_Y_n_deln;
if delnum >= 189 and delnum <= 192;
run;
有没有办法使用某种循环来自动执行此操作?本练习的重点是根据 delnum 为我的数据集获取季度时间片,delnum 是该数据集特定的数字格式的日期。
我听说过 proc 时间序列,乍一看似乎适用,但我对此知之甚少。
解决方法
听起来您要多次复制数据。最好只添加列以便能够快速过滤到您想要的集合。
如果期间重叠,则为每个期间添加一个单独的列。
data periods;
set output.Sample_Y_n_deln;
period1 = (181 <= delnum 184);
period2 = (185 <= delnum 188);
period3 = (189 <= delnum 182);
run;
您可以在分析步骤的 WHERE 子句中使用新变量。
proc means data=periods ;
where period1 ;
...
如果期间不重叠,那么您可以只使用一个带有 PERIOD 标识符的变量。
data periods;
set output.Sample_Y_n_deln;
if (181 <= delnum 184) then period=1;
else if (185 <= delnum 188) then period=2;
else if (189 <= delnum 182) then period=3;
run;
,
另一种选择是使用格式,但这取决于您接下来要做什么。例如,如果您想要按时间段进行摘要,这可以正常工作。还有多种方法可以在各种其他过程和数据步骤中使用该格式。
proc format;
value delnum_fmt
181 - 184 = "Period 1"
185 - 188 = "Period 2"
189 - 192 = "Period 3"
other = "Outside Period of Interest"
;
run;
proc freq data=output.sample_y_n_deln;
table delnum;
format delnum delnum_fmt.;
run;
Here 是 SAS 格式的很好的介绍性参考,当然还有 documentation reference。