I am attempting to import a pipe-delimited text file. I want all the fields to appear as text regardless of what transformations may occur later. For some reason, SAS decides that some fields are datetime and formats them accordingly. Is there a workaround with PROC IMPORT? If I use the infile statement, how do I get SAS to account for varying field lengths so they are not all cut off at 8 characters (I prefer not to specify lengths individually).
This is not a bug but the design of proc import.
If you need more control I would recommend running proc import. Then look in the LOG and you will find datastep code
SAS generated to read the file. Copy the code from the log to the editor and modify the INFORMAT statements to character informats which would be $XX. where XX represents the number of characters in the field. Remove the FORMAT statements as basically not much need with character. And modify the input statment to have a $ after each variable.
If the data actually are dates or datetimes and you manipulate them at all you would probably be better off using the SAS date / datetime though.
This is not a bug but the design of proc import.
If you need more control I would recommend running proc import. Then look in the LOG and you will find datastep code
SAS generated to read the file. Copy the code from the log to the editor and modify the INFORMAT statements to character informats which would be $XX. where XX represents the number of characters in the field. Remove the FORMAT statements as basically not much need with character. And modify the input statment to have a $ after each variable.
If the data actually are dates or datetimes and you manipulate them at all you would probably be better off using the SAS date / datetime though.
When reading delimeted files, PROC IMPORT writes a DATA step, though somewhat less sophisticated than the one written by DATA _NULL_ below. If you are running IMPORT from the Display Manager you can recover this program without copying from the LOG by using the RECALL command (under RUN) after executing the IMPORT . This can save a bit of editing of extraneous characters that will be a part of the LOG.
Thanks Art - that is what I ended up doing and it worked well.
Here is an example that creates variables based on the field names of a pipe delimited file where the first row are field names. The data is transposed into a model data set in this case with all character variables. The model data is used to drive the data step to read in the data fields beginnin in row 2.
One way is considering a loose length for all the variables .
data x;
infile 'xxxx';
input (var1 - var100) (: $200.);
....
Ksharp
that's a good time to use the compress=yes option
It is like having VARCHAR() for all fields instead of CHAR()
If you want PROC IMPORT to have an option to import all fields as character, please vote:
If you want PROC IMPORT to have an option to import all fields as character, please vote:
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: