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

时光毁灭记忆、已成空白 提交于 2021-02-08 10:42:22

问题


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:

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.


回答1:


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;




回答2:


By setting the qoute you are creating a String and the import won´t be able to seperate the variables properly.

your file should look more like this:

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


来源:https://stackoverflow.com/questions/60301822/how-to-import-a-txt-file-with-single-quote-mark-in-a-variable-and-another-in-ano

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!