使用数据步骤

时间:2016-06-27 15:04:55

标签: sas

我有以下数据:

Bellatorum School: CSULA Time: 1:40.5
The Kraken School: ASU Time: 1:45.35
Black Widow School: UoA Time: 1:33.7
Koicrete School: CSUF Time: 1:40.25
Khaos School: UNLV Time: 2:03.45
Max School: UCSD Time: 1:26.47

我想使用SAS数据步骤导入上述数据。例如,这就是我想要sas数据集的方式:

School Name        University Time
Bellatorum School  CSULA      01:40.5

我该怎么做

3 个答案:

答案 0 :(得分:3)

你可以摆弄分隔符。我想我的时间格式正确。

data school;
   length dlm $1;
   infile cards dlm=dlm;
   dlm=':';
   input school:$64. @;
   dlm=' ';
   input univ:$16. @;
   input @'Time:' time :hhmmss.;
   format time time12.2;
   cards;
Bellatorum School: CSULA Time: 1:40.5
The Kraken School: ASU Time: 1:45.35
Black Widow School: UoA Time: 1:33.7
Koicrete School: CSUF Time: 1:40.25
Khaos School: UNLV Time: 2:03.45
Max School: UCSD Time: 1:26.47
;;;;
   run;
proc print;
   run;

enter image description here

答案 1 :(得分:0)

一次尝试是读取整行然后修复datastep中的列:

data mytable (drop=mydata);
infile "D:input.txt" dlm='' truncover ;
input mydata $80.  ;
schoolname=scan(mydata,1,":");/*get the part before first :*/
University=tranwrd(scan(mydata,2,":"),"Time","");/*get part between first and second :, remove text time*/
time=catx(":",scan(mydata,3,":"),scan(mydata,4,":"));/*get part 3(hours) and 4(min+sec) and concat them*/
run;

这导致:

enter image description here

但现在时间是一个字符变量,如果你想把它作为日期时间,你需要用输入格式化它,如下所示:

data mytable (drop=mydata);
Format time time10.;
...
...
time=input(catx(":",scan(mydata,3,":"),scan(mydata,4,":")),hhmmss.);
run;

答案 2 :(得分:0)

您应该修改分隔符以及TIME格式。但您可以使用:作为分隔符来阅读它。您只需要从University字段的末尾删除虚假的Time字符串。您还可能希望从这两个部分创建实时时间值。

data want ;
  length School University $40 Minutes Seconds Time 8 ;
  infile cards dsd dlm=':' truncover ;
  input school university Minutes Seconds ;
  if scan(university,-1,' ')='Time' then
    university = substrn(university,1,length(university)-4)
  ;
  time = minutes*60 + seconds ;
  format time time11.2 ;
cards;
Bellatorum School: CSULA Time: 1:40.5
The Kraken School: ASU Time: 1:45.35
Black Widow School: UoA Time: 1:33.7
Koicrete School: CSUF Time: 1:40.25
Khaos School: UNLV Time: 2:03.45
Max School: UCSD Time: 1:26.47
;;;;