sas数据导入终极汇总-之中的一个

将数据文件读入SAS ——DATA Step / PROC IMPORT
   1.将SAS文件读入SAS——
   data sasuser.saslin;
     set "F:sas1.sas7bdat";
   run;
   proc contents data=sasuser.saslin;
   run;


   2.将其它形式文件导入成SAS ——PROC IMPORT / 直接读入其它形式文件
   proc import datafile = "c:datahsb2.sav" out= work.hsb2;
   run;
   proc contents data=hsb2;
   run;
   SAS导入数据:SAS recognizes the file type to be imported by file extension.
对数据长度的限制
    在一些操作环境,SAS假定外部文件的纪录对最长为256(一行数据包含空格等全部字符在内的长度),假设估计读入的纪录长度超过256,可在Infile语句中使用LRECL=n 这个命令。
读入以空格作为分隔符的原始数据
    假设原始数据的不同变量之间是以至少一个空格作为分隔符的。那能够直接採用List方法将这些数据读入SAS。
List Input读数据非常方便,但也有非常多局限性:
(1)       不能跳过数据;
(2)       全部的缺失值必须以点取代
(3)       字符型数据必须是不包括空格的,且长度不能超过8;
(4)       不能直接读入日期型等特殊类型的数据。

程序举例:
INPUT Name $ Age Height;

                      读入按列组织的数据
有些原始数据的变量之间没有空格或其它分隔符,因此这种文件不能以List形式对入SAS。但若不同变量值的都在每条记录的固定位置处,则能够依照Column 形式读入数据。Colunm读数据方法要求全部的数据均为字符型或者标准的数值型(数值中仅包含数字,小数点,正负号,或者是E,不包含逗号或日期型数据)。
相对于List方法。Column读数据方法有例如以下长处:
(1)       变量值之间无需用空格分开;
(2)       能够空格表示缺失值;
(3)       字符型数据中可包含空格。
(4)       可跳过数据。
程序举例:
INPUT Name $ 1-10 Age 11-13 Height 14-18;
使用格式命令读入非标准格式的数据
字符型数据: $informat w.
数值型数据:   informat w.d
日期型数据:   Datew.
(1)字符型:
$CHARw. :不删除前后空格,读入字符数据;
$HEXw. :将16进制的数据转化成字符数据;
$w.      :删除前面空格,读入字符数据;
(2)日期,时间或日期时间型数据
DATEw.           :以ddmmmyy或ddmmmyyyy形式读入日期。
DATETIMEw. :以ddmmmyy hh:mm:ss.ss 形式读入日期时间;
DDMMYYw.     :以ddmmyy或ddmmyyyy读入日期;
JULIANw.        :以yyddd或yyyyddd读入Julia日期;
MMDDYYw.     :以mmddyy或mmddyyyy形式读入日期;
TIMEw.             :以hh:mm:ss.ss形式读入时间;
(3)数值型数据
COMMAw.d       :读入数值型数据,将当中的逗号,$ 删除,并将括号转化为负号
HEXw.                :将16进制数据转化成浮点型数据
IBw.d                  :读入整数二进制数据;
PERCENTw.     :将百分数转化为普通数据。
w.d                      :读入标准的数值型数据。

INPUT Name $16. Age 3. +1 Type $1. +1 Date MMDDYY10. 
         (Score1 Score2 Score3 Score4 Score5) (4.1);
                         多种输入格式综合
读入位置控制——列指针
+n –n :控制列指针从当前位置向前或向后移动n个字符;
@n   :控制列指针指向
举例:
INPUT ParkName $ 1-22 State $ Year @40 Acreage COMMA9.;

                         读入杂乱数据
在不确定从哪一列開始读入数据,但知道读入的数据均位于某一特定字符或字符串之后时。可採用@’character’列指针。

如:有字符串例如以下。需读入Breed:后面的字符串
My dog Sam Breed: Rottweiler Vet Bills: $478

(1)SAS 语句:Input @’Breed: ’ DogBreed $;     读入内容: Rottweil
读入Breed:后面的字符串,赋给DogBreed。读入时到空格时。自己主动结束。
(2)SAS 语句:Input @’Breed:’ DogBreed $20.;   读入内容:Rottweiler Vet Bill
读入Breed: 后面的字符串,赋给DogBreed,读入字符串的长度为20。

(3)SAS语句:Input @’Breed:’ DogBreed :$20.;   读入内容:Rottweiler
      读入Breed: 后面的字符串,赋给DogBreed,读入字符串的长度为20,但遇到空格时不再继续读数据。
从原始数据中读入多行数据作为SAS的一条观測
使用行指针:
‘ / ’—— 到下一行读数据
‘#n ’——到第n 行读数据
INPUT City $ State $ / NormalHigh NormalLow #3 RecordHigh RecordLow;
从一行原始数据中读入多个观測
在Input语句末尾使用@@标示,告诉SAS继续读入本行后面的数据。

INPUT City $ State $ NormalRain MeanDaysRain @@;
有选择的读入原始数据
SAS让用户无需读入全部的原始数据。然后再推断是否是用户所须要的数据。

