yihao ren
yihao ren

Reputation: 379

how to read date and time from txt file using sas

I'm very new to SAS and I'm trying to read a txt file that contains date and time. The file is shown in the following figureenter image description here

I believe I have tried all the possible options that I can think of to read the file but the output is still in numeric form. The following is the code I'm using

data wb_bg_1619;
     infile "C:\Users\daizh\Desktop\Ren\SAS\wb_bg_0215.txt" firstobs=3 missover;

     informat DATE DATE7. TIME TIME5. ;
     input DATE TIME BG;
     run;
proc print data=wb_bg_1619;
run;

The output looks like this

enter image description here

Upvotes: 1

Views: 829

Answers (1)

Stu Sztukowski
Stu Sztukowski

Reputation: 12909

You've used an informat to automatically convert a date stored as text into a numeric SAS date format, which is the number of days since Jan 1 1960. To display that in a human readable format, you need to use a regular format. Add the following to the top or bottom of your code:

format date date9.
       time time.
;

This changes how the data is displayed to you, but does not change how SAS works with it. As far as SAS is concerned, a date is only a number. You could run the rest of your program without ever using a format and get the right numbers and calculations with it if you wanted to, but that sure makes troubleshooting hard.

To remember the difference between a format and informat:

  • informats are for inputs
  • formats are for you

Upvotes: 1

Related Questions