<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Open ZIP file to import batch of CSV files without external program in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482086#M124811</link>
    <description>&lt;P&gt;I've had success with extracting csv files from a .zip file using the macro contained within this &lt;A href="http://support.sas.com/resources/papers/proceedings12/057-2012.pdf" target="_self"&gt;paper&lt;/A&gt;. This also handles the zipping if you need to, but I only used it to unzip. It basically creates a .vbs script to handle all the unzipping. You could probably unzip all the csvs to a single path, then continue with your statements below. The instructions in the paper explain it better than I can here.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 27 Jul 2018 22:21:53 GMT</pubDate>
    <dc:creator>bobpep212</dc:creator>
    <dc:date>2018-07-27T22:21:53Z</dc:date>
    <item>
      <title>Open ZIP file to import batch of CSV files without external program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482075#M124808</link>
      <description>&lt;P&gt;I'm wondering if there's a way to uncompress a ZIP file so that SAS can dynamically import a batch of CSV files. I know the filename statement can unzip files, but I need to point to a folder of CSV files, because I need to use the file names for further processing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So far, I've gotten the following code to work on a folder in which all of the CSV files are already unzipped. Would love to back up this code one more step and start with a ZIP file. Using Base SAS 9.4.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; /* 1. Get file names from folder*/

filename csvfiles pipe 'dir "filepath\*.csv" /b';

data csvlist;
	length csvname $100; 
        infile csvfiles length=reclen;
	input csvname $varying100. reclen;
run;

/* 2. Create filepaths for proc import*/

