读取 SAS 中分隔文件的特定列 [英] Read specific columns of a delimited file in SAS

查看:31
本文介绍了读取 SAS 中分隔文件的特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来应该很简单,但我在文档中找不到如何做到这一点.我想读一个逗号分隔的文件,但它很宽,我只想读几列.

This seems like it should be straightforward, but I can't find how to do this in the documentation. I want to read in a comma-delimited file, but it's very wide, and I just want to read a few columns.

我以为我可以这样做,但是 @ 指针似乎指向文本的列而不是由分隔符定义的列号:

I thought I could do this, but the @ pointer seems to point to columns of the text rather than the column numbers defined by the delimiter:

data tmp;
 infile 'results.csv' delimiter=',' MISSOVER DSD lrecl=32767 firstobs=2;
 @1 id
 @5 name$
run;

在此示例中,我想根据分隔符仅读取第 1 列和第 5 列中的内容,但 SAS 正在读取文本文件的位置 1 和位置 5 中的内容.所以如果输入文件的第一行是这样开头的

In this example, I want to read just what is in the 1st and 5th columns based on the delimiter, but SAS is reading what is in position 1 and position 5 of text file. So if the first line of the input file starts like this

1234567, "x", "y", "asdf", "bubba", ... 更多变量 ...

1234567, "x", "y", "asdf", "bubba", ... more variables ...

我想要 id=1234567name=bubba,但我得到的是 name=567, ".

I want id=1234567 and name=bubba, but I'm getting name=567, ".

我意识到我可以阅读每一列并删除我不想要的那些,但必须有更好的方法.

I realize that I could read in every column and drop the ones I don't want, but there must be a better way.

推荐答案

确实,@ 确实指向文本列而不是分隔列.我发现的唯一使用标准输入的方法是阅读空白,即

Indeed, @ does point to column of text not the delimited column. The only method using standard input I've ever found was to read in blank, ie

input
id
blank $
blank $
blank $ 
name $
;

然后留空.

但是,如果您不介意以不同的方式编写输入,还有更好的解决方案.

However, there is a better solution if you don't mind writing your input differently.

data tmp;
 infile datalines;
 input @;
 id = scan(_INFILE_,1,',');
 name = scan(_INFILE_,5,',');
 put _all_;
 datalines;
12345,x,y,z,Joe
12346,x,y,z,Bob
;;;;
run;

它使格式化稍微混乱,因为您需要为每个不需要的基本字符格式的变量放置或输入语句,但根据您的需要,它可能更容易.

It makes formatting slightly messier, as you need put or input statements for each variable you do not want in base character format, but it might be easier depending on your needs.

这篇关于读取 SAS 中分隔文件的特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