set语句:
set语句从一个或多个sas数据集中读取观测值并实现纵向合并,每一个set语句执行时,sas就会读一个观测到pdv中,一个daya步可以有多个set语句,每个set语句可以跟多个sas数据集,多个set语句含有多个数据指针。
set会将输入数据集中的所有观测值和变量读取,除非你中间执行其他步骤
SET<SAS-data-set(s)<(data-set-options(s) )>><options>;
(data-set-options) specifies actions SAS is to take when it reads variables or observations into the program data vector for processing.
Tip:Data set options that apply to a data set list apply to all of the data sets in the list. Data set options specify actions that apply only to the SAS data set with which they appear. They let you perform the following operations:
主要的功能是以下四天,并给出相关例子
renaming variables ex--> set sashelp.class(rename = (name = name_new));
selecting only the first or last n observations for processing sashelp.class(where =(sex=‘M‘));
dropping variables from processing or from the output data set sashelp.class(drop =name sex);sashelp.class(keep=name sex);
specifying a password for a data set
输出两个数据集
data d1(keep = name) d2(keep = name sex);
set sashelp.class(keep = name sex);
run;
IN=选项应用
IN本身不是变量,所以不能通过赋值语句获得,IN=的最大作用是标识不同的数据集
data one; input x y$; cards; 1 a 2 b ; run; data two; input x z$; cards; 3 c 2 d ; run; data me; set one(in=ina)two(in=inb); if ina=1 then flag=1;else flag=0; run;
res:
data me;
set sashelp.class(firstobs=3 obs=6); /*读取第三到第六个变量*/
run;
*获取数据集中的总观测数;
data me(keep = total);
set sashelp.Slkwxl nobs=total_obs; *if 0 then set sashelp.Slkwxl nobs=total_obs;改进语句,因为sas是先编译再执行,所以可以选择不执行,只获取编译的信息就足够了
total = total_obs;
output;
stop;
run;
1:程序编译时首先读nobs=选项,该选项在头文件中,nobs=total_obs将总观测数传给临时变量total_obs
2:pdv读入数据集,并把所有变量放入pdv。
。。。。省略
POINT=选项 取指定的一条观测
data me;
n=3;
set sashelp.class point=n;
output;
do n=3,6,10;
set sashelp.class point=n;
output;*获取多个指定行的观测;
end;
set sashelp.class nobs = last point=last;
output; *获取最后一行观测值;
stop;
run;
point=n对应的是变量,不能直接赋值数字,省略stop后会让程序进入死循环,不用stop语句sas无法判断该数据指针是否指向了最后一条观测,从而会陷入死循环。如果不用output,会得不到数据集,point和stop一般是连在一起使用
_N_的使用
data d1 d2;
set sashelp.class;
if _n_ le 10 then output d1;
else output d2;
run;
set读取序列数据集合的一些注意事项:
set goods1:;*读取所有以good1开头的文件,比如goods12 goods13; set sales1-sales4; set sales1 sales2 sales3 sales4;*这两条语句等价; /* set sales1-sales99; *合法; set sales001-sales99; *不合法,如果以0开头,那么后面的文件的数字要比前面的文件的数值的位数多,至少是相等; */ set cost1-cost4 cost2: cost33-37; *可行; /* these two lines are the same */ set sales1 - sales4; set ‘sales1‘n - ‘sales4‘n; /* blanks in these statements will cause errors */ set sales 1 - sales 4; set ‘sales 1‘n - ‘sales 4‘n; /* trailing blanks in this statement will be ignored */ set ‘sales1 ‘n - ‘sales4 ‘n;
sas数据操作