有没有办法通过对 SAS 中另一个变量的数据进行分组来创建新变量?
Is there a way to create a new variable by grouping data from another variable in SAS?
我是 SAS 的新手,想通过对来自另一个变量的数据进行分组来在我的数据集中创建一个新变量。我想通过将数字 1-5 分组为 1,将数字 6-10 分组为 2,对来自变量 EDUC 的数据进行分组以创建一个新变量 NEW_EDUC。非常感谢任何帮助!
EDUC NEW_EDUC
3 1
2 1
9 2
5 1
1 1
4 1
8 2
1 1
6 2
这似乎是一个基本的 IF/THEN statement。
data want; *output data set name is WANT;
set have; *input data set name is HAVE;
*create new variable;
if 1 <= EDUC <= 5 then NEW_EDUC = 1;
else if 6 <= EDUC <= 10 then NEW_EDUC = 2;
run;
你可以只使用简单的算术。除以 5 并四舍五入为整数。
data want;
set have;
NEW_EDUC = ceil(educ/5);
run;
或者您可以使用 proc sql 步骤,如果您更习惯 SQL:
proc sql;
create table want as
select
case when EDUC >= 1 and EDUC <= 5 then 1
when EDUC >= 6 and EDUC <= 10 then 2
else . end as NEW_EDUC
,*
from have;
quit;
我是 SAS 的新手,想通过对来自另一个变量的数据进行分组来在我的数据集中创建一个新变量。我想通过将数字 1-5 分组为 1,将数字 6-10 分组为 2,对来自变量 EDUC 的数据进行分组以创建一个新变量 NEW_EDUC。非常感谢任何帮助!
EDUC NEW_EDUC
3 1
2 1
9 2
5 1
1 1
4 1
8 2
1 1
6 2
这似乎是一个基本的 IF/THEN statement。
data want; *output data set name is WANT;
set have; *input data set name is HAVE;
*create new variable;
if 1 <= EDUC <= 5 then NEW_EDUC = 1;
else if 6 <= EDUC <= 10 then NEW_EDUC = 2;
run;
你可以只使用简单的算术。除以 5 并四舍五入为整数。
data want;
set have;
NEW_EDUC = ceil(educ/5);
run;
或者您可以使用 proc sql 步骤,如果您更习惯 SQL:
proc sql;
create table want as
select
case when EDUC >= 1 and EDUC <= 5 then 1
when EDUC >= 6 and EDUC <= 10 then 2
else . end as NEW_EDUC
,*
from have;
quit;