<?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: How to Download a zip file and read the txt file in it? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-Download-a-zip-file-and-read-the-txt-file-in-it/m-p/790424#M253073</link>
    <description>&lt;P&gt;Thanks, Tom. I will try it out.&lt;/P&gt;</description>
    <pubDate>Sun, 16 Jan 2022 18:48:47 GMT</pubDate>
    <dc:creator>cokeyng</dc:creator>
    <dc:date>2022-01-16T18:48:47Z</dc:date>
    <item>
      <title>How to Download a zip file and read the txt file in it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Download-a-zip-file-and-read-the-txt-file-in-it/m-p/790315#M253011</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using Viya and would need to download a zip file and extract the csv file for data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found some sample codes online and tried it out.&lt;/P&gt;&lt;PRE&gt;FILENAME REFFILE temp;

PROC HTTP
	url="https://www150.statcan.gc.ca/n1/tbl/csv/18100004-eng.zip"
	method="GET"
    out=REFFILE;
RUN;

data _null_;
    length myout $ 200;
    file REFFILE filename=myout;
    stop;
run;&lt;/PRE&gt;&lt;P&gt;And the output is&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;1    %studio_hide_wrapper;
82   FILENAME REFFILE temp;
83   
84   PROC HTTP
85   url="https://www150.statcan.gc.ca/n1/tbl/csv/18100004-eng.zip"
86   method="GET"
87       out=REFFILE;
88   RUN;
NOTE: PROCEDURE HTTP used (Total process time):
      real time           4.49 seconds
      cpu time            0.54 seconds
      
89   
90   data _null_;
91       length myout $ 200;
92       file REFFILE filename=myout;
93       stop;
94   run;
NOTE: The file REFFILE is:
      Filename=/var/sastmp/SAS_work939E00018EF3_rhlsvp01/#LN00223,
      Owner Name=cng1,Group Name=sassrusr,
      Access Permission=-rw-r--r--,
      Last Modified=14Jan2022:20:27:39
NOTE: 0 records were written to the file REFFILE.
NOTE: DATA statement used (Total process time):
      real time           0.07 seconds
      cpu time            0.00 seconds
      
95   
96   %studio_hide_wrapper;
107  
108  &lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I have a few questions:&lt;/P&gt;&lt;P&gt;1. how do i know if it was a successful download?&lt;/P&gt;&lt;P&gt;2. the file reference is stored at REFFILE. To extract the csv file from there, I will have to define another file ref ZIP, right? but how do I transfer the reference from REFFILE temp to a ZIP?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cokey&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Jan 2022 03:33:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Download-a-zip-file-and-read-the-txt-file-in-it/m-p/790315#M253011</guid>
      <dc:creator>cokeyng</dc:creator>
      <dc:date>2022-01-15T03:33:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to Download a zip file and read the txt file in it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Download-a-zip-file-and-read-the-txt-file-in-it/m-p/790325#M253016</link>
      <description>&lt;P&gt;You are downloaded the file and then immediately overwrote it by using the FILE statement instead of the INFILE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the file is a ZIP file then use the ZIP engine when reading.&lt;/P&gt;
&lt;P&gt;Since you asked SAS to make up a name for the file use the PATHNAME() function to find what name it used.&lt;/P&gt;
&lt;P&gt;Here is code to dump the first few lines of the first file in the ZIP file to the LOG so you can see if it looks right.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  infile "%sysfunc(pathname(reffile))" zip member='*' ;
  input;
  list;
  if _n_ &amp;gt;3 then stop;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 Jan 2022 06:27:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Download-a-zip-file-and-read-the-txt-file-in-it/m-p/790325#M253016</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-15T06:27:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to Download a zip file and read the txt file in it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Download-a-zip-file-and-read-the-txt-file-in-it/m-p/790341#M253025</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro importStatCan(tableNum = , dset= );

filename out "%sysfunc(getoption(work))/&amp;amp;tableNum.-eng.zip";

proc http
 url="https://www150.statcan.gc.ca/n1/tbl/csv/&amp;amp;tableNum.-eng.zip"
 method="get"
 out=out;
run;

filename out;

filename ext "%sysfunc(getoption(work))/&amp;amp;tableNum..csv";

filename inzip ZIP "%sysfunc(getoption(work))/&amp;amp;tableNum.-eng.zip";
 
data _null_;
   /* using member syntax here */
   infile inzip(&amp;amp;tableNum..csv) 
       lrecl=256 recfm=F length=length eof=eof unbuf;
   file ext lrecl=256 recfm=N;
   input;
   put _infile_ $varying256. length;
   return;
 eof:
   stop;
