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
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 |
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;
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 @@
@Patrick Yes that's a better solution. Slow brain this Monday morning. 🙂
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.