4

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?

user3142441
  • 55
  • 1
  • 3
  • 9
  • 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 Dec 08 '14 at 18:51

3 Answers3

2

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.

DomPazz
  • 12,415
  • 17
  • 23
  • We had issues with `DLMSTR=`, it did not recognize `@A$3@A$3` as a missing value. That was in SAS 9.2, maybe it's fixed now. – Dejan Peretin Dec 29 '13 at 08:26
  • @DejanPeretin I had also thought of using `DLMSTR=` for specifying a multi-character delimiter, but it might not work across all versions. Hence, an alternative conversion method to single-char delimiter needs to be used. I think DomPazz's `DLMSTR=` should work fine for SAS 9.2 and newer since it was introduced in 9.2, but for 9.1.3 and older it would return as an error. – Yick Leung Dec 29 '13 at 08:41
  • 2
    Found this: If there are consecutive delimiter strings in the file which indicate missing values, the DLMSTR= option will not read the file properly until SAS 9.3. Source: http://support.sas.com/kb/33/385.html – Dejan Peretin Dec 29 '13 at 12:17
1

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;
Dejan Peretin
  • 10,891
  • 1
  • 45
  • 54
1

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.

Yick Leung
  • 1,048
  • 1
  • 9
  • 16