user10833069
user10833069

Reputation:

SAS How convert string to time, when load data from csv?

Im trying load data from csv. I have a few formats: date, time, numeric, string. I dont have problem to convert data to this format except time format. Data looks:

Date,Time,Transaction,Item
2016-10-30,9:58:12,1,Bread
2016-10-30,10:05:36,2,Scandinavian
2016-10-30,10:08:00,3,Hot chocolate

My code:

data lab0.piekarnia;
INFILE 'path_to_csv' delimiter=',' firstobs=2;
format Date yymmdd10.;
format Time time8.;
INPUT 
    Date yymmdd10.
    Time time8.
    Transaction 
    Item $;
run;

Result

What I try? I try to manually convert string '12:22:22', This method give good results, but I dont know how can I implement it when load csv.

data ds1;
j = input('12:22:22',HHMMSS8.);
format j time8.;
run;

Upvotes: 1

Views: 442

Answers (2)

Tom
Tom

Reputation: 51581

The first line has only 7 characters for the time value, but you told SAS to read exactly 8 characters. So it included the comma. When reading delimited data, like a CSV file, you need to use list mode input and not formatted mode. You do this by either eliminating the informat specification from the INPUT statement (and instead attach an informat to the variable with an INFORMAT statement) or by prefixing informat specification with the : (colon) modifier. Also if you don't define the length for ITEM (or give SAS something else, like an informat, that it can use to guess a length) it will be created as length $8.

input date :yymmdd10. time :time8. transaction item :$40.;

Upvotes: 0

Llex
Llex

Reputation: 1770

data have;
INFILE "path_to_csv" truncover delimiter=',' firstobs=2 ;
format Date yymmdd10.;
format Time time8.;
INPUT date time transaction item $32.;
informat
    Date yymmdd10.
    Time time.;
/*Instead input and informat statements you can use:
INPUT date:yymmdd10. time:time. transaction item $32.;
*/
run;

Upvotes: 0

Related Questions