BookmarkSubscribeRSS Feed
shivakrishna
Fluorite | Level 6

Hi All,

i am getting error while importing the data from csv (proc import)

181  proc import out= cla datafile ="C:\shiva\not.csv" dbms=csv replace;

NOTE: The previous statement has been deleted.

182  Range="not$A5:D10";

     -----

     180

ERROR 180-322: Statement is not valid or it is used out of proper order.

183  run;

NOTE: The SAS System stopped processing this step because of errors.

NOTE: PROCEDURE IMPORT used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds

Thanks

Shiva

18 REPLIES 18
vomer
Obsidian | Level 7

Try this:

PROC IMPORT OUT=cla

            DATAFILE= "C:\shiva\not.csv"

            DBMS=CSV REPLACE;

     SHEET="sheet 1";

     GETNAMES=YES;

     MIXED=YES;

     USEDATE=YES;

     SCANTIME=YES;

RUN;

shivakrishna
Fluorite | Level 6

thank you vomer

but i need to capture only part of the data (A5:D10)no need full data.

thanks

Shiva

data_null__
Jade | Level 19

SHEET and RANGE have no meaning when DBMS is CSV.

shivakrishna
Fluorite | Level 6

why ?

antony_allianz
Fluorite | Level 6

Range is applicable for Excel files not for Comma Separated Value files.

Excel file:

PROC IMPORT OUT= WORK.TEST

            DATAFILE= "C:\test.xls"

            DBMS=EXCEL2000 REPLACE;

     RANGE="A4:D5";

     GETNAMES=NO;

RUN;

CSV file :

proc import datafile="C:\temp\test.csv"

     out=shoes

     dbms=csv

     replace;

     getnames=no;

run;

Reeza
Super User

CSV files are comma separated variables text flat files.  There are no cells and the data structure is that it is separated by commas and has a delimiter to specify the end of line.

RANGE, as specified, is a construct of Excel. Excel knows how to read CSV files, and CSV files usually automatically open in Excel but it is not an excel file and the ranges have no meaning in CSV files. If you want to use RANGE, convert it to an .xls or .xlsx file explicitly.

If your table or information has header information or some other columns you don't want, you'll need to find another way to explicitly read it, using a data step most likely.

vomer
Obsidian | Level 7

Try this:

PROC IMPORT DATAFILE="c:\<path>\test.xls"

  OUT=seq(<for dataset options - see below> )

  DBMS=excel REPLACE;

  GETNAMES=yes; * variable names are in row 1 ;

  RANGE="< range-name OR absolute-range"; * omit if you want to read the entire worksheet;

Of course, you will need to save the file in xls instead of csv.

shivakrishna
Fluorite | Level 6

Hi All

May i know we cannot read part of the data from the CSV file. if yes WHY ?

Thanks

Shiva

UrvishShah
Fluorite | Level 6

Hello,

Here is my try...You can read any feild of data by using OBS options...i think data step works fine rather than proc import...

It will be better if you post your CSV sample file...Here i made simple macro based on your need which extract the 5 to 10 observations...But you need to fix the variable structure in your INPUT statement...

%macro csv_extract(start,end);

/* Design data structure as per your requirement */

data csv_data;

   infile "C:\Documents and Settings\ShahUr01\Urvish\test.csv" dlm = "," firstobs = 2;  

   input name $ age;

run;

/* Extract the required data once it is available in SAS */

data csv_sample;

   set csv_data;

   if _n_ GE &start. and _n_ LE &end.;

run;

%mend;

%csv_extract(5,10);

-Urvish

shivakrishna
Fluorite | Level 6

Hi urvish thank you for your code but i know by using datastep....

can you please why we are not create part of the data of csv file by using proc step (proc import)

Shiva

CharlotteCain
Quartz | Level 8

Hi,

Your question gave me an opportunity to research and learn something new. Anyway, if you are looking to import only selected columns from an external file using PROC IMPORT, it seems the RANGE option in specifying the selected columns only works for .xls files alone and not for CSV files. I guess I am probably reiterating the point DN made earlier, however it helps in my understanding as i am a newbie too.

For CSV files, ideally it is best to read using datastep and to be honest in my production work environment the best practice is actually to avoid proc import. Of course, saying that you would want to do what suits you best.

Nevertheless if you do find something works to specify the range in CSV, please post that solution, so we can all learn.

Thanks,

Charlotte

antony_allianz
Fluorite | Level 6

WHY DO WE GIVE RANGE TO READ CSV FILES USING PROC IMPORT? IT IS UNNECESSARY ONE.


shivakrishna
Fluorite | Level 6

thank you antony,

can you please elaborate why it is unnesessary..

shiva


antony_allianz
Fluorite | Level 6

IF WE WANT PARTICULAR ROWS WE USE FIRST OBS & OBS.

IF WE WANT PARTICULAR ROWS IN RUN TIME USING MACROS WITH PARAMETERS & USING INFILE WITH IN MACRO DEFINITION.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 18 replies
  • 11765 views
  • 6 likes
  • 10 in conversation