Help using Base SAS procedures

PROC IMPORT bug in 9.2?

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

PROC IMPORT bug in 9.2?

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).


Accepted Solutions
Solution
‎10-30-2012 10:45 AM
Super User
Posts: 10,458

Re: PROC IMPORT bug in 9.2?

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.

View solution in original post


All Replies
Solution
‎10-30-2012 10:45 AM
Super User
Posts: 10,458

Re: PROC IMPORT bug in 9.2?

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.

Valued Guide
Posts: 632

Re: PROC IMPORT bug in 9.2?

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.

Contributor
Posts: 53

Re: PROC IMPORT bug in 9.2?

Thanks Art - that is what I ended up doing and it worked well.

Respected Advisor
Posts: 3,777

Re: PROC IMPORT bug in 9.2?

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.

filename FT15F001 temp;
parmcards;
Name|Sex|Age|Height|Weight
Alfred|M|
14|69|112.5
Alice|F|
13|56.5|84
Barbara|F|
13|65.3|98
Carol|F|
14|62.8|102.5
Henry|M|
14|63.5|102.5
James|M|
12|57.3|83
Jane|F|
12|59.8|84.5
Janet|F|
15|62.5|112.5
Jeffrey|M|
13|62.5|84
John|M|
12|59|99.5
Joyce|F|
11|51.3|50.5
Judy|F|
14|64.3|90
Louise|F|
12|56.3|77
Mary|F|
15|66.5|112
Philip|M|
16|72|150
Robert|M|
12|64.8|128
Ronald|M|
15|67|133
Thomas|M|
11|57.5|85
William|M|
15|66.5|112
;;;;
   run;
data fields;
   infile FT15F001 obs=1 dsd dlm='|';
  
length name $32 dummy $128;
  
retain dummy ' ';
  
input name @@;
   run;
proc transpose out=model(drop=_name_);
   var dummy;
   id name;
   run;
data class;
   if 0 then set model;
   infile FT15F001 firstobs=2 dsd dlm='|';
  
input (_all_) (Smiley Happy;
   run;
proc print;
  
run;
Super User
Posts: 9,662

Re: PROC IMPORT bug in 9.2?

One way is considering a loose length for all the variables .

data x;

infile 'xxxx';

input (var1 - var100) (: $200.);

....

Ksharp

Valued Guide
Posts: 2,174

Re: PROC IMPORT bug in 9.2?

that's a good time to use the compress=yes option

It is like having VARCHAR() for all fields instead of CHAR()

Contributor
Posts: 74

Re: PROC IMPORT bug in 9.2?

If you want PROC IMPORT to have an option to import all fields as character, please vote:

PROC IMPORT -- all fields as character

Contributor
Posts: 74

Re: PROC IMPORT bug in 9.2?

If you want PROC IMPORT to have an option to import all fields as character, please vote:

PROC IMPORT -- all fields as character

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 790 views
  • 1 like
  • 7 in conversation