刚刚学sas,今天记下了一些data步的技巧,还有待补充,希望多多指教,多多交流~~欢迎讨论^^
/*input:命令输入(必须在变量最后,否则报错)*/
data list2;
informat header $30. name $15.;
input header= name=;
datalines;
header= age=60 AND UP name=PHILIP
;
run;
proc print;
run;
/**存储被编辑的程序**/
data class/view=class;
set sashelp.class;
run;
data class1(keep=name age weight)/pgm=work.cl;
set sashelp.class;
run;
data pgm=work.cl;
run;
/*指针用法_page_把指针移到新一页第一行*/
data class;
set sashelp.class;
run;
proc sort data=class;
by sex;
run;
data _null_;
set class;
by sex;
file print;
put name 1-8 @12 sex;
if last.sex then put // 'This is the last of' sex $ _page_;
run;
/*基于某些条件子集的生成*/
/*观测值子集*/
data sex0 sex1;
set sashelp.class;
if sex='男' then output sex0;
else if sex='女' then output sex1;
run;
proc print data=sex0;
/*同时输出多个数据集?*/
run;
/*生成变量子集*/
data sex(keep=name sex) age(keep=name age);
set sashelp.class;
run;
proc print data=age;
run;
/*cards & cards4(数据行中含有分号) 略*/
/*put (和input对应即可)*/
/*不常用但有意思的*/
data _null_;
input x y z;
put 130* '_';
put _all_;
cards;
1 3 4
4 5 6
;
run;
/*put列表输出*/
data a;
x1=1233.344;
x2=134342.05;
x3=3423.123;
put x1: comma10.2 x2:10.1 x3:10.4;
put (_all_)(3*comma10.2);
put (x1-x3) (3*comma10.2);
run;
/*put格式化输出*/
data a;
input name & $10. bldg $ room;
put name @20 (bldg room) ($1."-",3.);
cards;
GU BEIJING J 125
ROBERT US C 233
;
run;
/*查看sas自带的数据集说明*/
proc contents data=sashelp.prdsale;
run;
data prdsale;
set sashelp.prdsale;
run;
proc sort data=prdsale;
by year quarter month;
data b;
set prdsale;
by year quarter month;
if last.month; /*保留每月最后一个观测值*/
run;
/*set语句*/
/*set:读入数据集3 5 7 4个观测*/
data a;
set class;
obs=_n_;
data b;
do n=3,5,7,4;
set a point=n;
output;
end;
stop; /*使用point时,指针在遇到最后一行观测才会终止,因此要用stop强制停止,防止陷入死循环*/
proc print;
run;
/*连续两个set语句*/
data one;
input x y$;
cards;
1 Groucho
3 Harpo
5 Kart
;
data two;
input x y$;
cards;
2 Chico
4 Zeppo
;
data three;
set one;
if x=3;
set two point=_n_;
run;
/*按等差数列读取行观测*/
data a;
do obsnum=1 to last by 2;
set class point=obsnum nobs=last;
output;
end;
stop;
run;
/*_n_正在处理的观测值序号,nobs:数据集的总观测值,end:是否到达最后一个观测,后两个为set的option*/
/*数据集的最后一个观测*/
data a;
set sashelp.class end=last_obs;
if last_obs;
x=last_obs;
put 'last observation';
run;
/*保留部分观测*/
data males;
set sashelp.class;
where sex='男';
run;
data males;
set sashelp.class;
if sex='男' then output; /*去掉output也可*/
run;
/*对行进行依次连接*/
proc sort data=one;
by x;
run;
proc sort data=two;
by x;
run;
data a;
set one two;
by x;
proc print;
run;