data _null_;
	set csvlist end=end;
	count+1;
	call symputx('path'||put(count,4.-l),cats('filepath\',csvname));
	if end then call symputx('max',count);
run;


/* 3. Import CSV files one at a time using list of file paths from 1&amp;amp;2*/

%macro importcsv;
	%do i=1 %to &amp;amp;max;
		proc import datafile="&amp;amp;&amp;amp;path&amp;amp;i"
			out=temp&amp;amp;i  
			dbms=csv
			replace;
			getnames=yes;
			datarow=2;
		run;
	%end;
%mend importcsv;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Jul 2018 21:41:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482075#M124808</guid>
      <dc:creator>viola</dc:creator>
      <dc:date>2018-07-27T21:41:42Z</dc:date>
    </item>
    <item>
      <title>Re: Open ZIP file to import batch of CSV files without external program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482086#M124811</link>
      <description>&lt;P&gt;I've had success with extracting csv files from a .zip file using the macro contained within this &lt;A href="http://support.sas.com/resources/papers/proceedings12/057-2012.pdf" target="_self"&gt;paper&lt;/A&gt;. This also handles the zipping if you need to, but I only used it to unzip. It basically creates a .vbs script to handle all the unzipping. You could probably unzip all the csvs to a single path, then continue with your statements below. The instructions in the paper explain it better than I can here.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jul 2018 22:21:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482086#M124811</guid>
      <dc:creator>bobpep212</dc:creator>
      <dc:date>2018-07-27T22:21:53Z</dc:date>
    </item>
    <item>
      <title>Re: Open ZIP file to import batch of CSV files without external program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482100#M124813</link>
      <description>&lt;P&gt;You can read directly from the zip file, without unzipping it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is how you read it, note the filename statements:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://kb.iu.edu/d/azva" target="_blank"&gt;https://kb.iu.edu/d/azva&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And here's how you get the list of items in your zip file&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/sasdummy/2016/10/16/filename-zip-list-file-contents/" target="_blank"&gt;https://blogs.sas.com/content/sasdummy/2016/10/16/filename-zip-list-file-contents/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80715"&gt;@viola&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I'm wondering if there's a way to uncompress a ZIP file so that SAS can dynamically import a batch of CSV files. I know the filename statement can unzip files, but I need to point to a folder of CSV files, because I need to use the file names for further processing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So far, I've gotten the following code to work on a folder in which all of the CSV files are already unzipped. Would love to back up this code one more step and start with a ZIP file. Using Base SAS 9.4.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; /* 1. Get file names from folder*/

filename csvfiles pipe 'dir "filepath\*.csv" /b';

data csvlist;
	length csvname $100; 
        infile csvfiles length=reclen;
	input csvname $varying100. reclen;
run;

/* 2. Create filepaths for proc import*/

data _null_;
	set csvlist end=end;
	count+1;
	call symputx('path'||put(count,4.-l),cats('filepath\',csvname));
	if end then call symputx('max',count);
run;


/* 3. Import CSV files one at a time using list of file paths from 1&amp;amp;2*/

%macro importcsv;
	%do i=1 %to &amp;amp;max;
		proc import datafile="&amp;amp;&amp;amp;path&amp;amp;i"
			out=temp&amp;amp;i  
			dbms=csv
			replace;
			getnames=yes;
			datarow=2;
		run;
	%end;
%mend importcsv;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jul 2018 22:32:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482100#M124813</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-07-27T22:32:44Z</dc:date>
    </item>
    <item>
      <title>Re: Open ZIP file to import batch of CSV files without external program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482103#M124815</link>
      <description>&lt;P&gt;Do you really need to use PROC IMPORT?&amp;nbsp; Don't you know what format the files have?&lt;/P&gt;
&lt;P&gt;Try this little example.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let wdir=%sysfunc(pathname(work));

data _null_;
   file "&amp;amp;wdir/example.zip" zip member='file1.csv' dsd ;
   set sashelp.class (obs=5);
   put (_all_) (+0);
run;

data _null_;
   file "&amp;amp;wdir/example.zip" zip member='file2.csv' dsd ;
   set sashelp.class (firstobs=6 obs=10);
   put (_all_) (+0);
run;

data want ;
  if 0 then set sashelp.class ;
  infile "&amp;amp;wdir/example.zip" zip member='*' dsd truncover ;
  input (_all_) (+0);
run;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Jul 2018 22:45:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482103#M124815</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-27T22:45:50Z</dc:date>
    </item>
    <item>
      <title>Re: Open ZIP file to import batch of CSV files without external program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482107#M124816</link>
      <description>&lt;P&gt;I was wondering if I had any extra steps in here. The file extensions will always be the same, but the file names will change, and I don't want to have to look at what the names are each time I run this procedure. I'm not super familiar with what your proposed code is trying to achieve - will it let me read in names dynamically?&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jul 2018 22:57:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482107#M124816</guid>
      <dc:creator>viola</dc:creator>
      <dc:date>2018-07-27T22:57:49Z</dc:date>
    </item>
    <item>
      <title>Re: Open ZIP file to import batch of CSV files without external program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482139#M124825</link>
      <description>&lt;P&gt;No, you do have to know the file names. The link I showed indicates how to determine the name in the files using code. This allows you to use those in your code, you're not expected to use it manually. You may end up with a small macro but it should be pretty straightforward.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80715"&gt;@viola&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I was wondering if I had any extra steps in here. The file extensions will always be the same, but the file names will change, and I don't want to have to look at what the names are each time I run this procedure. I'm not super familiar with what your proposed code is trying to achieve - will it let me read in names dynamically?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Jul 2018 03:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482139#M124825</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-07-28T03:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: Open ZIP file to import batch of CSV files without external program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482143#M124827</link>
      <description>&lt;P&gt;The program is demonstrating that if your ZIP consists of multiple files that all share the same formal you do NOT need to know the names of the individual files to read them all with one data step without any macro logic or calls to procedures.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However if the ZIP files are not well defined.&lt;/P&gt;
&lt;P&gt;1) They have mixed combination of files, some of which you want to read and others that you want to ignore.&lt;/P&gt;
&lt;P&gt;2) The formats of the files (what columns they contain, what order the columns are in, what type of data the columns contain, ...) vary with in zip file.&lt;/P&gt;
&lt;P&gt;3) perhaps other confounding factors&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then you would be better served to get the list of files and in the ZIP file and use that to drive your process.&amp;nbsp; You might be able to use DOPEN() and DREAD() functions to do that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;In particular if you have to&amp;nbsp;use PROC IMPORT to guess about what is in the files then you will have to first copy the files out of the ZIP file.&amp;nbsp;&lt;/STRONG&gt; At least as of the last time I tried it PROC IMPORT could &lt;STRONG&gt;NOT&lt;/STRONG&gt; import from a member of a ZIP file.&amp;nbsp; Even if you use a SAS filename statement to point to a specific member of the ZIP file.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Jul 2018 03:26:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482143#M124827</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-28T03:26:02Z</dc:date>
    </item>
    <item>
      <title>Re: Open ZIP file to import batch of CSV files without external program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482146#M124828</link>
      <description>Tom is trying to indicate that PROC IMPORT will cause you problems in this process. Instead of PROC IMPORT you should use a data step to read the CSV files.  If the files are all supposed to be the same, I echo his concerns. PROC IMPORT guesses at variable types and lengths and not always correctly. You can try and get around this with GUESSINGROWS=MAX option but it slows the process down a lot, and still not a guarantee. If the files are different enough and you try and use them together you'll end up with issues because one variable will be character in one data set and numeric in another.  And then your next question is how to clean your data.  Seriously, having done this many times, make as much effort as possible to get the data read in correctly and cleanly. You save yourself time downstream.</description>
      <pubDate>Sat, 28 Jul 2018 03:28:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482146#M124828</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-07-28T03:28:14Z</dc:date>
    </item>
    <item>
      <title>Re: Open ZIP file to import batch of CSV files without external program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482150#M124829</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80715"&gt;@viola&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;The following link should give you all you need.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/sasdummy/2014/01/29/using-filename-zip/" target="_blank"&gt;https://blogs.sas.com/content/sasdummy/2014/01/29/using-filename-zip/&lt;/A&gt; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Jul 2018 03:53:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482150#M124829</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-07-28T03:53:58Z</dc:date>
    </item>
    <item>
      <title>Re: Open ZIP file to import batch of CSV files without external program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482563#M125021</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;for your explanations. I'm going to have to think this through a little more. I have about 60 files to read in, and each one is unique. However, once imported I do not have to combine them - they will stay as separate data sets, but it will be important to make sure that the formats are correct in the end. I hear what you're saying about PROC Import being imprecise and am reconsidering its use. Ultimately, I will need a solution that does not require my hard coding each of the variables - I do have a spec sheet that may be able to solve this problem.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jul 2018 18:27:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482563#M125021</guid>
      <dc:creator>viola</dc:creator>
      <dc:date>2018-07-30T18:27:30Z</dc:date>
    </item>
    <item>
      <title>Re: Open ZIP file to import batch of CSV files without external program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482907#M125159</link>
      <description>&lt;P&gt;Thanks! This macro ended up being the simplest solution for my problem, as now I have the flexibility to import the files from a local folder.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jul 2018 18:30:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/482907#M125159</guid>
      <dc:creator>viola</dc:creator>
      <dc:date>2018-07-31T18:30:00Z</dc:date>
    </item>
    <item>
      <title>Re: Open ZIP file to import batch of CSV files without external program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/551867#M153389</link>
      <description>&lt;P&gt;Hi Bob, I really like the functionality contained in this macro.&amp;nbsp; As a relatively new user, and still on SAS Studio, I'm having a hard time getting this to work.&amp;nbsp; I amended the first line to suit my needs:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366FF"&gt;%macro SASZip_Lite(zip=/folders/myfolders/source_folder/demo_20170101.zip, tfdr=/folders/myfolders/DEMO_Q1_2017_CSV);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Do you have any suggestions as to why this isn't working?&amp;nbsp; I've tried it with/without quotes, with/without the other terms included(sfdr fstyl, etc)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Thank you.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2019 19:28:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/551867#M153389</guid>
      <dc:creator>JMCass</dc:creator>
      <dc:date>2019-04-17T19:28:19Z</dc:date>
    </item>
    <item>
      <title>Re: Open ZIP file to import batch of CSV files without external program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/551883#M153390</link>
      <description>To run a macro you first run the macro definition code and then you call it separately. &lt;BR /&gt;&lt;BR /&gt;ie &lt;BR /&gt;&lt;BR /&gt;pretend macro:&lt;BR /&gt;&lt;BR /&gt;%macro print(dsn=);&lt;BR /&gt;proc print data=&amp;amp;dsn.;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%mend;&lt;BR /&gt;&lt;BR /&gt;THen to call it you use:&lt;BR /&gt;&lt;BR /&gt;%print(sashelp.class);&lt;BR /&gt;&lt;BR /&gt;I think you're mixing up the macro and the macro call.</description>
      <pubDate>Wed, 17 Apr 2019 20:13:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Open-ZIP-file-to-import-batch-of-CSV-files-without-external/m-p/551883#M153390</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-04-17T20:13:49Z</dc:date>
    </item>
  </channel>
</rss>

