BookmarkSubscribeRSS Feed
AlexeyS
Pyrite | Level 9

Hello, i have a problem with importing csv file.

the file contains from two columns, one numeric(id), and one character(name).

This Csv file was created as export from sql.

When i use proc import procedure i get that two my variables are character i use options getnames=yes and datarow=2).

But what is interesting, that if i open this file in excel and save it as Csv file, and then use import proc, i get one variable numeric and one character.

What is problem? What i can do?  Can i use proc import?

Thank you

4 REPLIES 4
Reeza
Super User

Set GuessingRows to the max number of observations. 

 

Proc import guesses at types. If you have two variables write a datastep to import your data. You have full control over the types. 

For an example see the log after your proc imports. You can see the differences in your two iterations. 

 

AlexeyS
Pyrite | Level 9

Unfortunately it doen't help. I don't want to write Infile statement

Kurt_Bremser
Super User

@AlexeyS wrote:

Unfortunately it doen't help. I don't want to write Infile statement


Well, you are at the point of an important decision.

Do you want to become a competent SAS user, or remain a button-pusher?

 

The best solution for importing a csv is a manually written data step that sets all attributes to your needs. And immediately fails when the import structure has accidentally changed from what you expected.

 (I'm not snarky here, I write from the perspective of ~20 years experience with SAS and data warehousing)

 

Reeza
Super User

Answering your specific questions:

1. What is problem? 

Proc Import guesses at types. There must be some difference in the CSV that causes SAS to see them differently. Excel can mess with values. If it looks like a date it will automatically convert it or removing leading zeroes. Your better off using the file from SQL 

 

2. What can you do?

Change GuessingRows to increase number of rows scanned before determining a variable type. 

Or

Control variable type explicitly using data step

 

3.Can I use Proc Import?

Sure, but you run the risk of inconsistent data. If only reading one dataset this isn't an issue. If you're reading many datasets it can get annoying fast. 

 

Writing a data step to explicitly define types is the only way to have full control. It's the same in most languages, if you want to control types you need to define them somehow. Computers only know what we tell them. 

 

What specifically about infile don't you want to deal with? 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1851 views
  • 0 likes
  • 3 in conversation