DATA Step, Macro, Functions and more

Importing a CSV File

Reply
Contributor
Posts: 23

Importing a CSV File

Hi,

 

 I have csv files having datetime variable in both text & datetime formats.

 

So, is there any way to import the file with datetime format?

 

if i import the data successfully, then i am having problem while appending with similar set of data.

 

your suggestion here will be highly appreciated.

Super User
Posts: 19,771

Re: Importing a CSV File

Posted in reply to ramya_sahu

ramya_sahu wrote:

 

 

So, is there any way to import the file with datetime format?

 

 


 

Get the import correct. 

Use the correct informats to specify the input format in a data step. If you use PROC IMPORT then you should make sure to set GUESSINGROWS to a very large number, possibly the size of the entire file so the data can be read in correctly. 

 


ramya_sahu wrote:

 

if i import the data successfully, then i am having problem while appending with similar set of data.

 

.


If you make sure that you specify the formats for the files and import all in the same manner then the issue will go away. 

Super User
Posts: 11,343

Re: Importing a CSV File

Posted in reply to ramya_sahu

ramya_sahu wrote:

Hi,

 

 I have csv files having datetime variable in both text & datetime formats.

 

if i import the data successfully, then i am having problem while appending with similar set of data.

 


It sounds like you are using proc import multiple times. Proc import has to guess characteristics of variables based on the values it sees. So multiple Proc Import on different files can result in different data types, lengths, informats and formats.

 

If these files are supposed to be the same structure use Proc import only one time. Use a large value for the guessingrows option, as large as the number of rows in the data set if it is less than 32,000 records.

Proc import will generate data step code that should appear in the log. Copy that data step from the log and paste into the editor.

Examine the informat statements. If you see a character informat for a variable you think should be numeric, date, datetime or time then you can change that. At the same time look at the character variables to see if the lengths are appropriate. A variable that is blank in the first few rows of a data set may get a length of 1 when it should be 25. I generally increase the length of character fields by 10 to 20% unless there is a file format document that says what the length should be, in which case use that length.

 

SAVE the modified program. Re-run with the new code.

To read the next data set use the same data step just change the name of the input file and the output data set name. Then all of the files will have the same characteristics.

Ask a Question
Discussion stats
  • 2 replies
  • 150 views
  • 1 like
  • 3 in conversation