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

Wanted to do a number of runs, one per SAS session.  Quit SAS.  Back in.  Run another program, or same program with different parameters.

 

Wanted a different "print output" file each time.  (Not the same as log.  And not same as .html)

 

Accumulate a set of these print output files, then look them over.

 

SAS seems to have no way of directly allowing this.  (Yes, for log files...  via %v -- but strange, at that.)

 

So, SAS documentation says, go ahead and use Windows Environment Variables for or within SAS file names.

 

Hmm.  Sounded like a solution.

 

%random% is an internal, built-in WEV.  Tried it in SAS.

 

SAS System Option File entry:

 

-ALTPRINT "c:\0_SAS_1\output_%random%.txt"

 

and

 

-ALTPRINT "c:\0_SAS_1\output_random.txt"

 

Along with other WEV attempts.

 

Nothing worked.

 

So, again, the idea was to create output files, one after another, per SAS 'session', named uniquely.  (Current time would be perfect, by the way, but 'random' would work.)

 

Any suggestions greatly appreciated.

 

Nicholas Kormanik

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@NKormanik wrote:

Me thinks we're getting close....

"a code snippet that generates a 10 random digit macro variable"

 

And then use that code snippet in the SAS file name.

 

proc printto print='output-code-snippet'
new;
run;

 

Next step is?

 

 


I might try something like:

data _null_;
 call symputx('d',put(today(),yymmddn8.));
 x= int(rand('uniform')*10000000000);
 call symputx('rd',x);
 call symputx('t',cats(put(hour(time()),z2.),put(minute(time()),z2.)));
run;

proc printto print="c:\folder\subfolder\basename_&d._&t._&rd..txt" new; 
run;

put the data step in a macro and call it before each proc printo. the Time component may not be much actual help in your need except for sort bits.

 

Or for extra added difficulty in getting all the functions aligned:

%macro myfname(base=);
%let rd= %sysfunc( int(%sysfunc(rand(uniform))*10000000000) );
%let d = %sysfunc(putn(%sysfunc(today()),yymmddn8.));
%let t = %sysfunc(cats(%sysfunc(putn(%sysfunc(hour(%sysfunc(time()))),z2.)),%sysfunc(putn(%sysfunc(minute(%sysfunc(time()))),z2.))));
&base._&d._&t._&rd
%mend;

proc printto print="%myfname(base=c:\folder\).txt" new; run;
proc print data=sashelp.class;
run;
proc printto;run;

You could provide a path, make sure to have the closing slash as a default in the macro definition by providing the path as a default value for base:

%macro myfname(base=c:\folder\subfolder\);

Which would make the call to proc printo look like:

proc printto print="%myfname.txt" new;

Note that the double quotes around the macro call are required. Single quotes won't resolve. I left the option of the file extension, the .txt in the call because you may want to use Proc Printo log= at some point and wouldn't actually want to have the same extension accidentally. If you have enough discipline that would never happen you could make the extension part of the macro value by using

&base._&d._&t._&rd..txt

The two dots are needed because the first is used by the macro processor to indicate concatenation and would "eat" the first period and TXT would not be an extension.

View solution in original post

6 REPLIES 6
LinusH
Tourmaline | Level 20

If you fine withy using an initial PROC PRINTO in your program(s) or autoxec, you can use &SYSDATE and &SYSTIME automatic macro variables. Only problem with this is that &SYSTIME is on minute level, so you can't execute your programs too frequently.

Data never sleeps
JosvanderVelden
SAS Super FREQ
Have you seen the examples of using proc printto?
https://documentation.sas.com/?docsetId=proc&docsetVersion=9.4&docsetTarget=n1oardywxmmut7n1pol0zkj5...
You have complete control of the filename here so you could use one with a datetime to be sure the names are unique.
NKormanik
Barite | Level 11
proc printto print='output-file'
new;
run;

Perhaps amounts to same problem mentioned initially -- if we could just get that 'output-file' name to be unique.

 

 

ballardw
Super User

I am not sure that you need to stop and restart SAS to accomplish what you want.

 

if you want something that does this "automagically" perhaps placing information that could be used as a filename in a control data set and then using call execute to execute each program variation with a Proc Printto preceding the actual "program" executed. Or an ODS destination sandwich around the programs to direct output to a separate file.

 

Since you mention "different parameters" another question becomes how are you using "different parameters"?

 

Note that @LinusH's suggestion of &Sysdate and &Systime could be extended by having a code snippet that generates a 10 random digit macro variable and append that to the date and time construct in the file name. Not a 100% guarantee of no duplicate but the likelihood is vanishingly small.

NKormanik
Barite | Level 11

Me thinks we're getting close....

"a code snippet that generates a 10 random digit macro variable"

 

And then use that code snippet in the SAS file name.

 

proc printto print='output-code-snippet'
new;
run;

 

Next step is?

 

 

ballardw
Super User

@NKormanik wrote:

Me thinks we're getting close....

"a code snippet that generates a 10 random digit macro variable"

 

And then use that code snippet in the SAS file name.

 

proc printto print='output-code-snippet'
new;
run;

 

Next step is?

 

 


I might try something like:

data _null_;
 call symputx('d',put(today(),yymmddn8.));
 x= int(rand('uniform')*10000000000);
 call symputx('rd',x);
 call symputx('t',cats(put(hour(time()),z2.),put(minute(time()),z2.)));
run;

proc printto print="c:\folder\subfolder\basename_&d._&t._&rd..txt" new; 
run;

put the data step in a macro and call it before each proc printo. the Time component may not be much actual help in your need except for sort bits.

 

Or for extra added difficulty in getting all the functions aligned:

%macro myfname(base=);
%let rd= %sysfunc( int(%sysfunc(rand(uniform))*10000000000) );
%let d = %sysfunc(putn(%sysfunc(today()),yymmddn8.));
%let t = %sysfunc(cats(%sysfunc(putn(%sysfunc(hour(%sysfunc(time()))),z2.)),%sysfunc(putn(%sysfunc(minute(%sysfunc(time()))),z2.))));
&base._&d._&t._&rd
%mend;

proc printto print="%myfname(base=c:\folder\).txt" new; run;
proc print data=sashelp.class;
run;
proc printto;run;

You could provide a path, make sure to have the closing slash as a default in the macro definition by providing the path as a default value for base:

%macro myfname(base=c:\folder\subfolder\);

Which would make the call to proc printo look like:

proc printto print="%myfname.txt" new;

Note that the double quotes around the macro call are required. Single quotes won't resolve. I left the option of the file extension, the .txt in the call because you may want to use Proc Printo log= at some point and wouldn't actually want to have the same extension accidentally. If you have enough discipline that would never happen you could make the extension part of the macro value by using

&base._&d._&t._&rd..txt

The two dots are needed because the first is used by the macro processor to indicate concatenation and would "eat" the first period and TXT would not be an extension.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 6 replies
  • 1701 views
  • 4 likes
  • 4 in conversation