有条件地创建 SAS 数据子类别的 SAS do-loop 问题

SAS do-loop question for creating SAS data subcategories conditionally


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 timeseries,乍一看似乎适用,但我不太了解它。

听起来您要多次复制数据。最好只添加列以便能够快速过滤到您想要的集合。

如果周期重叠,则为每个周期添加一个单独的列。

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 ;
   ...

如果句点不重叠,那么您可以只使用一个带有句点标识符的变量。

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 is a good introductory reference on formats in SAS, and of course the documentation reference.