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
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;
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.
Hi @Kurt_Bremser,
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...
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.
Ohw, right. Of course. I just need to define the path again. Wow
Thanks
@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.
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."
);
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.
Resulting in this;
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;
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!
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.
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
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.