Hi SAS Community,
I am trying to import a tab delimited file (sample file attached) where the first line is the variable names and lines 2 and 3 contain data from one id (should align as a single row in the SAS file). I used the following Import syntax:
proc import out=temp datafile='C:\Users\template.txt' dbms=dlm replace;
delimiter='09'x;
guessingrows=32767;
getnames=yes;
run;
The imported file is not in proper format and the variable names don't align with the variable values. I suspect this has to do with special characters in the character variables but for the life of me, I cant figure out how to fix it.
Any help will be much appreciated!
Best,
Pronabesh
The issue was carriage return and linefeed characters within quoted strings.
Here is the solution
/*----------------------------------*/
/* Import txt files to SAS files */
/*----------------------------------*/
/*------------------------------------------------------------------------*/
/* Remove carriage return and linefeed characters within quoted strings */
/*------------------------------------------------------------------------*/
data _null_;
infile 'C:\Users\Pronabesh\Desktop\SQLtoSAS\condition.txt' recfm=n sharebuffers;
file 'C:\Users\Pronabesh\Desktop\SQLtoSAS\condition.txt' recfm=n;
input a $char1.;
retain open 0;
/* This statement toggles the open flag. */
if a='"' then open=not open;
if a='0A'x and open then put ' ';
run;
/*----------------------------------*/
/* Import txt files to SAS files */
/*----------------------------------*/
proc import datafile='C:\Users\Pronabesh\Desktop\SQLtoSAS\condition.txt'
out=temp2
dbms=dlm replace;
delimiter='09'x;
guessingrows=32767; * Specifies the number of rows of the file to scan to determine the appropriate data type and length for the columns;
getnames=yes;
run;
SAS cannot guess correctly that two rows of data are a single record, it will align first column, second etc. according to the delimiter provided. I don't see any special characters, which are you concerned with.
You may need to investigate the details of the input statement regarding reading multiple lines and such.
You didn't provide much in the way of data to do any testing and you will want to specify what the expected result is for example data.
Thanks ballardw,
The SQL data miners provided me this data as tab delimited. I am assuming that every variable should be delimited by tab. However if I run my syntax on the sample dataset (agreed this is only a small sample) the delimiting does not work. I used '09'x as the delimiter.
Did you create Template.txt with an editor? I see '0d0d0a'x follow by " double quote on the next line.
I'm thinking that '0d0a' was created when you saved it.
Can you show a bit of the original unaltered file.
Hello Data_Null,
Thanks for your response. Here is a bigger cut of the data (see attached). I can send you the unadulterated file too but will need to strip some columns.
Run this program on the original file. Find the place with the '0d'x and show the output. I want to see what follows the '0d'x in the original unaltered file.
The issue was carriage return and linefeed characters within quoted strings.
Here is the solution
/*----------------------------------*/
/* Import txt files to SAS files */
/*----------------------------------*/
/*------------------------------------------------------------------------*/
/* Remove carriage return and linefeed characters within quoted strings */
/*------------------------------------------------------------------------*/
data _null_;
infile 'C:\Users\Pronabesh\Desktop\SQLtoSAS\condition.txt' recfm=n sharebuffers;
file 'C:\Users\Pronabesh\Desktop\SQLtoSAS\condition.txt' recfm=n;
input a $char1.;
retain open 0;
/* This statement toggles the open flag. */
if a='"' then open=not open;
if a='0A'x and open then put ' ';
run;
/*----------------------------------*/
/* Import txt files to SAS files */
/*----------------------------------*/
proc import datafile='C:\Users\Pronabesh\Desktop\SQLtoSAS\condition.txt'
out=temp2
dbms=dlm replace;
delimiter='09'x;
guessingrows=32767; * Specifies the number of rows of the file to scan to determine the appropriate data type and length for the columns;
getnames=yes;
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.