第一讲、SAS课程介绍
一、数据类型
a. 横截面数据集(cross-sectional data set):也就是说,由个人、家庭、企业、国家或一系列其他单位收集的样本组成的数据集(应忽略小时差)
b.时间序列数据集(time series data set)
c.混合横截面数据集(pooled cross section data set)
d.综列数据集(panel data set)
二、SAS模块介绍
Base SAS:
1.数据管理功能
2.基础统计计算功能
3.报表生成和图形显示功能
SAS/STAT 统计分析
SAS/INSIGHT 数据探索
SAS/ASSIST 易学易用模块学易用模块
SAS/ETS 时间序列处理
SAS/OR 运筹学和工程管理
SAS/QC 质量控制
SAS/IML 矩阵运算
SAS/GRAPH 绘画
SAS/AF 面向对象编程(OOP)应用开发工具,扩展SAS的功能
SAS/EIS 企业信息系统开发面向对象编程的开发工具
SAS/ACCESS 数据访问
SAS/TOOLKIT 扩展工具,将其他软件编写的程序转换为SAS的组成部分
SAS/Warehouse Administrator 数据仓库管理
SAS/Enterprise Miner 数据挖掘
SAS/Text Miner 文本挖掘
SAS/SHARE 并发库并发控制专用软件
SAS/CONNECT 使各平台SAS系统分布式处理,有效利用各平台机械资源
SAS/GIS 显示和分析地理信息系统和空间数据
SAS/SPECTRAVIEW 数据可视化分析工具通过几何成像分析多维数据
SAS/Intrnet 提供构建基础SAS企业内部网络支持
SAS/FSP 交互式菜单系统快速数据处理
SAS/CALC 多维电子表格软件功能完善
SAS/ENGLISH 利用自然英语获取信息的能力
SAS/SECURE 网络传输加密
SAS/OLAP SERVER 呈现多维数据存储和汇总结果
SAS/GENETICS: SAS该公司的第一个科研应用软件用于遗传学,有五个分析过程
SAS/High-Performance Forecasting:提供快速高效的预测结果
SAS/C, SAS/C 针对IBM开发大型机发工具SAS/LAB 特别是向导数据分析软件day-to-day类型数据
三、SAS工作空间
程序窗口(PROGRAM EDITOR)操作记录窗口(LOG)输出窗口(OUTPUT)
第二讲、SAS系统安装,SAS操作环境界面
1、界面介绍
2、Fill values 功能
3、Extract 功能
4、Edit/windows/tool菜单使数据探索丰富多彩(注意选项设置)
5.主菜单打开分布、拟合、多样化等窗口后Tables、Graphs、Curves菜单开放。
其他功能:
1、分布(输出大多数常用的统计量)
2.拟合(回归分析)
3.多元(方差分析)
第三讲、Insight模块
第四讲、SAS编程的基本语法
div>一、SAS程序的书写格式
1、SAS程序由语句组成,语句用分号结束;
2、 SAS语句可以从某一行的任意位置开始;3、几个SAS语句可以写在同一行上,但每句要使用分号;
二、SAS程序的书写格式
1、SAS程序由语句组成,语句用分号结束;
2、 SAS语句可以从某一行的任意位置开始;
3、几个SAS语句可以写在同一行上,但每句要使用分号;
4、一个语句也可以写成几行,只要语句中的单词不被断开就可以。在一个语句中各个单词之间至少要有一个空格;
5、SAS程序中一般不区分大小写(字符串中要区分大小写);
6、用 /*注释的内容*/ 来对程序进行注释;
三、SAS程序的结构
1、SAS数据集 样本(观测、记录)、变量(字段、域)、表(数据集)、 数据库;4、变量属性(名称、类型、长度、输入格式、输出格式、标签)
类型:数字型、字符串型 输入
格式:数据被SAS读取的格式
输出格式:数据呈现给人们的格式
data oranges;
input variety $ flavor texture looks;
/*total=flavor+texture+looks;*/
total=flavor+texture+looks;
label total="总数";
cards;
navel 9 8 6
temple 7 7 7
valencia 8 9 9
mandarin 5 7 8
;
proc sort data=oranges;
by descending total;
run;
proc print data=oranges;
var _numeric_;
/* var _character_;*/
title '对ORANGES数据集的品尝检验结果';
run;<span style="font-size:24px;">
</span>
四、SAS文件及命名
/*libname zhao clear;
libname _all_ clear;
libname zhao ('e:\data\' 'e:\');
libname zhao v6 'e:\data'*/
libname zhao 'e:\sastemp';
data zhao.aa;
input a ;
cards;
1
;
run;
第五讲、建立SAS数据集的5种方法
1、data步自己输入数据建立。Orange例 2、data步利用set 语句建立。Set例 3、从外部调入。Infile例 4、利用“导入数据”功能建立。Id.xls例、data.txt例 5、利用EFI(external file interface)功能导入。Column.dat例、tests.dat例、comma.dat例
第六讲、Analyst分析家模块
1、界面介绍
2、浏览与编辑模式 3、生成示例数据与抽样 4、程序清单 5、假设检验
第七讲、数据步(DATA 步)语法详解(1)
一、SAS运算符(operator例)
算术运算符: + - * / ** 比较算符: =(EQ) ^=(NE) >(GT) <(LT) >=(GE) <=(LE) in 逻辑算符: &(AND) |(!)(OR) ^(~)(Not) 其它:><(最小值) < >(最大值) ||(!)(连接)
data oranges;
input variety $ flavor texture looks;
total=flavor+texture+looks;
if variety in ('navel','valencia') /* in应用 */
then total=total*10;
if (flavor<texture & variety='mandarin') /* & 符号 */
then total=total*100;
a=flavor<>texture**2; /*取小的一个*/
b=variety||'.var'; /*连接*/
label total="总数"; /*显示标签 默认为total*/
cards;
navel 9 8 6
temple 7 7 7
Valencia 8 9 9
mandarin 5 7 8
;
proc sort data=oranges;
by descending total;
run;
proc print data=oranges;
var _numeric_;
/* var _character_;*/
title '对ORANGES数据集的品尝检验结果';
run;
二、SAS程序的结构
1、按机器使用的字符排列次序(ASCⅡ或Unicode)从左到右被比较。 2、两个不等长的字符串被比较时,系统在较短的字符串后自动添上空格 3、在比较符后加冒号,则只比较首字母三、运算次序
第一级:** (+ -)前缀 ^ >< < > 第二级:* / 第三级:+ - 第四级:|| 第五级:其余比较算符 第六级:& 第七级:|四、SAS函数
1、数学函数:data bbb;
input x1-x5 x6 $ date yymmdd15.;
/*format date yymmdd10.;*/
*format date weekdate12.;
x7='o';
x8='I am a student';
/*以下是数学函数*/
a1=max(x1,x2);
a2=sum(x1,x2);
a3=sign(x3);
a4=sqrt(x5);
a5=mean(x1,x2);
/*以下是截取函数*/
b1=floor(a5);
/*以下是字符函数*/
c1=index(x6,x7);
c2=substr(x6,3,3);
c3=scan(x8,2);
c4=upcase(x8);
c5=compress(x6,x7);
/*以下是时间函数*/
d1=weekday(date);
d2=year(date);
d3=qtr(date);
d4=day(date);
/*以下是概率函数*/
e1=probnorm(0);
e2=n(of x1-x5);
e3=nmiss(of x1-x5);
e4=var(x1,x2);
e5=std(x1,x2);
/*以下是随机数函数*/
f1=ranuni(1);
cards;
1 2 3 5 -6 amazon 2004/9/22
3 5 0 8 0 box 1960/1/2
9 10 -7 9 4 check 1960/1/1
7 6 . 1 8 delete 1959/12/31
-2.5 -2.3 3 1 2 desk 2005/9/29
;
run;
proc print;
run;
第七讲、数据步(DATA 步)语法详解(2)
文件操作语句 运行语句 控制语句 信息语句
一、DATA步流程
1、DATA语句标志了数据步开始,并指定了数据步结束时要生成的数据集名字。 2、每次读入一行数据,读入后执行数据步中的其它语句,循环数据步读取整个数据。 3、_N_变量表示DATA步已经执行的次数 4、_error_变量值为1时表示程序出错 Flow例data flow;
/*put x= y= z= _n_;*/
input x y;
z=x+y;
put x= y= z=;
put _n_= _error_=;
cards;
10 20
100 200
1000 2000
;
run;
data flow1;
put x= y= z=;
set flow;
put x= y= z=;
run;
二、文件操作语句
data new1(drop=z);
set flow;
run;
data new2(keep=x);
set flow;
run;
data new3(label='the new3 dataset');
set flow;
run;
data new4(rename=(x=xx y=yy));
set flow;
run;
proc print data=new1;
run;
proc print data=new2;
run;
2、--数据集名字:(data2例) 1)、data语句后无数据集名字,则自动生成 data1, data2等为数据集名字,相当于data _data_; 2)、data _null_;不产生实际的数据集,只是把内容暂存于内存,以供put语句等读取结果,可以节约计算机资源 3)、data _last_;以最近产生的数据集命名并取代其内容。 3、
1)、分组格式: input (g1-g5) (3*7.2 , 2*5.2);
input (a b) ( $5.)
input (a b) ($ , 5.)
@-固定该数据行;用于需要多个input语句同时读取一行数据。
2)、@@-无需分行而按变量连贯读数据
input@@例,Input@例
3)、利用@n +n #n 进行指针控制
input1例
data ;
input type $ @;
if type='c' then input course $ prof $;
else if type='s' then input name $ id 3.2;
cards;
c math zxs
s zhao 58888
;
proc print;
run;
4)、几
个特殊符号的用法(: & ~)
读值时第一次遇到分隔符、或是数据行末尾、或是读满列数而结束
字符型输入值可以嵌有一个或几个空格
字符型输入值保留引号(与infile 语句中选项dsd一起使用才有效) Input2例
data topics9;
infile datalines dsd;
input speaker :$15. title ~$40. location & $10.;
datalines;
Whitfield,'Looking at Lift',Blue Room
Puentes,"Life After the Revolution",Red Room
Townsend,"Peace in Our Times",Green Room
;
proc print ;
run;<span> </span>
Cards语句与datalines语句可以通用 如果输入数据中含有分号,可用cards4语句或datalines4语句,同时,数据结尾用4个分号表示数据输入结束 Cards4例
data;
input number citation $50.;
cards4;
1 Berry
2 LIN ET AL., 1995; BRADY, 1993
3 BERG, 1990; ROA, 1994; WILLIAMS, 1992
;;;;
run;
proc print;
run;
Put语句负责在log窗口输出一些结果,file print选项可把输出转移到output窗口 Put _infile_ (输出最新的数据行到SAS log 窗口) Put _all_ (输出所有变量的值) Put _page_ (输出新页) Put例、put1例
data;
/*file print;*/ /* change the output window*/
put 1230*'SAS学习';
run;
proc print ;
run;
data put;
input x y;
z=x+y;
put _infile_; /*put _all_;*/
put _n_=;
cards;
10 20
100 200
1000 2000
;
run;
File print 把put语句产生的输出结果转移到output窗口
在DATA步中,SAS系统对每个BY组创建两个临时变量: First .variable Last .variable
它们用来区别每个BY组的第一个和最后一个观测
By例
data by;
input state $ city $ month x;
cards;
nc a 1 3.02
nc a 2 2.1
nc a 3 4.03
nc b 1 5.03
nc b 2 6.3
nc b 3 7.3
va c 1 8.1
va c 2 8.2
va c 3 9.3
va d 1 10.8
va d 2 11.1
va d 3 31.2
;
run;
data by1;
set by;
by state;
file print;
if x=8.1 then put _all_;
run;
/*data by2;
set by;
by state city month;
file print;
if x=8.1 then put _all_;
run;
Set语句可以串接多个数据集,形式如下 Data 数据集; set 数据集A 数据集B ……; <by 变量;> Run; Rename选项负责改名;
in选项产生变量负责记录观测来自该数据集; 例set2
data a;
input id ming $ sex $ @@;
cards;
1 MARY F 3 ANN F 4 TOM M
;
data b;
input id name $ sex $ @@;
cards;
2 JOSE F 5 ERIE M 6 MAY F 1 MARY M
;
data result;
set a(rename=(ming=name)) b(in=inb);
*by id;
if inb=1 then bonus=100;
run;
proc print;
title'串接数据集';
run;
Cards语句与datalines语句可以通用 如果输入数据中含有分号,可用cards4语句或datalines4语句,同时,数据结尾用4个分号表示数据输入结束 Cards4例
data a;
input id ming $ sex $ @@;
cards;
1 MARY F 3 ANN F 4 TOM M
;
data b;
input id name $ sex $ @@;
cards;
2 JOSE F 5 ERIE M 6 MAY F 1 MARY M
;
data result;
set a(rename=(ming=name)) b(in=inb);
*by id;
if inb=1 then bonus=100;
run;
proc print;
title'串接数据集';
run;
使用Merge语句并接多个数据集,形式如下 Data 数据集; merge 数据集1 数据集2 ……; by 变量; run; 见merge例
data person;
input name $ sex $;
cards;
MARY F
ANN F
TOM M
;
data place;
input name $ city $ region;
cards;
MARY MIAMI 2
ANN TAMPA 6
JOSE ERIE 5
MARY TAMPA 7
;
/*proc sort data=person;
by name;
proc sort data=place;
by name;*/
data result;
merge person place;
*by name;
proc print;
title'DATA SET RESULT';
run;
Update语句用一个升级数据集中的观测来修改一个主数据集。 Update语句一定要与BY语句一起使用,BY语句给出了合并观测时共同变量的名字 主数据集中的共同变量必须是单值的,即不能有两个或两个以上的观测其共同变量值相同 Update例
data a;
input id name $ sex $;
cards;
1 zxs m
2 zjy m
3 zm f
;
data b ;
input id name $ sex $ sales;
cards;
1 zxs f 34
;
data c;
update a b;
by id ;
run;
proc print;
run;
1)、Delimiter (DLM)选项:规定一个字符替代空格作为分隔符。 2)、Firstobs选项 :不是从文件的第一个记录开始,而是从指定行开始读取记录 3)、Obs选项:规定用户想从输入文件中连续读取的最后一个记录号 Infile(dlm)例、 Infile(obs)例
data new1;
infile cards delimiter=',';
input x y z;
cards;
1,2,3
4,5,6
;
data new2;
infile cards dlm='ab';
input x y z;
cards;
1aa2ab3
4bb5ba6
7a8b9
;
run;
proc print data=new1;
run;
proc print data=new2;
run;
data abc;
infile 'e:\SAS\data\数据处理\column.dat' firstobs=2 obs=3;
input a $9.;
run;
proc print;
run;
DSD选项: 1、使最外层引号括起的内容当成整个字符串输入,并去除最外层引号 2、默认分隔符为逗号 3、两个相邻的分隔符意味着前一个分隔符后的字段为缺失值 Infile(dsd)例 (回顾input2例)
data;
infile cards dsd;
input x:$15. y;
cards;
"i'm a student",,
;
run;
proc print;
run;
当行末尾数据宽度少于规定宽度时
Flowover: 默认选项,将下一条记录读入
Missover:将变量置为缺失
Truncover:将数据直接读入,不管宽度是否少于规定宽度
Stopover:系统终止数据步执行,报告出错
Lrecl=n选项:设定源数据文件行的物理长度,缺省为256,最大可设为32767
Pad | Nopad选项:pad选项指当宽度少于规定宽度时,在后面填充空格至Lrecl=n规定的行长度,缺省为nopad
data;
infile 'e:\SAS\missover.dat';/*flowover missover truncover stopover pad*/
input x 5.;
run;
proc print;
run;
三、运行(算)语句
data jn;
input a b c;
file print;
put _n_;
if a>3 then delete;
*put _n_;
*total=a+b;
cards;
1 2 3
3 3 2
5 3 1
3 3 3
;
proc print;
run;
data ins;
input id 1-3 reject 8-10 #2 idc 1-3 pass;
if id ne idc then lostcard;
/*如果是delete语句,
则input语句对应的两条记录同时删除*/
cards;
301 32
301 61432
302 53
302 83171
400 92845
411 46
411 99551
;
proc print;
title '每个观测包括两个数据行';
run;
data check;
input ssn 1-3 pay;
if ssn=222 then stop;/*abort return;stop*/
put _all_;
cards;
111 100
222 200
444 300
;
data a;
input b;
cards;
12345
;
proc print data=a;
run;
data aa;
set sasuser.business;
where employs>50;
*where industry='Food';
*where employs between 50 and 70;
*where employs in (61,62);
*where employs is missing;
*where industry ? 'Ele'; /* ? means "contains"*/
*where industry like '%t%'; /* % can substitute many char*/
*where industry like 'O_l'; /* _ can substitute one char*/
run;
proc print;
run;
DATA步的每次迭代,后台自动使用output语句,但如果程序中主动加上output语句,即相当于取消自动output语句,变成在条件符合时输出。
output例
data;
input a b;
if a=1 then output;
put _n_;
cards;
2 3
1 5
2 4
1 6
;
run;
proc print;
run;
用到output语句其他情况:output123例
1.从一个输入的数据文件中,创建几个SAS数据集
2.从输入的每个数据行中,创建二个或更多个观测
3.把几个输入观测组合并成为一个观测
后面接名字表示输出到指定数据集,但名字必须也在DATA后出现
data year81 year82 year83;
input year x1-x2;
if year=1981 then output year81;
else if year=1982 then output year82;
else if year=1983 then output year83;
cards;
1981 1 2
1982 2 3
1983 3 4
;
run;
proc print;
run;
quit;
data repeat;
infile cards;
input subject $ meal1-meal3;
drop meal1-meal3;
mea=meal1;output;/*把meal1的值赋给mea*/
mea=meal2;output;
mea=meal3;output;
cards;
a 2 5 4
b 3 6 2
;
proc print;
run;
data pay;
input area amount;
cards;
111 100
111 25
333 160
333 80
777 142.66
;
proc sort data=pay;
by area;
data subtotal;
set pay;
by area;
if first.area then total=0;
total+amount;
drop amount;
if last.area then output;
run;
proc print;
run;
data a;
seed1=161321804;/*赋予随机数种子初始值*/
seed2=135279821;
do i=1 to 5;
x1=ranuni(seed1);
x2=ranuni(seed2);
output;
end;
proc print;
title '使用随机数函数';
run;
data b;
seed3=161321804;
seed4=936674311;
do i=1 to 5;
call ranuni(seed3,x3);/*seed3存放种子值,x3为随机数变量名*/
call ranuni(seed4,x4);
output;
end;
proc print;
title '调用随机数子程序,产生2个
随机数流,并能观测到当前随机数种子值';
run;
data _null_;
call system('dir *.*');
run;
四、控制语句
data do;
input x ;
file print;
if x>5 then do;
y=x*10;
put x= y=;
end;
cards;
1
6
7
;
proc print;
run;
data a;
input x @@;
do i=1 to 4;/*把i改成其他字符同样可行*/
y=4*x;
output; /*如果略去output,则只输出最后一次循环的结果*/
end;
cards;
11 22 33 44 55 66
;
run;
proc print;
run;
data iteratel;
input x @@;
exit=10;
do i=1 to exit;
seed=1; /*初始种子值*/
call ranuni(seed,y);
z=x*y;
if z>25 then i=exit;
output;
end;
cards;
5 1000 2500
;
proc print;
run;
data while;
n=0;
do while (n<4);
file print;
put n=;
n+1;
end;
run;
data until;
n=0;
do until(n>=4); /*试试改成n>=0*/
file print;
put n=;
n+1;
end;
run;
data old;
input grade $ @@;
cards;
A B1 B2 B3 C D
;
run;
data new;
set old ;
x=1000;
select(grade);
when('A') salary=x*1.5;
when('B1','B2','B3') salary=x*1.3;
when('C') salary=x*1.1;
otherwise salary=x;
end;
run;
PROC PRINT;
RUN;
data;
input x @@;
if x=1 then y=x;
else if 2<=x<=4 then y=x*10;
else if 5<=x<=7 then y=x*100;
else y=x*1000;
cards;
1 2 3 4 5 6 7 8
;
proc print;
run;
data aa;
input x @@ ;
if x then y=x;
cards;
1 0 . 4
;
run;
proc print;
run;
data survey;
input x y z;
if x=y then return;/*试试把return换成output*/
x=y+z ;
a=x**2;
cards;
1 1 3
2 3 3
;
proc print;
run;
data info;
input x @@;
file print;
if 1<=x<=5 then goto ok;
put x;/*不是属于IF的执行*/
count+1;/*不是属于IF的执行*/
*return;
ok:y+x;/*这句一直要执行*/
cards;
7 2 16 5 323 1 11
;
proc print;
run;
data;
input type $ wd station $ ;
elev=.;
if type='ALUV' then link calcu;
year=1985;
return;/*返回到data步开头*/
calcu:
if station='SITE_1' then elev=6650-wd;
if station='SITE_2' then elev=5500-wd;
return;/*返回到link语句的下面*/
cards;
ALUV 523 SITE_1
UPPA 234 SITE_2
ALUY 666 SITE_2
;
proc print;
run;
data zzz;
*drop i;
do i=1 to 5;
input name $ idno status $;
if status='PT' then continue ;
input benefits $10.;
output;
end;
cards;
Jones 9011 PT
Thomas 876 PT
Richards 1002 FT
Eye/Dental
Kelly 85111 PT
Smith 433 FT
HMO
;
PROC PRINT;
RUN;
data week;
input name $ idno start;
bonus=0;
do year=start to 1991;/*from start to year add 50 every year*/
if bonus ge 500 then leave;
bonus+50;/*如果在do循环里是类似累加,循环进行到exit+1*/
end;
cards;
Jones 9011 1990
Thomas 876 1976
Barnes 7899 1991
Harrell 1250 1975
Richards 1002 1990
Kelly 85 1981
Stone 091 1990
;
PROC PRINT;
RUN;
/*用途:计算复利的一个工具*/
data;
capital=2000;
do year=1 to 2;
capital+2000;
do month=1 to 2;
interest=capital*(0.075/12);
capital+interest;
end;
end;
run;
/*1.1:capital:4000 interest:25 total=4025
1.2:capital:4025 interest:25.15 total=4050.15
2.1:capital:6050.15 interest:37.81 total=6087.81
2.2:capital:6087.81 interest:38.04 total=6126*/
proc print;
run;
data _null_;
file print;
do mon='JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP';
select ;
when(mon='MAR'|mon='APR'|mon='MAY')put 'spring'mon=;
when(mon='JUN'|mon='JUL'|mon='AUG')put 'summer'mon=;
otherwise put 'fall'mon=;
end;
end;
run;
跳转代码(go to,link):需要暂停主代码,执行“标签”指明的其它代码 中止代码(return,continue,leave):负责某种形式的中止。
五、信息语句
当需要用同一种方法处理很多变量时,可以用数组语句定义这组变量为某个数组中的元素。 1.显示下标array语句:由一个数组名字,在数组中元素个数的说明,及元素列表组成。 2.隐含下标array语句:由一个数组名字、一个下标变量和列表名组成。
形如:
ARRAY array-name {subscript} <$> <<array-elements><(initial-values)>>
例 array simple{3} red green yellow;
array x{5,3} score1-score15;
array c{3}等价于array c{3} c1-c3
array test{3} t1 t2 t3 (90 80 70)
数组中的变量必须全是数值型或字符串型,array例
data;
array x{3} a b c (1 2 3);
file print;
put x{1}= x{2}= x{3}=;
run;
data;
array x{2,3} aa bb cc dd ee ff(1 2 3 4 5 6);
file print;
put x{2,1}=;
run;
data;
array x{2} $ aaa bbb('data' 'process');
file print;
put x{2}=;
run;
data;
array x{6:9,0:9} x60-x99;
file print;
put x{7,0};
run;
规定数组中每一维的上下界: array x{1:5,1:3} score1-score15;
{*}表示SAS系统通过数组中变量的个数来确定下标。可用dim(数组名)函数计算数组元素个数。 _temporary_建立一个临时数组元素列表 array(dim)例,array(temporary)例,array(prod)例
data zz;
input d1-d7;
array day(*) d1-d7;
do i=1 to dim(day);
day(i)=day(i)+10;
end;
drop i;
cards;
99 11 22 222 22 3 44
1 2 3 4 5 6 7
;
run;
PROC PRINT;RUN;
data a;
input var1 var2 var3;
cards;
10 20 30
100 . 300
. 40 400
;
run;
data b(drop=i);
set a;
array array1(3) _TEMPORARY_ (.1 .2 .3) ;
array array2(3) var1 var2 var3;/*把a数据集中的多个变量用数组来表示*/
do i=1 to 3;
if array2(i)=. then array2(i)=array1(i);
end;
proc print;
run;
data a;
input x1-x5;
cards;
1 2 3 4 5
2 3 4 5 6
4 5 6 7 8
;
run;
data b;
set a;
array test(5) x1-x5;
prod=0;
do i=1 to dim(test)-1;
if i=1 then prod=test(i);
prod=prod*test(i+1);
end;
proc print;
run;
形如:ARRAY array-name <(index-variable)> <$> array-elements <(initial-values)> 如果没有规定下标变量,SAS系统使用自动变量_i_作为下标变量,下标变量范围从1到这个数组元素的个数 如:array x x1-x5 Array(s)例
data test;
input sco1-sco5;
array s sco1-sco5;
do _i_ =1 to 5;
s=s*100;
end;
cards;
0.95 0.88 0.57 0.90 0.65
1 2 3 4 5
;
run;
PROC PRINT;RUN;
Do over 语句对每个数组元素自动地执行Do组中的语句,它等价于 do i=1 to k; 其中i是这个数组的下标变量,k是数组元素的个数。 Do over语句常用在对隐含下标数组元素执行Do组里的语句 Do over例
data test;
input sc01-sc05;
array s sc01-sc05;
do over s;
s=s*100;
end;
cards;
0.95 0.88 0.57 0.90 0.65
1 2 3 4 5
;
PROC PRINT;
RUN;