<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Importing CSV File in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Importing-CSV-File/m-p/48903#M13256</link>
    <description>Lane&lt;BR /&gt;
don't use proc import, &lt;BR /&gt;
Instead use a data step with &lt;BR /&gt;
an infile pointing to your data, &lt;BR /&gt;
length statements defining your columns &lt;BR /&gt;
and finally an input statement to read them.&lt;BR /&gt;
 &lt;BR /&gt;
Then you will have control over the parsing required. &lt;BR /&gt;
  &lt;BR /&gt;
The basic idea takes longer to describe that to demo&lt;BR /&gt;
Imagine I have a pipe-delimited file  stored as c:\temp\gambol.csv  holding text like&lt;BR /&gt;
id|name|state|money|date&lt;BR /&gt;
1|me|NV|$123,456.78|2-Apr-2011&lt;BR /&gt;
2|two of us are better|NV|-123,456.78|3-Apr-2011&lt;BR /&gt;
 &lt;BR /&gt;
Then this is the simple data step to read that[pre]data gambling ;&lt;BR /&gt;
  infile 'c:\temp\gambol.csv' DSD dlm='|' lrecl= 10000 firstobs=2 ;&lt;BR /&gt;
  length id 8  name  $30 state $2 money 8 date 6 ;&lt;BR /&gt;
  informat money dollar20. date date11. ;&lt;BR /&gt;
    format money dollar20. date date11. ;&lt;BR /&gt;
  input id -- date ;&lt;BR /&gt;
run ;[/pre]The length statement ensures name can hold up to 30 characters.&lt;BR /&gt;
The DSD option on the infile statement invokes CSV-type parsing. DLM= indicates the delimiter. &lt;BR /&gt;
That informat statement is very handy as it indicates how to parse those columns which are not simple text or numbers. The format statements ensures the internal storage is converted to something more like the input when you display this data.&lt;BR /&gt;
An INPUT statement defines the columns to read - and as SAS already knows about the columns you want to read, and learned about them in the order in which they appear on the input file, you need only define the first and last separated by the indication (&lt;B&gt;--&lt;/B&gt;) that you want all columns defined from ID to DATE. &lt;BR /&gt;
The syntax is probably not shorter than proc import, but is much more reliable because SAS is not required to make the judgement of how far to read through the file to decide what informat to use for each column.&lt;BR /&gt;
However, for a quick load of unknown data, proc import might be helpful. &lt;BR /&gt;
With 400,000 rows to read I imagine your input data are already well documented, so no need to ask proc import to make up its own definitions.&lt;BR /&gt;
  &lt;BR /&gt;
peterC</description>
    <pubDate>Mon, 02 May 2011 13:46:57 GMT</pubDate>
    <dc:creator>Peter_C</dc:creator>
    <dc:date>2011-05-02T13:46:57Z</dc:date>
    <item>
      <title>Importing CSV File</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Importing-CSV-File/m-p/48898#M13251</link>
      <description>SAS newbie here. I am trying to import a CSV file. Two columns are giving me trouble. I have county names in one column and state names in the other. Anything over 6 to 8 characters gets cut off in these columns once imported. Any idea on how I can keep SAS from cutting off my columns. I apologize if this is a problem that can be found by searching the forums, but I simply don't know what keywords to use to search for this problem. &lt;BR /&gt;
&lt;BR /&gt;
Also, it is too big to convert to XLS. 400,000+ rows of data. &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Thanks in advance</description>
      <pubDate>Tue, 12 Apr 2011 20:37:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Importing-CSV-File/m-p/48898#M13251</guid>
      <dc:creator>Lane</dc:creator>
      <dc:date>2011-04-12T20:37:23Z</dc:date>
    </item>
    <item>
      <title>Re: Importing CSV File</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Importing-CSV-File/m-p/48899#M13252</link>
      <description>Try using a length statement before you input the data.&lt;BR /&gt;
&lt;BR /&gt;
Good luck!</description>
      <pubDate>Tue, 12 Apr 2011 20:38:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Importing-CSV-File/m-p/48899#M13252</guid>
      <dc:creator>RickM</dc:creator>
      <dc:date>2011-04-12T20:38:45Z</dc:date>
    </item>
    <item>
      <title>Re: Importing CSV File</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Importing-CSV-File/m-p/48900#M13253</link>
      <description>Rick, thanks for the reply. You'll have to forgive me since I'm drastically unfamiliar with SAS. Where would the length code fall within the following import code? And what length is appropriate? (I'm assuming that, say "length county $15" doesn't specify 15 characters...am I correct here?)&lt;BR /&gt;
