楼主: skidrowxu
4073 10

[问答] 求助:关于 SAS input statement的问题 [推广有奖]

  • 0关注
  • 0粉丝

高中生

55%

还不是VIP/贵宾

-

威望
0
论坛币
-1 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
88 点
帖子
10
精华
0
在线时间
45 小时
注册时间
2016-7-10
最后登录
2017-4-28

楼主
skidrowxu 发表于 2016-10-30 11:50:04 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
大家好,我是一名菜鸟,在咱们论坛上学到了很多东西。今天碰见一个问题,怎么也想不明白,也找不到相关资料,所以在这请高手指教。

现在有一个文本文档叫testdata1.txt, 里面有以下数据:

1

22

333

4444

55555


如果用以下的两段code去分别生成名为的test1和test2的dataset.

datatest1;

infile'C:\SAS Training\testdata1.txt';

input x;


run;



datatest2;

infile'C:\SAS Training\testdata1.txt';

input x 5.;

run;


以下两个dataset的运行结果:


test1.png
test2.png


请问为什么得到的结果不一样,为什么test2这个dataset少了1和333这两歌观测,而保留了剩余的三个。


我挺着急的,希望高手们帮帮小弟的忙,谢谢




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:statement Statemen Statem State Input Tencent Users file 资料

沙发
skidrowxu 发表于 2016-10-30 12:03:15
我想是不是因为在文本文档里,所以的data lines 的长度是不同的,所以在建test 2的时候需要在INFILE statement 里面加上 PAD option?

藤椅
cmuz4882309 发表于 2016-10-30 12:12:13
学习了,谢谢楼主。

板凳
cnae0771248 发表于 2016-10-30 12:18:02
是不是回帖超过20个字才给1个土木币呀,因为我之前回了几个字没有币,回的超过20个字就有一个币。你也可以试试。

报纸
penaironguc76 发表于 2016-10-30 12:28:17
喜欢 ,有没有此类的施工图?

地板
cmvqc253605 发表于 2016-10-30 12:28:25
谢谢楼主分享,又收集了其中一个分部的技术交底了,谢谢

7
cmv67745873 发表于 2016-10-30 12:28:25
好资料新资料必须顶起来 !

8
franky_sas 发表于 2016-10-30 13:59:24
input整数的时候,最好不加informat,你可以看看《小白学SAS》,里面讲的很详细。

9
DriftKingHan 发表于 2016-10-30 16:35:42
可以在informat 5. 前加个冒号,这样就解决了当然还可以用truncover
  1. filename myData 'F:\RawData.txt';
  2. data test;
  3.         infile mydata;
  4.         input x : 5.;
  5. run;
  6. proc print data = test;
  7. run;
复制代码


至于为什么会出现上面的情况,因为使用了informat 5.
所以这里的读取方式是formatted input,是从源数据取5列作为变量值
但是在取不够的情况下,就直接取下一个record的值了
在SAS文档里正好有你这个例子,也有解释
Reading Past the End of a Line
By default, if the INPUT statement tries to read past the end of the current input data record, then it moves the input pointer to column 1 of the next record to read the remaining values. This default behavior is specified by the FLOWOVER option. A message is written to the SAS log:
NOTE: SAS went to a new line when INPUT
statement reached past the end of a line.
Several options are available to change the INPUT statement behavior when an end of line is reached. The STOPOVER option treats this condition as an error and stops building the data set. The MISSOVERand TRUNCOVER options do not allow the input pointer to go to the next record when the current INPUT statement is not satisfied. The SCANOVER option, used with @'character-string' scans the input record until it finds the specified character-string. The FLOWOVER option restores the default behavior.
The TRUNCOVER and MISSOVER options are similar. The MISSOVER option causes the INPUT statement to set a value to missing if the statement is unable to read an entire field because the value is shorter than the field length that is specified in the INPUT statement. The TRUNCOVER option writes whatever characters are read to the appropriate variable.
For example, an external file with variable-length records contains these records:
----+----1----+----2
1
22
333
4444
55555
The following DATA step reads this data to create a SAS data set. Only one of the input records is as long as the informatted length of the variable TESTNUM.
data numbers;
   infile 'external-file';
   input testnum 5.;
run;
This DATA step creates the three observations from the five input records because by default the FLOWOVER option is used to read the input records.
If you use the MISSOVER option in the INFILE statement, then the DATA step creates five observations. All the values that were read from records that were too short are set to missing. Use the TRUNCOVER option in the INFILE statement if you prefer to see what values were present in records that were too short to satisfy the current INPUT statement.
infile 'external-file' truncover;
The DATA step now reads the same input records and creates five observations.
已有 1 人评分学术水平 热心指数 收起 理由
hyquid + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1   查看全部评分

10
skidrowxu 发表于 2016-11-3 15:30:26
DriftKingHan 发表于 2016-10-30 16:35
可以在informat 5. 前加个冒号,这样就解决了当然还可以用truncover
谢谢了!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-19 18:19