10-08-2017 05:28 PM
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:
infile '/home/jacobrobertwest0/hw/A1_Ans_only.txt' dlm=',';
input answer $;
When I do this, it shows up as a single variable "answer" with one observation, just the first answer on the txt file.
10-08-2017 07:11 PM
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;
10-08-2017 07:18 PM
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;
10-08-2017 07:56 PM - edited 10-08-2017 08:05 PM
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:
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 @@