BookmarkSubscribeRSS Feed
jacobrwest
Calcite | Level 5

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

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

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

 

ChrisNZ
Tourmaline | Level 20

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;

 

Patrick
Opal | Level 21

@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 @@

ChrisNZ
Tourmaline | Level 20

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 4418 views
  • 2 likes
  • 3 in conversation