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.
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.
Try the below great article, apologies as I am unbale to access URL links via my SAS client but try the below link:
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;
Can't I INFILE multiple CSVs in a ZIP without unzipping them?
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.