用户仅需先读入足够的变量,以推断该条观測是否为自己所需,然后在INPUT语句后以@结尾。以使SAS读数据的指针停在此处。保留此行数据。然后用户使用IF语句推断读入的观測是否为所需数据,若是。则使用第二个INPUT语句继续读入其余数据。

INPUT Type $ @; 
   IF Type = ’surface’ THEN DELETE; 
   INPUT Name $ 9-38 AMTraffic PMTraffic;
@ & @@
(1)    均为锁定数据行的标示。
(2)    @标示在SAS进入下个循环之前就释放锁定的数据行,而@@标示在继续锁定数据行
在INFILE语句中控制输入的选项
(1)FIRSTOBS=n : 从n条观測開始读入数据
(2)OBS=n 读入n条观測
(3)当读进内存的观測长度小于INPUT语句设定的长度时
当SAS指针到达一条记录的末尾,而在INPUT语句中尚有未读入的变量时,SAS默认继续读入下一行数据。
MISSOVER:不读入下一行数据,而将未赋值的变量以缺失值填充。 
TRUNCOVER:当使用column或格式化读入方式时,某些数据行长度小于其它数据行长度时,使用TRUNCOVER选项,可防止SAS读入下一行数据。
使用DATA步读入分隔符文件
在INFILE语句中使用DLM= 选项或者DSD选项能够读入以特定符号作为分隔符的原始文件。

(1)The DLM= option (i.e. DLM=’&’)
假设是以Tab作为分隔符。则使用DLM=’09’X命令
(2)The DSD option:主要有三个功能
忽略单引號内的分隔符。
不将引號作为数据读入SAS;
将一行内连续两个单引號作为一个缺失值处理。

使用IMPORT程序步读入分隔符文件
IMPORT 程序的功能
(1)       自己主动扫描数据文件,并确定变量的类型(数值型或字符型)。
(2)       为字符型变量,自己主动设定变量的长度;
(3)       识别一些日期型数据。
(4)       将两个连续的分隔符作为一个缺失值读入SAS
(5)       读入引號内数据
(6)       自己主动将原始数据中不存在的变量赋缺失值。
PROC IMPORT DATAFILE=’filename’ OUT=data-set;
SAS依据读入文件的扩展名确定文件的类型。若读入文件没有正确的扩展名。或者是DLM文件。用户必须在IMPORT程序步中使用DBMS=option 选项。当读入数据集的名称已经存在于SAS库中,可用REPLACE选项将原数据覆盖。

PROC IMPORT DATAFILE=’filename’ OUT=data-set DBMS=identifier REPLACE;
在默认情况下,IMPORT程序步将第一行数据作为变量的名称。若第一行数据并不是变量名,可在IMPORT语句后使用GETNAMES=NO语句。

若IMPORT程序读入的是分隔符文件。默认分隔符为空格。若不是,则需使用DILIMITER=statement语句指定分隔符。
PROC IMPORT DATAFILE=’filename’ OUT=data-set 
                            DBMS=DLM REPLACE;
       GETNAMES=NO;
       DELIMITER=’delimiter-character’;
RUN;
使用IMPORT程序步读入PC文件
PROC IMPORT DATAFILE=’filename’ OUT=data-set
       DBMS=identifier REPLACE;
列示SAS数据集的内容
PROC CONTENTS DATA=data-set;
CONTENTS程序步的功能是显示SAS对数据集的详细描写叙述,主要内容有:
(1)       数据集描写叙述
               数据集的名称。
               观測的数量。
               变量的数量;
               创建日期
(2)       变量描写叙述
               变量类型;
               变量长度;
               变量的输出格式;
               变更的输入格式;
              变量标识。

实例:
1.读入逗号分隔数据:cars_novname.csv

Acura,MDX,SUV,Asia,All,"$36,945 ","$33,337 ",3.5,6,265,17,23,4451,106,189
Acura,RSX Type S 2dr,Sedan,Asia,Front,"$23,820 ","$21,761 ",2,4,200,24,31,2778,101,172
Acura,TSX 4dr,Sedan,Asia,Front,"$26,990 ","$24,647 ",2.4,4,200,22,29,3230,105,183
Acura,TL 4dr,Sedan,Asia,Front,"$33,195 ","$30,299 ",3.2,6,270,20,28,3575,108,186
Acura,3.5 RL 4dr,Sedan,Asia,Front,"$43,755 ","$39,014 ",3.5,6,225,18,24,3880,115,197

proc import datafile="cars_novname.csv" out=mydata dbms=csv replace;
    getnames=no;
run;

proc contents data=mydata;
run;

SAS creates default variable names as VAR1-VARn when variables names are not present in the raw data file.

2.读入制表键分隔的数据:
proc import datafile="cars.txt" out=mydata dbms=tab replace;
   getnames=no;
run;
3.依据不同任务将不同的数据集永久保存到相应任务的目录下:
libname dis "c:dissertation";
proc import datafile="cars.txt" out=dis.mydata dbms=dlm replace;
   delimiter='09'x;
   getnames=yes;
