0
votes

First of all, I beg your pardon for the lack of my technical english. I'll try to make my self clear with some images.

I'm trying to import a text file "as is" to SAS. It looks like this:

File sample

In text:

YPJC200,FG;00899;Pesos;0;3500;EDENOR S.A. "B" 1 VOTO
TRAD15.9FF;00902;Pesos;0;3000;EDENOR S.A. "B" 1 VOTO

It's a text file delimited with semicolon ";" but in addition it has:

  • Some names has dot "." in it (eg: TRAD15.9FF)
  • Some names has comma "," in it (eg: YPJC200,FG)
  • Column description has doble quotes

Of course I'm getting errors or column displacements.

As I said before, I want to import the full file "as is" in one column, replace the three first lines (that are wrong headers), replace with correct ones in one line, and export to text file again. So I need to get the same file with headers changed.

I'm using this code after trying other things:

DATA    WORK.WANT;
    INFILE '\\server\Interfaces\position.txt'
    DSD LRECL = 32767 MISSOVER DLM = '|';
    INFORMAT CAMPO $1500.;
    INPUT    CAMPO $;
    IF _N_ >= 6;
RUN;

With "INFORMAT CAMPO $1500.;" I'm loading the file in just one column.

I'm using " DLM = '|' " to avoid delimiters. I can't set any delimiter other than this because my file has all of them (; . , ) except of course the pipe. If avoid the "DLM" option it assumes comma delimiter, so it's unuseful to.

But now I have the problem with double quotes.

I'm getting double quotes everywhere like this:

"YPJC200,FG;00899;Pesos;0;3500;EDENOR S.A. ""B"" 1 VOTO"
"TRAD15.9FF;00902;Pesos;0;3000;EDENOR S.A. ""B"" 1 VOTO"

So, the direct question is: How can I import the file exactly as it is, line by line, char by char, in the simplest way?

Thanks in advance

2
Please post your sample data as text as part of your question. Do you expect people to type it out for you?user667489
I add it now. If you see where it says "File Sample" is a link to an image, but as I have a reputation of 8 Stackoverflow didn't let me to paste into the post. The image has the column names to read it easily as it has a lot of "delimiters".Edu Ortiz

2 Answers

0
votes

If you want to read the full line into one long character variable why are you mentioning anything to do with delimiters on the INFILE statement? If you want to preserve leading spaces then use the $CHAR informat.

data want ;
  infile '\\server\interfaces\position.txt' truncover firstobs=6;
  input campo $char1500.;
run;
0
votes

Don't use DSD in your infile statement. This will make SAS treat quote characters differently from other characters, which you don't want to do here.

Also, you can write your file straight out to another text file with fixed headers without ever creating a SAS dataset from it, e.g.

DATA _null_;
    INFILE '\\server\Interfaces\position.txt' LRECL = 32767 firstobs = 6;
    file "\\path\to\fixed\file.txt" ;
    INPUT;
    if _n_ = 1 then put "var1,var2,..."; /*insert fixed header row text here*/
    put _infile_;
RUN;