读入数据时,INPUT var1 $12. 和Informat var $12. 的区别

(1)读入数据时,INPUT var1 $12. 和Informat var $12. 的区别

如读入数据集:

Albany/NY/43/-74 Albuquerque/NM/36/-106 Amarillo/TX/35/-102 Anchorage/AK/61/-150 Annapolis/MD/39/-77 Atlanta/GA/34/-84 Augusta/ME/44/-70 Austin/TX/30/-98 Baker/OR/45/-118 Baltimore/MD/39/-76 Bangor/ME/45/-69 Baton Rouge/LA/31/-91 Birmingham/AL/33/-87 Bismarck/ND/47/-101 Boise/ID/43/-116

程序1:

DATA dataset1;

       INFILE '..../../aa.txt' DLM='/'; 

      INPUT var1 $  var2 $ var3 var4;

RUN;

程序2:

DATA dataset1;

       INFILE '..../../aa.txt' DLM='/'; 

      INPUT var1 $12.  var2 $2.  var3 var4;

RUN;

程序3:

 

DATA dataset1;

       INFILE '..../../aa.txt' DLM='/';

      INFORMAT var1 $12. var2 $2.;

     FORMAT var1 $12. var2 $2.;

      INPUT var1 $  var2 $  var3 var4;

RUN;

 

执行程序1的结果:
Obs
Var1
Var2
Var3
Var4
1
Albany
NY
43
-74
2
Albuquer
NM
36
-106
3
Amarillo
TX
35
-102
4
Anchorag
AK
61
-150
5
Annapoli
MD
39
-77
6
Atlanta
GA
34
-84
7
Augusta
ME
44
-70
8
Austin
TX
30
-98
9
Baker
OR
45
-118
10
Baltimor
MD
39
-76
11
Bangor
ME
45
-69
12
Baton Ro
LA
31
-91
13
Birmingh
AL
33
-87
14
Bismarck
ND
47
-101
15
Boise
ID
43
-116
 
执行程序2的结果
 
Obs
Var1
Var2
Var3
Var4
1
Albany/NY/43
/-
74
.
2
Amarillo/TX/
35
-102
.
3
Annapolis/MD
/3
9
-77
4
Atlanta/GA/3
4/
-84
.
5
Austin/TX/30
/-
98
.
6
Baltimore/MD
/3
9
-76
7
Bangor/ME/45
/-
69
.
8
Birmingham /A
L/
33
-87
9
Bismarck/ND/
47
-101
 
 
执行程序3的结果
Obs
Var1
Var2
Var3
var4
1
Albany
NY
43
-74
2
Albuquerque
NM
36
-106
3
Amarillo
TX
35
-102
4
Anchorage
AK
61
-150
5
Annapolis
MD
39
-77
6
Atlanta
GA
34
-84
7
Augusta
ME
44
-70
8
Austin
TX
30
-98
9
Baker
OR
45
-118
10
Baltimore
MD
39
-76
11
Bangor
ME
45
-69
12
Baton Rouge
LA
31
-91
13
Birmingham
AL
33
-87
14
Bismarck
ND
47
-101
15
Boise
ID
43
-116
 
 
 分析:
程序1:未指定读入变量的长度,则SAS默认为字符型变量的长度为8,因此,后面观测读入时被自动切断,仅读入8个字符;
程序2:在INPUT语句中指定字符串的长度,则SAS在扫描源文件时,严格按照INPUT语句指定的长度进行读入;
程序3:在INFORMAT语句中指定字符串的长度,表明SAS系统给该变量分配的存储空间,读入时则按照分隔符先读入数据,然后按照定义的长度存储。

你可能感兴趣的:(SAS编程)