热线电话:13121318867

登录
首页精彩阅读SAS一些有用的语句
SAS一些有用的语句
2017-04-01
收藏

SAS一些有用的语句

今天本想继续介绍 summary过程的,但是发现别人整理的比我更好,于是周末再更,好好整理一下描述性统计分析及评分卡建模,今天给大家介绍一些sas有用的语句吧。


1.如果在同一程序中需要多次调用print语句,用%str可以节省程序书写;

%let print=%str(proc print data=sashelp.class;run;);

&print;

2.查看变量类型;

data a;

set sashelp.class;

w=vtype(age);

run;

3.结构查询语言;

select语句用来检索数据(order从句,format选项) ;

create语句创建表格; 

update语句增加或修改表格中列的数值(insert和delete语句插入和删除行,用alter语句增加、修改、删去列)

例子:

data employee;

input empname $ empcity $ empyears emptitle $ sales;

cards;

aa   beijing   3   engineer 2300

bb   shangqiu  2   teacher  3000

cc   changsha  32  worker   4000

dd   changsha  20  salesrep 1000

;

proc sql;

create table changsha as   /*对表格的操作*/

select empname,

empcity,

empyears format=yymmdd8.,/*对表格列的操作*/

sales*2 as totalsales,

/*count(empyears) as count_emp,*//*计算by排序文件的观测数、均值(avg)*/ 

/*10*(sales-empyears) as total,*//*有效的表达式*/

emptitle

from employee

where empcity='changsha'

order by empyears asc;  /*asc升序,desc降序*/

alter table changsha add gender char(1);/*对表格列的操作,添加性别变量,相似modify,drop*/

update changsha/*对表格值的操作:赋值*/

set gender='f' 

where empname in ('aa','cc');

update changsha

set gender='m'

where gender is missing;

insert into changsha    /*对表格行的操作,插入一条观测*/

values('ff', 'changsha',10, 8000,'teacher','m');

delete from changsha    /*对表格行的操作*/

where empyears>20;

quit;

proc print data=changsha;

run;

4.计算行数;

%let dsid=%sysfunc(open(sashelp.class,i));  

%let n=%sysfunc(attrn(&dsid,nobs));

%let rc=%sysfunc(close(&dsid));

%put obsnum=&n;

5.scan()用法:

data a;

arg='ABC.DEF(X=Y)';

word=scan(arg,-3);

/*put word;*/

run;

data c;

arg='ABC.DEF(X=Y)';

word=scan(arg,-20);

put word;

run;

data d;

input Arr $ & 22.;

ArrivalGate=scan(Arr,1,' ');

DepartureGate = scan(Arr,2,' ');

put  DepartureGate  ArrivalGate arr;

cards;

Arrival DepartureGates

;

run;

6.sysfunc()用法;

data a;

input group $ number point;

cards;

A 1 85

A 2 66

B 1 69

C 3 55

D 4 24

;

run;


%macro macro_name(group,number,point);

data out_&group.;

group="&group";

number=&number.;

point=&point.;

run;

%mend;


%macro test;

*打开存放参数的数据集;

%let dsid=%sysfunc(open(work.a,i));

%put &dsid;        *%put是在日志输出改宏变量的值;

*fetch是为了判断是否读取完数据集记录,若已读完,则返回-1,否则返回0,赋给宏变量rc;

%let rc=%sysfunc(fetch(&dsid));

%do %while(&rc=0);

*trim是去掉字符右边的空格,left是去掉字符左边的空格,getvarc(&dsid,1)是读取数据集的第一列,

读取第二列则为getvarc(&dsid,2),getvarc是针对字符,若该列是数值型的则为getvarn(&dsid,2);

%let group=%sysfunc(trim(%sysfunc(left(%sysfunc(getvarc(&dsid,1))))));

%let number=%sysfunc(trim(%sysfunc(left(%sysfunc(getvarn(&dsid,2))))));

%let point=%sysfunc(trim(%sysfunc(left(%sysfunc(getvarn(&dsid,3))))));

%put &group;     %put &number;     %put &point;


%macro_name(&group,&number,&point);*将参数传给调用你所需要运行的宏macro_name;


%let rc=%sysfunc(fetch(&dsid));        *同上fetch;

%put &rc;

%end;

%let rc=%sysfunc(close(&dsid));        *关闭数据集;

%put &rc;

%mend;


%test;


数据分析咨询请扫描二维码

最新资讯
更多
客服在线
立即咨询