SAS(十一)DATA步--信息语句

SAS(十一)DATA步--信息语句

数组(array)语句

  • 当需要用同一种方法处理很多变量时,可以用数组语句定义这组变量为某个数组中的元素。
  • 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
  • 规定数组中每一维的上下界:

array x{1:5,1:3} score1-score15;

  • {*}表示SAS系统通过数组中变量的个数来确定下标。可用dim(数组名)函数计算数组元素个数。
  • _temporary_建立一个临时数组元素列表
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 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;

 

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;

 

  • 形如:ARRAY array-name <(index-variable)> <$> array-elements <(initial-values)>
  • 如果没有规定下标变量,SAS系统使用自动变量_i_作为下标变量,下标变量范围从1到这个数组元素的个数

如:array x x1-x5

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 over 语句对每个数组元素自动地执行Do组中的语句,它等价于 do i=1 to k;

其中i是这个数组的下标变量,k是数组元素的个数。

  • Do over语句常用在对隐含下标数组元素执行Do组里的语句
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;

 

Informat语句

  • Informat语句把输入格式与变量联系起来,在data步,可以用informat语句对input语句中列出的变量规定缺省时的输入格式
data infex;                                                                                                                             
     informat default=3.1 default=$char4. x1 2.2;                                                                                       
/*数值的输入格式忽略w.d格式中w的值*/                                                                                                    
     input x1-x5 name $;                                                                                                                
         file print;                                                                                                                    
         put x1-x5 name;                                                                                                                
         cards;                                                                                                                         
1111  22   33   4444  100 Johnny                                                                                                        
;                                                                                                                                       
run;

 

Format语句 

  • data步把变量同输出格式联系起来
  • 可以规定缺省选项,如:
format default=8.2;

 

data;                                                                                                                                   
informat a dollar9.;/*必须指定,否则不能识别数据*/                                                                                      
format a comma9.;                                                                                                                       
input a @@;                                                                                                                             
cards;                                                                                                                                  
$1900000                                                                                                                                
;                                                                                                                                       
run;                                                                                                                                    
proc print;                                                                                                                             
run;

 

data ff;                                                                                                                                
     format header $7. name $8.;                                                                                                        
         input header= name=;                                                                                                           
         cards;                                                                                                                         
header=zxs zjy   name=zhou aaa                                                                                                          
header=123 name=aaa                                                                                                                     
;                                                                                                                                       
run;                                                                                                                                    
proc print;                                                                                                                             
run;

 

Drop和keep语句

  • Drop语句:删掉变量语句

drop variable-list

  • Keep语句:保留变量语句

keep variable-list

  • 虽然出现在dropkeep语句中的变量不包含在正被创建的SAS数据集中,但这些变量仍可以用在程序语句中
  • Dropkeep不能同时使用

 

Retain语句 

  • 使用retain给变量赋予初值,并保留到读下一个观测
retain month1-month5 1
retain var1-var4 (1,2,3,4)

retain 后无变量时,用 input 或赋值语句创建的所有变量的值 , 保留上次 data 步执行的值
data retire;                                                                                                                            
  input amount @@;                                                                                                                      
  retain year 1994 total 0;                                                                                                             
  year=year+1;                                                                                                                          
  total=total+amount;/*total=sum(total,amount)*/                                                                                        
  /*可用下列语句代替                                                                                                                    
  retain year 1994;                                                                                                                     
  year+1;                                                                                                                               
  total+amount;*/                                                                                                                       
  cards;                                                                                                                                
  500 1000 1500 2200 2700                                                                                                               
  ;                                                                                                                                     
  run;                                                                                                                                  
  proc print;                                                                                                                           
  run;

 

data group;                                                                                                                             
  input x;                                                                                                                              
  retain;                                                                                                                               
  if x=1 then sex='M';                                                                                                                  
  if x=2 then sex='F';                                                                                                                  
cards;                                                                                                                                  
1                                                                                                                                       
2                                                                                                                                       
3                                                                                                                                       
2                                                                                                                                       
;                                                                                                                                       
run;                                                                                                                                    
proc print;                                                                                                                             
run;

 

Attrib语句

  • data步内允许用一个attrib语句来规定一个或几个变量的输出、输入格式,标签和长度,即规定变量属性
  • Attrib x  label=中国载人飞船
              length=4
              informat=8.5
              format=8.2;
 

 

data a;                                                                                                                                 
  input x @@;                                                                                                                           
  attrib x label='中国载人飞船'                                                                                                         
           length=8                                                                                                                     
           informat=comma8.                                                                                                             
           format=dollar12.;                                                                                                            
cards;                                                                                                                                  
12,345,678                                                                                                                              
;                                                                                                                                       
run;                                                                                                                                    
proc print;                                                                                                                             
run;

 

Window语句

  • 创建用户专用的窗口,可用来显示文字说明或接受输入的数据
data _null_;                                                                                                                            
   window start                                                                                                                         
     #5 @26 'welcome to the sas system' color=yellow                                                                                    
         #7 @19 'this program creates two sas data sets'                                                                                
         #8 @26 'and uses three procedures'                                                                                             
         #12 @27 'press enter to continue';                                                                                             
   display start;                                                                                                                       
   stop;                                                                                                                                
run;

 

 

猜你喜欢

转载自blog.csdn.net/LuYi_WeiLin/article/details/103256794
SAS