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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 846 views
  • 0 likes
  • 3 in conversation