引用外部原始文件

libname clinic ’D:\SAS‘;         *同样要指定libref,DATA STEP可以输出到这个libref;

filename test0  'D:\test2\test1.dat';   *给test1.dat原始数据文件一个名为test0的fileref, 为了后面infile用。filename也可以指定为一个文件夹,infile做相应的改变;

infile test0 ;    *引用test0对应的原始数据文件。当filename指定的是一个文件夹,filename test0 'D:\test2';  infile test0 (test1.dat)/ infile test0 ('test1');

infile 'D:\test2\test1.dat';   *可以直接引用原始数据文件;

input ID $ 1-4 Age 6-7 ActLevel $ 9-12 Sex $ 14;  *给原始数据加变量名,character 加$,数字不用,原始数据文件需要满足以下standard;

infile tests obs=10;   *obs=10表示只能有10个观察单位,那么方便我们验证是否正确读取了数据。如果数据正确,去掉BOS=10,再运行该行代码;

 

直接添加变量:

 

 

 前面的变量名决定了stress变量的顺序,后面的数字只代表tests中引用的位置,数字顺序可以不按从大到小排列;

 

 

 

在原有的变量上计算,赋值给变量本身:

 

DATA clinic.rep;
    set sasuser.repertory;
    TestDate='01jan2011'd;          *创建时间变量;
run;

 

data clinic.admit3;
    set sasuser.admit;
    if sex='M';                   *data的条件语句if ,proc print 条件语句where;
run;
proc print data=clinic.admit;
    where sex='M';
    var ID name sex age date;
run;

 

data clinic.data2;
input variable $1; *必提前给好变量名; datalines; *也可用cards;
... instream data ...
; *用; 结束,无run; ;
data _null_;                  *使用关键字_null_  , 可以使用data步骤,且不必创建数据集;
    set clinic.admit;
run;
*set读入stress数据集,file和put都是导出到外部数据集newdat,newdat(filename)已经指定好了某外部原始文件。
               *或者 file 'c:\clinic\patients\stress.dat';

data _null_;

set sashelp.class;
file 'E:\sas\test.txt';
put name 1-10 sex 12-13 age 15-16 height 18-19 weight 21-23;
run;

         

 

 

libname clinic 'E:\sas';
filename tests 'E:\sas\test.txt';
    data clinic.stress;
       infile tests;
       input name $ 1-10 sex $ 12-13 age 15-16 height 18-19 weight 21-23;
       BMI = height/weight**2;
       testdate='01jan2000'd;
       if sex ='';
    run;
    data clinic.peaky;
       input name $ 1-13 sex $ 15-16 age 17-19 height 21-24 weight 25-27;
       datalines;
谢尔比托马斯  男 30 172 60
谢尔比亚瑟    男 34 175 65
;

       data clinic.stress2;
           input ID 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
                 RecHR 35-37 TimeMin 39-40 TimeSec 42-43
                 Tolerance $ 45;
       datalines;
2458 Murray, W            72  185 128 12 38 D
2462 Almers, C            68  171 133 10  5 I
2501 Bonaventure, T       78  177 139 11 13 I
2523 Johnson, R           69  162 114  9 42 S
;


*Reading Data from an External File;


     libname clinic 'c:\bethesda\patients\admit'; 
     filename admit 'c:\clinic\patients\admit.dat';
     data clinic.admittan;
        infile admit obs=5;
        input ID 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
              RecHR 35-37 TimeMin 39-40 TimeSec 42-43
              Tolerance $ 45;
        if tolerance='D';
        TotalTime=(timemin*60)+timesec;
     run; 
     proc print data=clinic.admittan;
     run;

*Reading Instream Data;

     libname clinic 'c:\bethesda\patients\admit';
     data clinic.group1;
        input ID 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
              RecHR 35-37 TimeMin 39-40 TimeSec 42-43
              Tolerance $ 45;
        if tolerance='D';
        TotalTime=(timemin*60)+timesec;
     datalines;
     2458 Murray, W            72  185 128 12 38 D
     2462 Almers, C            68  171 133 10  5 I
     2501 Bonaventure, T       78  177 139 11 13 I
     2523 Johnson, R           69  162 114  9 42 S
     2539 LaMance, K           75  168 141 11 46 D
     2544 Jones, M             79  187 136 12 26 N
     2595 Warren, C            77  170 136 12 10 S
     ;
     proc print data=clinic.group1;
     run;

 

     data perm.update;
        infile invent;
        input Item $ 1-13 IDnum $ 15-19
              InStock 21-22 BackOrd 24-25;
        Total=instock+backord;
     run;

     data work.test;
        infile loan;
        input Code $ 1 Amount 3-10;
        if code='1' then type='variable';
        else if code='2' then type='fixed';
        else put 'MY NOTE: invalid value: '
             code=;
     run;

 

posted on 2019-09-30 19:23  be·freedom  阅读(504)  评论(0)    收藏  举报