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

FILEVAR allows to INFILE multiple CSVs with the same structure. How can I use this if the CSVs are in FILENAME ZIP? For example,

filename i "wid_all_data.zip";

proc http url="https://wid.world/bulk_download/wid_all_data.zip" out=i;
run;

filename i zip "wid_all_data.zip";

data i;
	i=dopen("i");
	do j=1 to dnum(i);
		k=cats('i("',dread(i,j),'")');
		output;
	end;
run;

data j;
	set i;
	infile l filevar=k dlm=";" firstobs=2;
	input (country variable percentile)(:$16.) year value age pop :$16.;
run;

SAS does not use k=i("WID_data_AN.csv") as FILEVAR here.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Sadly, this is not possible. To read a file in a ZIP archive, you need to create a file reference to the ZIP, and use FILEREF(MEMBER) syntax as the file specification in the INFILE statement. The variable used in the FILEVAR= option, OTOH, must hold a physical filename.

You will have to import each file separately, and concatenate the results.

View solution in original post

3 REPLIES 3
qoit
Pyrite | Level 9

Try the below great article, apologies as I am unbale to access URL links via my SAS client but try the below link:

 

https://blogs.sas.com/content/sasdummy/2015/05/11/using-filename-zip-to-unzip-and-read-data-files-in... 

 

Just add the below line:

 

filename inzip ZIP "c:\projects\data.zip";
 
/* Read the "members" (files) from the ZIP file */
data contents(keep=memname isFolder);
 length memname $200 isFolder 8;
 fid=dopen("inzip");
 if fid=0 then
  stop;
 memcount=dnum(fid);
 do i=1 to memcount;
  memname=dread(fid,i);
  /* check for trailing / in folder name */
  isFolder = (first(reverse(trim(memname)))='/');
  output;
 end;
if upcase(scan(memname,-1,".")) = CSV; /* Flag all CSV files */
 rc=dclose(fid);
run;
Junyong
Pyrite | Level 9

Can't I INFILE multiple CSVs in a ZIP without unzipping them?

Kurt_Bremser
Super User

Sadly, this is not possible. To read a file in a ZIP archive, you need to create a file reference to the ZIP, and use FILEREF(MEMBER) syntax as the file specification in the INFILE statement. The variable used in the FILEVAR= option, OTOH, must hold a physical filename.

You will have to import each file separately, and concatenate the results.

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