给变量分组,常用的方法有:
1、if-else then ;
2、select-when ;
3.proc format.
下面以sashelp.class 为例,对不同age的同学分组。
注意 select when 的方法,很容易弄混。
http://blog.sina.com.cn/s/blog_41889b9001015htv.html
/***1. if -else then****/
data class;
set sashelp.class;
if age<12 then agegroup="A";
else if age<14 then agegroup="B";
else agegroup="C";
run;
/***2. select when***/
data class;
set sashelp.class;
select;
when(age<12) agegroup="A";
when(age<14) agegroup="B"; /*据jingju 的提醒,不必写成12〈=age<14)*/
other agegroup="C";
end;
run;
proc sql ;
create table class as
select *,case when(age<12) then"A"
when(age<14) then "B"
else "C"
end asagegroup
from sashelp.class;
quit;
/***3. proc fomat ***/
proc format;
value agefmt low-<12="A"
12-<14="B"
14-hight="C"
;
run;
data class;
set sashelp.class;
agegroup=put(age,agefmt.);
run;