Export zip files using ods

Accepted Solution Solved
Reply
Occasional Contributor PWS
Occasional Contributor
Posts: 15
Accepted Solution

Export zip files using ods

[ Edited ]

Hello,

 

I'm trying to zip csv files and export them on a specific location. I constantly get an error message that actually states that I'm trying to put the zip file on a location that doesn't exists, because 'I want' to export it on the path/[old file name]

 

Does someone know what the solution for this is.

 

In the attachment you'll find the example of an export. Some paths or filenames are predefined.

In the error attachment you can find some info from the log, were it states that 'I want' to export it on the path/[old file name]

 

Thanks a lot


Accepted Solutions
Solution
‎01-21-2018 01:43 AM
Community Manager
Posts: 3,191

Re: Export zip files using ods

I think it's cleaner when you use filerefs -- that point to the files you need -- instead of file paths/names in your ODS archive directives.  I have an example in this article.

 

Complete code from my example:

 

/* Change this to suit your environment */
%let projectDir = c:\projects\sgf2013\filenamezip;

/* Clean slate! */
filename newfile "&projectDir./carstats.zip";
data _null_;
  if (fexist('newfile')) then 
  	rc = fdelete('newfile');
run;
filename newfile clear;

/* Create folder if it doesn't exist */
options dlcreatedir;
libname out "&projectDir./data";

/* Create some data */
filename newcsv "&projectDir./data/pct.csv";
proc means noprint data=sashelp.cars;
var msrp;
output out=out.pct median=p50 p95=p95 p99=p99;
run;
ods csv file=newcsv;
proc print data=out.pct;
format _all_; /* clear the formats */
run;
ods csv close;

/* Create an informative document about this package */
filename rm "&projectDir./readme.rtf";
ods rtf(readme) 
  file="&projectDir./readme.rtf" style=Printer;
ods rtf(readme) 
  text="These are some instructions for what to do next";
proc datasets lib=out nolist;
contents data=pct;
quit;
ods rtf(readme) close;

/* Creating a ZIP file with ODS PACKAGE */
ods package(newzip) open nopf;
ods package(newzip) add file=newcsv path="data/";
ods package(newzip) add file=rm;
ods package(newzip) publish archive 
  properties(
   archive_name="carstats.zip" 
   archive_path="&projectDir."
  );
ods package(newzip) close;

View solution in original post


All Replies
Super User
Posts: 8,590

Re: Export zip files using ods

In this path from the ERROR messsage:

/srv/sas/data/sasexports/selligent/selligent_wait/snip_hkgzk_vie_20180119.csv/expclang.snip_hkgzk_vie_export.zip

you have

snip_hkgzk_vie_20180119.csv

which is usually the name of a file, not a directory.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor PWS
Occasional Contributor
Posts: 15

Re: Export zip files using ods

Posted in reply to KurtBremser

Hi @KurtBremser,

 

Thank you. This is also my question. I can't really see were I go wrong. Mostly because of the predefined path. So "&file_export_wait_s" is actually this path; 

/srv/sas/data/sasexports/selligent/selligent_wait/

I don't understand why, when zipping the file, the original filename is added to the path...

 

 

Super User
Posts: 8,590

Re: Export zip files using ods

This is your problem:

archive_path="&file_export_wait_s"

I bet that this macro variable contains

/srv/sas/data/sasexports/selligent/selligent_wait/snip_hkgzk_vie_20180119.csv

and not just a path, as you used it in the previous data _null_ step in the FILE statement:

data _null_ ;          
set expclang.&_file_name._export ;   
FILE  "&file_export_wait_s" lrecl=4000 DLM= ';' ; 
/*FILE  "&file_export_ready_s" lrecl=4000 DLM= ';' ; */
if _n_ = 1 then put @1 "CUSTOMER_ID;SUBJECTLINE;PREHEADER;REFERRER;FLEXBANNERS;GLOBALIDS;TEXTS;CONTENT";   
put (_all_) (~);
run ;

That step would not work if the name in the FILE statement is a directory.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor PWS
Occasional Contributor
Posts: 15

Re: Export zip files using ods

Posted in reply to KurtBremser

Ohw, right. Of course. I just need to define the path again. Wow

 

Thanks

Super User
Posts: 8,590

Re: Export zip files using ods


PWS wrote:

Ohw, right. Of course. I just need to define the path again. Wow

 

Thanks



Often it just takes another pair of eyes ...

One of the reasons why forums like this are essential.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 8,590

Re: Export zip files using ods

One method to keep this from happening is to always separate path and file names:

%let out_path=/srv/sas/data/sasexports/selligent/selligent_wait;
%let csv_out=snip_hkgzk_vie_20180119.csv;
%let zip_out=snip_hkgzk_vie_export.zip;

And then you'd have

data _null_;
set expclang.&_file_name._export;
file "&out_path./&out_csv." lrecl=4000 DLM= ';';

and

