DATA Step, Macro, Functions and more

Reading a TXT File with only one line of data

Reply
New Contributor
Posts: 2

Reading a TXT File with only one line of data

Hello there,

I'm quite new to SAS, I'm in a class at university and I have scoured the internet for an answer to this question and have come up with nothing. 

 

In SAS Studio, I'm attempting to read a txt file of an answer key. The answers are all on one line, delimited by a comma. However, I'd like each answer to fill an observation, not a different variable, so I can compare the answer key to another data file where the answers are observations, and my assignment says we cannot change the .txt file (I would personally just like to go in and put each answer on a separate line.

 

My question is, how can I read this one line of data as one variable "Answer" with each answer as an observation, not a variable?

 

Here is my code so far:

 

data anskey;
infile '/home/jacobrobertwest0/hw/A1_Ans_only.txt' dlm=',';
input answer $;
run;

 

When I do this, it shows up as a single variable "answer" with one observation, just the first answer on the txt file.

 

Thank you

PROC Star
Posts: 1,760

Re: Reading a TXT File with only one line of data

Posted in reply to jacobrwest

Like this?

data _null_;
  file "%sysfunc(pathname(work))\t.txt";
  put 'a,vbb,sdfs,erw,3';
run;

data WANT;
  infile "%sysfunc(pathname(work))\t.txt" dlm=',' pad;
  length VAR $8;
  do until(VAR=' ');
    input VAR : $8. @;
    output;
  end;
run;

 

VAR
a
vbb
sdfs
erw
3

 

PROC Star
Posts: 1,760

Re: Reading a TXT File with only one line of data

If you want to avoid the LOST CARD  message in the log:

data WANT;
  infile "%sysfunc(pathname(work))\t.txt" dlm=',' pad end=EOF;
  length VAR $8;
  input @;
  NBVARS=countw(_infile_,',');
  do I=1 TO NBVARS;
    input VAR : $8. @;
    output;
  end;
run;

 

Respected Advisor
Posts: 4,173

Re: Reading a TXT File with only one line of data

[ Edited ]
Posted in reply to jacobrwest

@jacobrwest

Or as a variation to @ChrisNZ:

filename myData temp;
data _null_;
  file myData;
  put 'a,vbb,sdfs,erw,3,,99';
run;

data WANT;
  infile myData dlm=',' dsd;
  input var :$8. @@;
run;

 

The @@ will hold the input buffer on the same line during iteration of the data step.

In each iteration a position of your source data will be read, then the data step will loop again (input cursor still on the same line but now starting at the position where it was at the end of the previous iteration). So you read once "cell" of data per iteration of the data step.

If there is no explicit output in your data step then an implicit output will get executed at the end of each iteration.

The data step will stop execution when the input pointer reaches the end of your source data (=last value read).

 

And here the docu link you couldn't find:

http://documentation.sas.com/?docsetId=lestmtsref&docsetTarget=n0oaql83drile0n141pdacojq97s.htm&docs...

 

I normally restrict my searches to the SAS site. I.e. to find above link my search term has been:

site:support.sas.com 9.4 input statement @@

PROC Star
Posts: 1,760

Re: Reading a TXT File with only one line of data

@Patrick Yes that's a better solution. Slow brain this Monday morning. Smiley Happy

Ask a Question
Discussion stats
  • 4 replies
  • 161 views
  • 1 like
  • 3 in conversation