4
votes

I have only 1 line without line feed (CRLF CRLF), the linefeed is a string of 4 characters, in this example is "@A$3" I don't need dlm for now, and I need to import it from a external file (/files/Example.txt)

JOSH 30JUL1984 1011 SPANISH@A$3RACHEL 29OCT1986 1013 MATH@A$3JOHNATHAN 05JAN1985 1015 chemistry

I need this line into 3 lines:

JOSH 30JUL1984 1011 SPANISH
RACHEL 29OCT1986 1013 MATH
JOHNATHAN 05JAN1985 1015 chemistry

How I can do that in SAS?

*Added: Your solutions are working with this example, but i have a issue, a line that contains more than the maximum length allowed for the line(32,767 bytes),

For example this line in the above exercise contains 5,000 records.

Is it possible?

3
If you're simply trying to read multiple records from one line of text, append "@@" to the end of the 'input' line, such as: input column1 255. @@;Patrick

3 Answers

2
votes

Use the DLMSTR= option on the infile statement -- this will specify "@A$3" as the delimiter. Then use @@ on the input statement to tell SAS to look for more records on the same line.

data test;
infile "/files/Example.txt" dsd dlmstr='@A$3';
informat var $255.;
input var $ @@;
run;

With your example, you will get a data set with 3 records with 1 variable containing the strings you are looking for.

Adjust the length of var as needed.

1
votes

You could do something like this:

First import the file as a single row (be sure to adjust the length):

DATA WORK.IMPORTED_DATA;
INFILE "/files/Example.txt" TRUNCOVER;
LENGTH Column1 $ 255;
INPUT @1 Column1  $255.;
RUN;

Then parse imported data into variables using a data step:

data result (keep=var1-var4);
set  WORK.IMPORTED_DATA;

delim = '@A$3';
end = 1;
begin = 1;
do while (end > 0);

    end = find(Column1, delim, begin);
    row = substr(Column1, begin, end - begin);

    var1 = scan(row, 1);
    var2 = scan(row, 2);
    var3 = scan(row, 3);
    var4 = scan(row, 4);

    begin = end + length(delim);
    output;
end;
run;
1
votes

Try this in data step by viewing @A$3 as a multi-character delimiter:

data want (keep=subject);
    infile 'C:\sasdata\test.txt';
    input;                                                     
    length line $4500  subject $80;
    line=tranwrd(_infile_,"@A$3",'!');         

    do i=1 by 1 while (scan(line,i,'!') ^= ' ');
        subject=scan(line,i,'!');                       
        output;
    end;
run;

_infile_ gives the current row that is being read in the data step. I converted the multi-character delimiter @A$2 into a single-character delimiter. tranwrd() can replace a sub-string inside a string. And then use the delimiter inside the scan() function.

Also, if you want to break the values up into separate variables, just scan some more. E.g. put something like B = scan(subject,2); into do loop and data want (keep= A B C D);. Cheers.