ods package(newzip) publish archive 
  properties(
   archive_name="&out_zip." 
   archive_path="&out_path."
  );
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor PWS
Occasional Contributor
Posts: 15

Re: Export zip files using ods

Posted in reply to KurtBremser

Hi @KurtBremser

 

Thanks. Still have some problems with zipping the file. I would expect to have the same zip file name as the csv file. So, with todays date. But somehow I'm not able to get it right. I need to automate the date, because, of course, it's different every day

 

Besides that, it looks like the file isn't zipped at all, because the filesize of the zipped file is 0

 

I now have this; see attachments.

 

 

Occasional Contributor PWS
Occasional Contributor
Posts: 15

Re: Export zip files using ods

Resulting in this;

Zip files ftp.PNG

Solution
‎01-21-2018 01:43 AM
Community Manager
Posts: 3,191

Re: Export zip files using ods

I think it's cleaner when you use filerefs -- that point to the files you need -- instead of file paths/names in your ODS archive directives.  I have an example in this article.

 

Complete code from my example:

 

/* Change this to suit your environment */
%let projectDir = c:\projects\sgf2013\filenamezip;

/* Clean slate! */
filename newfile "&projectDir./carstats.zip";
data _null_;
  if (fexist('newfile')) then 
  	rc = fdelete('newfile');
run;
filename newfile clear;

/* Create folder if it doesn't exist */
options dlcreatedir;
libname out "&projectDir./data";

/* Create some data */
filename newcsv "&projectDir./data/pct.csv";
proc means noprint data=sashelp.cars;
var msrp;
output out=out.pct median=p50 p95=p95 p99=p99;
run;
ods csv file=newcsv;
proc print data=out.pct;
format _all_; /* clear the formats */
run;
ods csv close;

/* Create an informative document about this package */
filename rm "&projectDir./readme.rtf";
ods rtf(readme) 
  file="&projectDir./readme.rtf" style=Printer;
ods rtf(readme) 
  text="These are some instructions for what to do next";
proc datasets lib=out nolist;
contents data=pct;
quit;
ods rtf(readme) close;

/* Creating a ZIP file with ODS PACKAGE */
ods package(newzip) open nopf;
ods package(newzip) add file=newcsv path="data/";
ods package(newzip) add file=rm;
ods package(newzip) publish archive 
  properties(
   archive_name="carstats.zip" 
   archive_path="&projectDir."
  );
ods package(newzip) close;
Occasional Contributor PWS
Occasional Contributor
Posts: 15

Re: Export zip files using ods

[ Edited ]
Posted in reply to KurtBremser

I still have some problems deleting the original csv after it is zipped.

 

For debugging purpose I've written the whole directory. 

 

 

 

Zipping the file works fine, but now I want to delete the original csv. I do not get an error message, but the csv is still there.

Could someone help?

Thanks!

Super User
Posts: 8,590

Re: Export zip files using ods

DO NOT POST LOGS IN PICTURES!

Use the {i} icon and copy/paste the text.

 

Add a

put rc=;

in the data _null_ step to show the return code in the log.

You are using just the filename in the fdelete() call, not the complete path, which is needed.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor PWS
Occasional Contributor
Posts: 15

Re: Export zip files using ods

Posted in reply to KurtBremser

This is the log;

161       +ods package(archived) open nopf;
162       +ods package(archived) add file="/srv/sas/data/sasexports/selligent/selligent_wait/snip_hkgzk_vie_&_file_prefix..csv";
163       +ods package(archived) publish archive
164       +properties(
165       +archive_name="snip_hkgzk_vie_&_file_prefix..zip"
166       +archive_path='/srv/sas/data/sasexports/selligent/selligent_wait/'
167       +);
NOTE: Writing zip package /srv/sas/data/sasexports/selligent/selligent_wait/snip_hkgzk_vie_20180129.zip.
168       +ods package(archived) close;
169       +data _null_;
170       +rc = fdelete ("/srv/sas/data/sasexports/selligent/selligent_wait/snip_hkgzk_vie_&_file_prefix..csv");
171       +put rc=;
172       +run;

NOTE: In a call to the FDELETE routine, the fileref /srv/sas/data/sasexports/selligent/selligent_wait/snip_hkgzk_vie_20180129.csv exceeds 8 characters, and will be truncated.
rc=20004
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
Super User
Posts: 8,590

Re: Export zip files using ods

The fdelete() function expects a file reference, not a physical filename as argument.

I used the example from the documentation as blueprint:

data _null_;
fname = 'tempfile';
rc = filename(fname,"/srv/sas/data/sasexports/selligent/selligent_wait/snip_hkgzk_vie_&_file_prefix..csv");
if rc = 0 and fexist(fname) then rc = fdelete(fname);
put rc=;
rc = filename(fname);
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor PWS
Occasional Contributor
Posts: 15

Re: Export zip files using ods

Posted in reply to KurtBremser
Thanks a lot!!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 14 replies
  • 254 views
  • 4 likes
  • 3 in conversation