run;
3.读入空格键分隔的数据:
proc import datafile="cars_sp.txt" out=mydata dbms=dlm replace;
getnames=no;
run;
4.分隔符的终极样例:
Other kinds of delimiters
You can use delimiter= on the infile statement to tell SAS what delimiter you are using to separate variables in your raw data file. For example, below we have a raw data file that uses exclamation points ! to separate the variables in the file.

22!2930!4099
17!3350!4749
22!2640!3799
20!3250!4816
15!4080!7827
The example below shows how to read this file by using delimiter='!' on the infile statement.

DATA cars;
INFILE 'readdel1.txt' DELIMITER='!' ;
INPUT mpg weight price;
RUN;

PROC PRINT DATA=cars;
RUN;
As you can see in the output below, the data was read properly.

OBS    MPG    WEIGHT    PRICE

1      22     2930      4099
2      17     3350      4749
3      22     2640      3799
4      20     3250      4816
5      15     4080      7827
It is possible to use multiple delimiters. The example file below uses either exclamation points or plus signs as delimiters.

22!2930!4099
17+3350+4749
22!2640!3799
20+3250+4816
15+4080!7827
By using delimiter='!+' on the infile statement, SAS will recognize both of these as valid delimiters.

DATA cars;
INFILE 'readdel2.txt' DELIMITER='!+' ;
INPUT mpg weight price;
RUN;

PROC PRINT DATA=cars;
RUN;
As you can see in the output below, the data was read properly.

OBS    MPG    WEIGHT    PRICE

1      22     2930      4099
2      17     3350      4749
3      22     2640      3799
4      20     3250      4816
5      15     4080      7827


import缺陷及注意事项:
Proc import does not know the formats for your variables, but it is able to guess the format based on what the beginning of your dataset looks like. Most of the time, this guess is fine. But if the length of a variable differs from beginning to end of your file, you might end up with some truncated values.


重点语法-Infile options
For more complicated file layouts, refer to the infile options described below.

DLM=
The dlm= option can be used to specify the delimiter that separates the variables in your raw data file. For example, dlm=','indicates a comma is the delimiter (e.g., a comma separated file, .csv file). Or, dlm='09'x indicates that tabs are used to separate your variables (e.g., a tab separated file).

DSD 
The dsd option has 2 functions. First, it recognizes two consecutive delimiters as a missing value. For example, if your file contained the line 20,30,,50 SAS will treat this as 20 30 50 but with the the dsd option SAS will treat it as 20 30 . 50 , which is probably what you intended. Second, it allows you to include the delimiter within quoted strings. For example, you would want to use the dsd option if you had a comma separated file and your data included values like "George Bush, Jr.". With the dsd option, SAS will recognize that the comma in "George Bush, Jr." is part of the name, and not a separator indicating a new variable.


FIRSTOBS=
This option tells SAS what on what line you want it to start reading your raw data file. If the first record(s) contains header information such as variable names, then set firstobs=n where n is the record number where the data actually begin. For example, if you are reading a comma separated file or a tab separated file that has the variable names on the first line, then use firstobs=2 to tell SAS to begin reading at the second line (so it will ignore the first line with the names of the variables).


MISSOVER 
This option prevents SAS from going to a new input line if it does not find values for all of the variables in the current line of data. For example, you may be reading a space delimited file and that is supposed to have 10 values per line, but one of the line had only 9 values. Without the missover option, SAS will look for the 10th value on the next line of data. If your data is supposed to only have one observation for each line of raw data, then this could cause errors throughout the rest of your data file. If you have a raw data file that has one record per line, this option is a prudent method of trying to keep such errors from cascading through the rest of your data file.

OBS= 
Indicates which line in your raw data file should be treated as the last record to be read by SAS. This is a good option to use for testing your program. For example, you might use obs=100 to just read in the first 100 lines of data while you are testing your program. When you want to read the entire file, you can remove the obs= option entirely.

A typical infile statement for reading a comma delimited file that contains the variable names in the first line of data would be:

INFILE "test.txt" DLM=',' DSD MISSOVER FIRSTOBS=2 ;


读入有缺失值的数据或者读入数值中含有分隔符的数据
DATA cars2;
length make $ 20 ;
INFILE 'readdsd.txt' DELIMITER=',' DSD ;
INPUT make mpg weight price;
RUN;

PROC PRINT DATA=cars2;
RUN;


48,'Bill Clinton',210
50,'George Bush, Jr.',180
DATA guys2;
length name $ 20 ;
INFILE 'readdsd2.txt' DELIMITER=',' DSD ;
INPUT age name weight ;
RUN;


PROC PRINT DATA=guys2;
RUN;

最经典样例:从某行開始读入数据
DATA cars2;
length nf 8;
INFILE 'F:cars1.csv' DELIMITER=',' dsd MISSOVER firstobs=2 ;
INPUT nf zh hh xb cs IHA fj;
RUN;

PROC PRINT DATA=cars2;
RUN;
原文地址:https://www.cnblogs.com/yangykaifa/p/7009834.html