BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
InspectahDex
Obsidian | Level 7

I have this code:

 

proc export data=finaldata
outfile="&ExportLoc./finaldata_%sysfunc(today(),yymmddN.)%sysfunc(hour(%sysfunc(time())),z2.)%sysfunc(minute(%sysfunc(time())),z2.)%sysfunc(second(%sysfunc(time())),z2.)_123.txt"
dbms=dlm replace;
delimiter='|';
run;

 

I want to take what is created from there to export out into a zipped file. I have the below code but the log says it can't find the physical file because it doesn't exist. What am I missing?

 

 

 

ods package(newzip) open nopf;
ods package(newzip) add file="&ExportLoc./finaldata_%sysfunc(today(),yymmddN.)%sysfunc(hour(%sysfunc(time())),z2.)%sysfunc(minute(%sysfunc(time())),z2.)%sysfunc(second(%sysfunc(time())),z2.)_123.txt";
ods package(newzip) publish archive
properties(
archive_name="finaldata_%sysfunc(today(),yymmddN.)%sysfunc(hour(%sysfunc(time())),z2.)%sysfunc(minute(%sysfunc(time())),z2.)%sysfunc(second(%sysfunc(time())),z2.)_123.zip"
archive_path="&ExportLoc."
);
ods package(newzip) close;

 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Hi,

 

How about by using filename ZIP? By the way I suggest you to use proc format to have "easier" file naming:

/* Assuming we have this: */
%let ExportLoc = %sysfunc(pathname(work));

data finaldata;
  set sashelp.class;
run;


/* to make naming textfie easier! */
proc format; 
  picture  myDatetime (default=14)
    other='%Y%0m%0d%0H%0M%0S' (datatype=datetime)
  ;
run;


/* reference to ZIP device */
filename f ZIP "&ExportLoc./newzip.zip" member = "finaldata_%sysfunc(datetime(), myDatetime.)_123.txt";
proc export data=finaldata
    outfile=f
    dbms=dlm 
    replace;
  delimiter='|';
run;

 

Bart

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

5 REPLIES 5
Reeza
Super User

Post the full log will help. But since you're using time and date, my guess is that the time doesn't match exactly anymore. If you run it a minute later your file will have a different name so you cannot use dynamic time references when you need to do something list. I would suggest creating the filename once and then using it through the process.


%let file_export = &ExportLoc./finaldata_%sysfunc(today(),yymmddN.)%sysfunc(hour(%sysfunc(time())),z2.)%sysfunc(minute(%sysfunc(time())),z2.)%sysfunc(second(%sysfunc(time())),z2.)_123;


proc export data=finaldata
outfile= "&file_export..txt"
dbms=dlm replace;
delimiter='|';
run;

ods package(newzip) open nopf;
ods package(newzip) add file="&file_export..txt";
ods package(newzip) publish archive
properties(
archive_name="&file_export..zip"
archive_path="&ExportLoc."
);
ods package(newzip) close;

@InspectahDex wrote:

I have this code:

 

proc export data=finaldata
outfile="&ExportLoc./finaldata_%sysfunc(today(),yymmddN.)%sysfunc(hour(%sysfunc(time())),z2.)%sysfunc(minute(%sysfunc(time())),z2.)%sysfunc(second(%sysfunc(time())),z2.)_123.txt"
dbms=dlm replace;
delimiter='|';
run;

 

I want to take what is created from there to export out into a zipped file. I have the below code but the log says it can't find the physical file because it doesn't exist. What am I missing?

 

 

 

ods package(newzip) open nopf;
ods package(newzip) add file="&ExportLoc./finaldata_%sysfunc(today(),yymmddN.)%sysfunc(hour(%sysfunc(time())),z2.)%sysfunc(minute(%sysfunc(time())),z2.)%sysfunc(second(%sysfunc(time())),z2.)_123.txt";
ods package(newzip) publish archive
properties(
archive_name="finaldata_%sysfunc(today(),yymmddN.)%sysfunc(hour(%sysfunc(time())),z2.)%sysfunc(minute(%sysfunc(time())),z2.)%sysfunc(second(%sysfunc(time())),z2.)_123.zip"
archive_path="&ExportLoc."
);
ods package(newzip) close;

 




InspectahDex
Obsidian | Level 7

Because the code will be part of an automated process, I need to do this all in one step without having to run the entire program twice. 

Reeza
Super User
You should not have to run my solution twice.

The issue with your solution is a matter of timing. If you run it at 11:59:58 (11:59AM) the text file has the 59 as minutes. If the next step is run at 12:00:01 (12:00PM) you'll have 00 as the minutes so that file doesn't exist. So you need to create the filename once and fix it, not calculate it dynamically throughout your code.

yabwon
Onyx | Level 15

Hi,

 

How about by using filename ZIP? By the way I suggest you to use proc format to have "easier" file naming:

/* Assuming we have this: */
%let ExportLoc = %sysfunc(pathname(work));

data finaldata;
  set sashelp.class;
run;


/* to make naming textfie easier! */
proc format; 
  picture  myDatetime (default=14)
    other='%Y%0m%0d%0H%0M%0S' (datatype=datetime)
  ;
run;


/* reference to ZIP device */
filename f ZIP "&ExportLoc./newzip.zip" member = "finaldata_%sysfunc(datetime(), myDatetime.)_123.txt";
proc export data=finaldata
    outfile=f
    dbms=dlm 
    replace;
  delimiter='|';
run;

 

Bart

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



InspectahDex
Obsidian | Level 7
Thank you! That worked and solved my problem! Also thanks for the proc format tip!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1055 views
  • 5 likes
  • 3 in conversation