0
votes

I'm new to SAS. I'm trying to read a txt file where the same variables are listed in multiple columns. enter image description here

The first variable is the date. The second one is time, and the last one is Blood Glucose. Thanks a lot for your kindness and help.

Sincerely

Wilson

1
Does you data file really use only 7 characters for the date?Tom

1 Answers

1
votes

The data can be read using a list input statement with the : (format modifier) and @@ (line hold) features specified.

glucose-readings.txt (data file)

01jan16 14:46  89   03jan16 11:27 103   04jan16 09:40  99
05jan16 09:46 105   11jan16 10:58 108   13jan16 10:32 109
14jan16 10:49  90   18jan16 09:32 110   25jan16 10:37 100

Sample program

data want;
  infile "c:\temp\glucose-readings.txt";

  input 
    datepart :date9.
    timepart :time5.
    glucose
  @@;

  datetime = dhms(datepart,0,0,timepart);

  format 
    datepart date9.
    timepart time5.
    datetime datetime19.
    glucose 3.
  ;
;

proc print; run;

enter image description here

From the documentation INPUT Statement: List

:
... For a numeric variable, this format modifier reads the value from the next non-blank column until the pointer reaches the next blank column or the end of the data line, whichever comes first.
...
@@
holds an input record for the execution of the next INPUT statement across iterations of the DATA step. This line-hold specifier is called double trailing @.
...
Tip The double trailing @ is useful when each input line contains values for several observations.

Be sure to read the documentation, that is were you will find detailed explanations and useful examples.