如何从我的 table SAS 4GL - RETAIN 创建变量列?
how to create variable column from my table SAS 4GL - RETAIN?
我有以下格式的数据集。前 5 个变量已经存在。我需要创建 "NEED_TO".
这意味着在 MTH_SATTL 列上带有“1”的行之后,当 START 列上的日期比 DATA 列上的日期早时,然后填写 1 直到下一个 GROUP nad GR_ROZL。
我试过使用 RETAIN 语句,但我认为我的语法有问题
Data Start Group GR_ROZL MTH_SATTL Need_to
01OCT2019 . G11 2 0 0
01NOV2019 . G11 2 0 0
01DEC2019 . G11 2 0 0
01JAN2020 01JAN2020 G11 2 0 0
01FEB2020 01JAN2020 G11 2 1 1
01MAR2020 01JAN2020 G11 2 0 1
01APR2020 01JAN2020 G11 2 0 1
01OCT2019 . G11 3 0 0
01NOV2019 . G11 3 0 0
01DEC2019 . G11 3 0 0
01JAN2020 01JAN2020 G11 3 0 0
01FEB2020 01JAN2020 G11 3 0 0
01MAR2020 01JAN2020 G11 3 1 1
01APR2020 01JAN2020 G11 3 0 1
01OCT2019 . G12W 2 0 0
01NOV2019 . G12W 2 0 0
01DEC2019 . G12W 2 0 0
01JAN2020 01JAN2020 G12W 2 0 0
01FEB2020 01JAN2020 G12W 2 1 1
01MAR2020 01JAN2020 G12W 2 0 1
01APR2020 01JAN2020 G12W 2 0 1
规则"fill in 1 till to the next GROUP and GR_ROZL"表示您将希望通过处理获得。如果 BY
组是连续的但没有整理,您将需要 NOTSORTED
选项。您还需要一个 RETAIN
ed 变量,该变量在组开始时重置并根据您的逻辑有条件地分配。
示例:
data want;
set have;
by group gr_rozl NOTSORTED;
retain need_to 0;
* reset at the start of group;
if first.gr_rozl then need_to = 0;
* assign need_to if it is unassigned and meets assignment criteria;
* this ensures the assigned value is carried forward (via retain) until reset again;
if not need_to and start < data then need_to = 1;
* alternate way to assign using logic operator OR;
* need_to = need_to OR (start < data);
run;
要在 后续行 上设置 need_to
值,在 start < data
首次出现后,您可以使用额外的保留变量作为逻辑标志。
示例:
data have; input
Date Start Group $ GR_ROZL MTH_SATTL;
attrib date start informat=date9. format=date9.;datalines;
01OCT2019 . G11 2 0
01NOV2019 . G11 2 0
01DEC2019 . G11 2 0
01JAN2020 01JAN2020 G11 2 0
01FEB2020 01JAN2020 G11 2 1
01MAR2020 01JAN2020 G11 2 0
01APR2020 01JAN2020 G11 2 0
01OCT2019 . G11 3 0
01NOV2019 . G11 3 0
01DEC2019 . G11 3 0
01JAN2020 01JAN2020 G11 3 0
01FEB2020 01JAN2020 G11 3 0
01MAR2020 01JAN2020 G11 3 1
01APR2020 01JAN2020 G11 3 0
01OCT2019 . G12W 2 0
01NOV2019 . G12W 2 0
01DEC2019 . G12W 2 0
01JAN2020 01JAN2020 G12W 2 0
01FEB2020 01JAN2020 G12W 2 1
01MAR2020 01JAN2020 G12W 2 0
01APR2020 01JAN2020 G12W 2 0
run;
data want;
set have;
by group gr_rozl NOTSORTED;
retain need_to need_to_flag 0;
* reset at the start of group;
if first.gr_rozl then do; need_to = 0; need_to_flag = 0; end;
* set retained value for fill in;
if not need_to and need_to_flag then need_to = 1;
* set flag for setting retained value on subsequent rows;
if mth_sattl and not need_to_flag and start < date then need_to_flag = 1;
drop need_to_flag;
run;
我有以下格式的数据集。前 5 个变量已经存在。我需要创建 "NEED_TO".
这意味着在 MTH_SATTL 列上带有“1”的行之后,当 START 列上的日期比 DATA 列上的日期早时,然后填写 1 直到下一个 GROUP nad GR_ROZL。
我试过使用 RETAIN 语句,但我认为我的语法有问题
Data Start Group GR_ROZL MTH_SATTL Need_to
01OCT2019 . G11 2 0 0
01NOV2019 . G11 2 0 0
01DEC2019 . G11 2 0 0
01JAN2020 01JAN2020 G11 2 0 0
01FEB2020 01JAN2020 G11 2 1 1
01MAR2020 01JAN2020 G11 2 0 1
01APR2020 01JAN2020 G11 2 0 1
01OCT2019 . G11 3 0 0
01NOV2019 . G11 3 0 0
01DEC2019 . G11 3 0 0
01JAN2020 01JAN2020 G11 3 0 0
01FEB2020 01JAN2020 G11 3 0 0
01MAR2020 01JAN2020 G11 3 1 1
01APR2020 01JAN2020 G11 3 0 1
01OCT2019 . G12W 2 0 0
01NOV2019 . G12W 2 0 0
01DEC2019 . G12W 2 0 0
01JAN2020 01JAN2020 G12W 2 0 0
01FEB2020 01JAN2020 G12W 2 1 1
01MAR2020 01JAN2020 G12W 2 0 1
01APR2020 01JAN2020 G12W 2 0 1
规则"fill in 1 till to the next GROUP and GR_ROZL"表示您将希望通过处理获得。如果 BY
组是连续的但没有整理,您将需要 NOTSORTED
选项。您还需要一个 RETAIN
ed 变量,该变量在组开始时重置并根据您的逻辑有条件地分配。
示例:
data want;
set have;
by group gr_rozl NOTSORTED;
retain need_to 0;
* reset at the start of group;
if first.gr_rozl then need_to = 0;
* assign need_to if it is unassigned and meets assignment criteria;
* this ensures the assigned value is carried forward (via retain) until reset again;
if not need_to and start < data then need_to = 1;
* alternate way to assign using logic operator OR;
* need_to = need_to OR (start < data);
run;
要在 后续行 上设置 need_to
值,在 start < data
首次出现后,您可以使用额外的保留变量作为逻辑标志。
示例:
data have; input
Date Start Group $ GR_ROZL MTH_SATTL;
attrib date start informat=date9. format=date9.;datalines;
01OCT2019 . G11 2 0
01NOV2019 . G11 2 0
01DEC2019 . G11 2 0
01JAN2020 01JAN2020 G11 2 0
01FEB2020 01JAN2020 G11 2 1
01MAR2020 01JAN2020 G11 2 0
01APR2020 01JAN2020 G11 2 0
01OCT2019 . G11 3 0
01NOV2019 . G11 3 0
01DEC2019 . G11 3 0
01JAN2020 01JAN2020 G11 3 0
01FEB2020 01JAN2020 G11 3 0
01MAR2020 01JAN2020 G11 3 1
01APR2020 01JAN2020 G11 3 0
01OCT2019 . G12W 2 0
01NOV2019 . G12W 2 0
01DEC2019 . G12W 2 0
01JAN2020 01JAN2020 G12W 2 0
01FEB2020 01JAN2020 G12W 2 1
01MAR2020 01JAN2020 G12W 2 0
01APR2020 01JAN2020 G12W 2 0
run;
data want;
set have;
by group gr_rozl NOTSORTED;
retain need_to need_to_flag 0;
* reset at the start of group;
if first.gr_rozl then do; need_to = 0; need_to_flag = 0; end;
* set retained value for fill in;
if not need_to and need_to_flag then need_to = 1;
* set flag for setting retained value on subsequent rows;
if mth_sattl and not need_to_flag and start < date then need_to_flag = 1;
drop need_to_flag;
run;