Help using Base SAS procedures

Reading a CSV file

Accepted Solution Solved
Reply
Regular Contributor
Posts: 173
Accepted Solution

Reading a CSV file

Hello everyone,

I am trying to read a csv file.  For some reason my SAS code is reading beyond the last row.  To be more specific, the input file has 2101 rows, but SAS reads in 2706 rows.  Therefore, the last 605 rows are blank.  Here is my SAS code:

Data nov1 infile "my_dir\nov1.csv' delimiter = ',' missover DSD lrecl=32767 firstobs=2;

input

col1 col2 col3 ... col31;

run;

The code works for other csv files.  Am i missing something obvious here?

I appreciate any input anyone may have Smiley Happy

Thank you !


Accepted Solutions
Solution
‎04-02-2013 08:28 PM
Super User
Super User
Posts: 6,502

Re: Reading a CSV file

You will have to type the names at least once.  I usually just read them from the file and change the delimiters to spaces and past them into the program.

You can save some typing a couple of ways.

1) Put the names into a macro variable.

%let names=transaction_type avg .... last_var ;

input &names ;

if 0=n(of &names) then delete;

2) Use name ranges so you only have to retype the first and last variable name.

if 0=n(of transaction_type -- last_var) then delete ;

View solution in original post


All Replies
Respected Advisor
Posts: 3,777

Re: Reading a CSV file

Did you get any interesting or unexpected messages in the log?

You may need to specify TERMSTR but without knowing more that is just a guess.

Super User
Super User
Posts: 6,502

Re: Reading a CSV file

Sounds like your file just has some extra blank lines at the end.

Why not just delete the rows without any non-missing values?

data nov1;

  infile "my_dir\nov1.csv' delimiter = ',' missover DSD lrecl=32767 firstobs=2;

  input col1 - col31 ;

   if 0=n(of col1-col31) then delete;

run;

Regular Contributor
Posts: 173

Re: Reading a CSV file

Thank you data_null and Tom!

Tom,

Your suggestion sounds great.  The only problem is the columns are not named 'col1 ... col31).  They are named like 'transaction_type', 'avg', 'cust_id',... etc.;

How would I adopt these columns in your suggestion?

Thank you!

Solution
‎04-02-2013 08:28 PM
Super User
Super User
Posts: 6,502

Re: Reading a CSV file

You will have to type the names at least once.  I usually just read them from the file and change the delimiters to spaces and past them into the program.

You can save some typing a couple of ways.

1) Put the names into a macro variable.

%let names=transaction_type avg .... last_var ;

input &names ;

if 0=n(of &names) then delete;

2) Use name ranges so you only have to retype the first and last variable name.

if 0=n(of transaction_type -- last_var) then delete ;

Regular Contributor
Posts: 173

Re: Reading a CSV file

Thank you, Tom, for your input!  That worked beautifully.

I really appreciate it Smiley Happy

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 215 views
  • 1 like
  • 3 in conversation