BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Junyong
Pyrite | Level 9

If there is a CSV file inside a ZIP file, for example, then we can directly import the data using a DATA step as follows.

filename have zip "!userprofile\desktop\have.zip";
data dataset;
infile have(filetoread.csv) dsd;
input year one two three;
run;

The DATA step does not read an XLS file, so we need PROC IMPORT instead. Does PROC IMPORT provide something similar? Now I am using DATA _NULL_ with INFILE, FILE, INPUT, PUT, etc. to unzip first before reading but wonder whether there is a direct way to avoid unzipping and directly access inside.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

I think that PROC IMPORT has some quirk that makes it not work with members from a ZIP file.

Try copying the file first.

filename down1 "%sysfunc(getoption(work))\download.zip";
proc http method="get" out=down1
url="http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Research_Data_Factors_CSV.zip";
run;
filename down2 zip "%sysfunc(getoption(work))\download.zip" member="F-F_Research_Data_Factors.CSV";
filename down3 temp;
data _null_;
  infile down2;
  file down3;
  input;
  put _infile_;
run;
proc import file=down3 dbms=csv out=data1 replace;
run;

View solution in original post

6 REPLIES 6
Junyong
Pyrite | Level 9

Thanks for this comment, but it seems this code doesn't work.

filename down1 "%sysfunc(getoption(work))\download.zip";
proc http method="get" out=down1
url="http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Research_Data_Factors_CSV.zip";
run;
filename down2 zip "%sysfunc(getoption(work))\download.zip" member="F-F_Research_Data_Factors.CSV";
proc import file=down2 dbms=csv out=data1 replace;
run;

PROC HTTP downloads down1 correctly, but PROC IMPORT fails to read down2 inside.

Tom
Super User Tom
Super User

I think that PROC IMPORT has some quirk that makes it not work with members from a ZIP file.

Try copying the file first.

filename down1 "%sysfunc(getoption(work))\download.zip";
proc http method="get" out=down1
url="http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/F-F_Research_Data_Factors_CSV.zip";
run;
filename down2 zip "%sysfunc(getoption(work))\download.zip" member="F-F_Research_Data_Factors.CSV";
filename down3 temp;
data _null_;
  infile down2;
  file down3;
  input;
  put _infile_;
run;
proc import file=down3 dbms=csv out=data1 replace;
run;
Tom
Super User Tom
Super User

@Junyong wrote:

If there is a CSV file inside a ZIP file, for example, then we can directly import the data using a DATA step as follows.

filename have zip "!userprofile\desktop\have.zip";
data dataset;
infile have(filetoread.csv) dsd;
input year one two three;
run;

The DATA step does not read an XLS file, so we need PROC IMPORT instead. Does PROC IMPORT provide something similar? Now I am using DATA _NULL_ with INFILE, FILE, INPUT, PUT, etc. to unzip first before reading but wonder whether there is a direct way to avoid unzipping and directly access inside.


Does the ZIP contain an XLS file or a CSV file?  You cannot read an XLS file with a data step.  It is a binary format.  You could read the CSV file.  Normally you would put quotes around the member name in your reference to the member of the aggregate storage location that the HAVE libref is pointing to.

infile have("filetoread.csv") dsd;

Also make sure that the file is not saved with a path in the ZIP file. If it is then you will need to include that path as part of the quoted name.

 

Junyong
Pyrite | Level 9

Thanks for this reply. I know DATA doesn't accept XLS, but wonder if PROC IMPORT can directly access to an XLS member file inside a ZIP file as DATA can.

Tom
Super User Tom
Super User

@Junyong wrote:

Thanks for this reply. I know DATA doesn't accept XLS, but wonder if PROC IMPORT can directly access to an XLS member file inside a ZIP file as DATA can.


Same answer:

I think that PROC IMPORT has some quirk that makes it not work with members from a ZIP file.

Although for a binary file like an XLS file you will need to use FILECOPY or some other method instead of the simple line by line copy that will work with a text file.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 735 views
  • 0 likes
  • 3 in conversation