将 CSV 中的所有列作为字符导入? [英] Import all columns from CSV as character?

查看:33
本文介绍了将 CSV 中的所有列作为字符导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题.

PROC IMPORT OUT= braw.address
DATAFILE= "&path.\address_data.csv"
DBMS=csv REPLACE;
GETNAMES=YES;

RUN;

此语句将根据值将数据集列创建为字符 数字,这很聪明,但不是我想要的.

This statement will create the dataset columns as character or numeric depending on the values, which is smart, but not what I want.

我想将它们全部导入为字符,以便更轻松地进行正则表达式评估.

I want to import them all as character, to make for easier regex evaluation.

有没有简单的方法可以做到这一点?

Is there a simple way to do this?

推荐答案

如果您不想编写 SAS 宏来将所有列作为字符读取,您可以尝试作弊".手动编辑文件并复制第一行(包含列标题的行.由于这些很可能都是字符串,SAS 应将所有列作为字符导入.

If you do not want to write a SAS macro to read all the columns as character, you could try a "cheat". Manually edit the file and duplicate the first row (the one containing column headers. Since those will most likely all be character strings, SAS should import all the columns as character.

当然,一个宏来做到这一点不会那么困难.你可以试试这样的:

Of course, a macro to do this would not be that difficult. You can try something like this:

%macro readme(dsn,fn);
/* Macro to read all columns of a CSV as character */
/* Parameters:                                     */
/*   DSN - The name of the SAS data set to create  */
/*   FN  - The external file to read (quoted)      */
/* Example:                                        */
/*    %readme(want, 'c:\temp\tempfile.csv');       */
data _null_;
  infile &fn;
  input;
  i = 1;
  length headers inputstr $200;
  headers = compress(_infile_,"'");
  newvar = scan(headers,1,',');
  do until (newvar = ' ');
     inputstr = trim(inputstr) || ' ' || trim(newvar) || ' $';
     i + 1;
     newvar = scan(headers,i,',');
     end;
  call symput('inputstr',inputstr);
  stop;
run;

data &dsn;
  infile &fn firstobs=2 dsd dlm=',' truncover;
  input &inputstr.;
run;
%mend;
%readme(want, 'c:\temp\tempfile.csv');

这篇关于将 CSV 中的所有列作为字符导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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