Proc import also work for space-delimited files. When dbms = dlm is indicated, SAS assumes the file is space-delimited unless otherwise specified (as we did above for commas and tabs). We first show sample a program for reading in a space-delimited file that does not include variable names. SAS creates default variable names as VAR1-VARn in when we do not provide variable names.
proc import datafile="practice_4.txt" out=mydata dbms=dlm replace;
getnames=no;
run;
Next, if your space-delimited file contains variable names, you change the getnames option
proc import datafile="practice_4.txt" out=mydata dbms=dlm replace;
getnames=yes;
run;
If you wish to create a permanent SAS data file using proc import in the directory "c:\practice4 you can use the code below.
libname dis "c:\practice4";
proc import datafile="practice_4.txt" out=dis.mydata dbms=dlm replace;
getnames=yes;
run;
Proc Import will have to either use the first line as variable names or to create dummy variables for the file. Another problem is that the Proc Import may not properly assign attributes such as the variable length and format. The Data Step with infile and input statement may provide better control in this aspect. You will,however need to specify the start and end for each variable on the input statement. When you run proc import, code for a data step is generated behind the scenes and that is what is actually run to read in your data. This data step can be found in the SAS log after running proc import and can be copied, amended, and rerun. The best solution is run PROC import first as outlined above and then view the SAS log. Then modify this code using an infile and input statement using TRUNCOVER
data WORK.MYDATA ;
%let _EFIERR_ = 0; /* set the ERROR detection macro variable */
infile 'C:\practice_4.txt" delimiter = ' ' TRUNCOVER DSD lrecl=32767 ;
informat VAR1 $X
informat VAR2 $X
informat VAR3 $X
informat VAR4 $X
*****************************
DATA temp;
infile 'C:\practice_4.txt" delimiter = ' ' TRUNCOVER DSD lrecl=32767 ;
INPUT var1 var2
RUN;
PROC PRINT DATA=temp;
RUN;
However, hard coding hundreds or even thousands of variables, if not totally impossible, is very tedious and time consuming, increasing the possibility of human errors. Also the contents of the dataset may change from time to time, making it even more difficult for SAS programmers to update and to maintain the codes. There is a good SUGI paper that describes how to efficiently import text files with a large number of variables. See: http://www.lexjansen.com/scsug/2007/data/Data-Hu.pdf
... View more