CarLaTeX
CarLaTeX

Reputation: 305

How to import a txt file with single quote mark in a variable and another in another variable

I have a file prova.txt, which contains:

001|PROVA|MILANO|1000
002|'80S WERE GREAT|FORLI'|1100
003|'80S WERE GREAT|ROMA|1110

I'm importing it as a SAS dataset with this code:

libname mylib "/my/lib"; 

data prova;
infile '/my/lib/prova.txt' 
    dlm='|' dsd lrecl=50 truncover;
format 
    codice  $3.
    nome    $20.    
    luogo   $20.
    importo 4.
    ;
input 
    codice  :$3.
    nome    :$20.
    luogo   :$20.       
    importo :4.
    ;
run;

And I get this result:

enter image description here

As you can see, the first and the third records are imported well, whereas the second has nome = 80S WERE GREAT|FORLI and the rest of the variable are a mess.

How can I correctly import this file?

P.S. = the single quote marks in the file are correct. FORLI' is name of a town in Italy and there is a firm whose name begins with '80. Of course these are not the real names, but the real case is exactly like that. I need to import the variables with a quote marks in their content.

Upvotes: 0

Views: 322

Answers (1)

Richard
Richard

Reputation: 27508

The DSD and DLM cause the single quoted (') embedded delimiter (|) to be part of the data value.

Remove the DSD options and the delimiter will not be considered 'value embedded' and thus become a value separator for the example case of 002|'80S WERE GREAT|FORLI'|1100

The format statement implicitly defines a variable value type and length if it contains the first mention of a variable in the step. Because of that you can simplify the input statement to be just a list of the variables:

filename sample 'c:\temp\sample.txt';
data _null_;
  file sample;
  input;
  put _infile_;
datalines;
001|PROVA|MILANO|1000
002|'80S WERE GREAT|FORLI'|1100
003|'80S WERE GREAT|ROMA|1110
;

data want;
data prova;
infile sample dlm='|' lrecl=50 truncover;
format 
    codice  $3.
    nome    $20.    
    luogo   $20.
    importo 4.
    ;
input 
    codice  
    nome    
    luogo   
    importo 
    ;

putlog _infile_;
run;

proc print;
run;

enter image description here

Upvotes: 2

Related Questions