sas宏语言详细教程
所有的变量值都只能是字符
data sales2001;
set sashelp.orsales;
if year=2001;
run;
proc print data=sales2001;
title 'sales in 2001';
footnote1 'create-2017';/*脚注,标题,对表进行修改编辑,可以直接写footnote,如果有多个脚注,可以加1,加2*/
footnote2 'on then win system using sas 9.4';
run;
/*使用宏语言改写*/
%let year=2001;
/*定义了宏变量YEAR,并且赋值2001,
在后面使用&year来代替2001*/
data sales&year;
set sashelp.orsales;
if year=&year;
run;
proc print data=sales&year;
title 'sales in &year';/*这里有问题*/
footnote1 'create-2017';/*脚注,标题,对表进行修改编辑,可以直接写footnote,如果有多个脚注,可以加1,加2*/
footnote2 'on then win system using sas 9.4';
run;
%let town=detroit;
data city;
input city $ state $;
cards;
detroit mi
chicago il
;
run;
data city2;
set work.city;
where city='detroit';
run;
%let town=detroit;
data city;
input city $ state $;
cards;
detroit mi
chicago il
;
run;
data city2;
set work.city;
where city="&town";/*detroit是city这个列的一个值,所以要用字符串,因此如果要用宏变量,
也要加引号,需要加双引号,单引号不对,后面写了单引号内的宏变量不会被解析,双引号内的宏变量才会被解析*/
run;
在这里插入图片描述
在这里插入代码片
%let x=100;
%let y=%eval(&x+20);整型函数
%put y=&y;
%put z=%eval(&x+&y);
返回位置,从1开始,如果有多个重复的,则返回第一个
%let city=shanghai;
%let loc=%index(&city,g);
%put weizhi=&loc;
%put weizhi2=%index(&city,a);
宏过程
宏过程的定义
%macro print;
proc print data=sashelp.class;
run;
%mend print;
/*宏过程的调用,%宏名称*/
%print;
%macro add_xy(x,y);
%let sum_xy=%eval(&x+&y);/*这里不能直接x+y,会报错
%put &sum_xy;
%mend add_xy;
%add_xy(5,12);