run;


proc import out=&amp;amp;dset. datafile=ext dbms=csv replace; 
guessingrows=max;
run;

filename ext clear;
filename inzip clear;

%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's a macro I wrote a few years back to download StatCan data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/5c8f4e769e898353d7e8115610f5ad5e" target="_blank" rel="noopener"&gt;https://gist.github.com/statgeek/5c8f4e769e898353d7e8115610f5ad5e&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: Successfully tested for your request:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%importStatCan(tableNum=18100004, dset=want);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Data set has 1,039,302 rows and 15 columns (141MB text file) and worked successfully.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It takes a while with the GUESSINGROWS=MAX set in the macro. If you want, change that to 1000 to make it run faster.&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/395603"&gt;@cokeyng&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using Viya and would need to download a zip file and extract the csv file for data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I found some sample codes online and tried it out.&lt;/P&gt;
&lt;PRE&gt;FILENAME REFFILE temp;

PROC HTTP
	url="https://www150.statcan.gc.ca/n1/tbl/csv/18100004-eng.zip"
	method="GET"
    out=REFFILE;
RUN;

data _null_;
    length myout $ 200;
    file REFFILE filename=myout;
    stop;
run;&lt;/PRE&gt;
&lt;P&gt;And the output is&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1    %studio_hide_wrapper;
82   FILENAME REFFILE temp;
83   
84   PROC HTTP
85   url="https://www150.statcan.gc.ca/n1/tbl/csv/18100004-eng.zip"
86   method="GET"
87       out=REFFILE;
88   RUN;
NOTE: PROCEDURE HTTP used (Total process time):
      real time           4.49 seconds
      cpu time            0.54 seconds
      
89   
90   data _null_;
91       length myout $ 200;
92       file REFFILE filename=myout;
93       stop;
94   run;
NOTE: The file REFFILE is:
      Filename=/var/sastmp/SAS_work939E00018EF3_rhlsvp01/#LN00223,
      Owner Name=cng1,Group Name=sassrusr,
      Access Permission=-rw-r--r--,
      Last Modified=14Jan2022:20:27:39
NOTE: 0 records were written to the file REFFILE.
NOTE: DATA statement used (Total process time):
      real time           0.07 seconds
      cpu time            0.00 seconds
      
95   
96   %studio_hide_wrapper;
107  
108  &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;I have a few questions:&lt;/P&gt;
&lt;P&gt;1. how do i know if it was a successful download?&lt;/P&gt;
&lt;P&gt;2. the file reference is stored at REFFILE. To extract the csv file from there, I will have to define another file ref ZIP, right? but how do I transfer the reference from REFFILE temp to a ZIP?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cokey&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Jan 2022 18:54:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Download-a-zip-file-and-read-the-txt-file-in-it/m-p/790341#M253025</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-15T18:54:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to Download a zip file and read the txt file in it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Download-a-zip-file-and-read-the-txt-file-in-it/m-p/790424#M253073</link>
      <description>&lt;P&gt;Thanks, Tom. I will try it out.&lt;/P&gt;</description>
      <pubDate>Sun, 16 Jan 2022 18:48:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Download-a-zip-file-and-read-the-txt-file-in-it/m-p/790424#M253073</guid>
      <dc:creator>cokeyng</dc:creator>
      <dc:date>2022-01-16T18:48:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to Download a zip file and read the txt file in it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Download-a-zip-file-and-read-the-txt-file-in-it/m-p/790425#M253074</link>
      <description>&lt;P&gt;Thanks, Reeza. I will try it out.&lt;/P&gt;</description>
      <pubDate>Sun, 16 Jan 2022 18:50:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Download-a-zip-file-and-read-the-txt-file-in-it/m-p/790425#M253074</guid>
      <dc:creator>cokeyng</dc:creator>
      <dc:date>2022-01-16T18:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to Download a zip file and read the txt file in it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Download-a-zip-file-and-read-the-txt-file-in-it/m-p/790820#M253219</link>
      <description>It works. Thanks.</description>
      <pubDate>Wed, 19 Jan 2022 07:24:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Download-a-zip-file-and-read-the-txt-file-in-it/m-p/790820#M253219</guid>
      <dc:creator>cokeyng</dc:creator>
      <dc:date>2022-01-19T07:24:01Z</dc:date>
    </item>
  </channel>
</rss>