&lt;BR /&gt;
PROC IMPORT OUT= WORK.states &lt;BR /&gt;
            DATAFILE= "C:\Documents and Settings\dregiste\states.csv" &lt;BR /&gt;
            DBMS=csv REPLACE;&lt;BR /&gt;
     		GETNAMES=YES;&lt;BR /&gt;
	     RUN;</description>
      <pubDate>Tue, 12 Apr 2011 20:47:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Importing-CSV-File/m-p/48900#M13253</guid>
      <dc:creator>Lane</dc:creator>
      <dc:date>2011-04-12T20:47:47Z</dc:date>
    </item>
    <item>
      <title>Re: Importing CSV File</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Importing-CSV-File/m-p/48901#M13254</link>
      <description>I made a test csv file and used your code and it worked fine with me.  I usually use the datastep approach rather than proc import so I'm not sure why it isn't working for you.  You could try using &lt;BR /&gt;
&lt;BR /&gt;
PROC IMPORT OUT= WORK.states &lt;BR /&gt;
DATAFILE= "C:\states.csv" &lt;BR /&gt;
DBMS=csv REPLACE;&lt;BR /&gt;
GETNAMES=YES;&lt;BR /&gt;
GUESSINGROWS=1000;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
state.csv&lt;BR /&gt;
------------------------------------&lt;BR /&gt;
State,City&lt;BR /&gt;
Massachusetts,Boston&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: RickM

Message was edited by: RickM</description>
      <pubDate>Tue, 12 Apr 2011 21:18:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Importing-CSV-File/m-p/48901#M13254</guid>
      <dc:creator>RickM</dc:creator>
      <dc:date>2011-04-12T21:18:03Z</dc:date>
    </item>
    <item>
      <title>Re: Importing CSV File</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Importing-CSV-File/m-p/48902#M13255</link>
      <description>I added your guessingrows command and in the log i found a data step code with the informats and formats. I copied and pasted that and increased the number for county and state in both format and informat and that got it to work! Thanks for your help Rick!</description>
      <pubDate>Tue, 12 Apr 2011 21:31:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Importing-CSV-File/m-p/48902#M13255</guid>
      <dc:creator>Lane</dc:creator>
      <dc:date>2011-04-12T21:31:29Z</dc:date>
    </item>
    <item>
      <title>Re: Importing CSV File</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Importing-CSV-File/m-p/48903#M13256</link>
      <description>Lane&lt;BR /&gt;
don't use proc import, &lt;BR /&gt;
Instead use a data step with &lt;BR /&gt;
an infile pointing to your data, &lt;BR /&gt;
length statements defining your columns &lt;BR /&gt;
and finally an input statement to read them.&lt;BR /&gt;
 &lt;BR /&gt;
Then you will have control over the parsing required. &lt;BR /&gt;
  &lt;BR /&gt;
The basic idea takes longer to describe that to demo&lt;BR /&gt;
Imagine I have a pipe-delimited file  stored as c:\temp\gambol.csv  holding text like&lt;BR /&gt;
id|name|state|money|date&lt;BR /&gt;
1|me|NV|$123,456.78|2-Apr-2011&lt;BR /&gt;
2|two of us are better|NV|-123,456.78|3-Apr-2011&lt;BR /&gt;
 &lt;BR /&gt;
Then this is the simple data step to read that[pre]data gambling ;&lt;BR /&gt;
  infile 'c:\temp\gambol.csv' DSD dlm='|' lrecl= 10000 firstobs=2 ;&lt;BR /&gt;
  length id 8  name  $30 state $2 money 8 date 6 ;&lt;BR /&gt;
  informat money dollar20. date date11. ;&lt;BR /&gt;
    format money dollar20. date date11. ;&lt;BR /&gt;
  input id -- date ;&lt;BR /&gt;
run ;[/pre]The length statement ensures name can hold up to 30 characters.&lt;BR /&gt;
The DSD option on the infile statement invokes CSV-type parsing. DLM= indicates the delimiter. &lt;BR /&gt;
That informat statement is very handy as it indicates how to parse those columns which are not simple text or numbers. The format statements ensures the internal storage is converted to something more like the input when you display this data.&lt;BR /&gt;
An INPUT statement defines the columns to read - and as SAS already knows about the columns you want to read, and learned about them in the order in which they appear on the input file, you need only define the first and last separated by the indication (&lt;B&gt;--&lt;/B&gt;) that you want all columns defined from ID to DATE. &lt;BR /&gt;
The syntax is probably not shorter than proc import, but is much more reliable because SAS is not required to make the judgement of how far to read through the file to decide what informat to use for each column.&lt;BR /&gt;
However, for a quick load of unknown data, proc import might be helpful. &lt;BR /&gt;
With 400,000 rows to read I imagine your input data are already well documented, so no need to ask proc import to make up its own definitions.&lt;BR /&gt;
  &lt;BR /&gt;
peterC</description>
      <pubDate>Mon, 02 May 2011 13:46:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Importing-CSV-File/m-p/48903#M13256</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2011-05-02T13:46:57Z</dc:date>
    </item>
  </channel>
</rss>

