0
votes
filename Source 'C:\Source.txt';
Data Example;
Infile Source;
Input Var1 Var2;
Run;

Is there a way I can import all the variables from Source.txt without the "Input Var1 Var2" line? If there are many variables, I think it's too time consuming to list out all the variables, so I was wondering if there's any way to bypass that.

Thanks

2

2 Answers

1
votes

Maybe you can use proc import ?

For a CSV I use this and I don't have to define every variable

proc import datafile="&CSVFILE"
    out=myCsvData
    dbms=dlm
    replace;
    delimiter=';';
    getnames=yes;
    run;

It depends on what you have in your txt file. Try different delimiters.

0
votes

If you are looking at a solution which is INFILE statement based then following reference code should help.

data _null_;
  set sashelp.class;
  file '/tester/sashelp_class.txt' dsd dlm='09'x;
  put name age sex weight height;
run;

/* Version #1 : When data has mixed data(numeric and character) */
data reading_data_w_format;
  infile '/tester/sashelp_class.txt' dsd dlm='09'x;
  format name $10. age 8. gender $1. weight height 8.2;
  input (name--height) (:);
run;
proc print data=reading_data_w_format;run;
proc contents data=reading_data_w_format;run;

/* Version #2 : When all data can be read a character. 
   I know this version doesn't make sense, but it's still an option*/
data reading_data_wo_format;
  infile '/tester/sashelp_class.txt' dsd dlm='09'x;
  input (var1-var5) (:$8.); /* Length would be max length of value in all the columns */
run;
proc print data=reading_data_wo_format;run;
proc contents data=reading_data_wo_format;run;

I'd suggest to write down the informat for the variables to be read so that you are sure that the file is as per your specification. PROC IMPORT will try to scan the data first from 1st row till GUESSINGROWS(do not set it to high, if each column is of consistent length) value and based on the length and type, it will use an informat and length which it finds suitable for the reading the variables in the file.