fly36
fly36

Reputation: 83

how to display a sentence in the SAS result output window

I wrote a macro using sas. I saved the log in an external txt file, using the following code.

proc printto log="path\log.txt"; run;

%macro loop(num);

  for i = 1 : #
    data a;
    set a;
    display &i;
    run;       
%mend;

% loop(100)

proc printto;  run;

My program works like a loop. for i = 1 : 100, do something then i = i +1. I want to display i at the end of each loop, so I know where the program is. I do not want it displayed in log /(the log txt file) because it will be mixed with the log and hard to read. so I can not use put "&i" function.

Please let me know if there is any better place to display i (maybe the result output window?) If so, how to do it please.

Upvotes: 3

Views: 9687

Answers (2)

Quentin
Quentin

Reputation: 6378

Since you are using PC SAS, in an interactive session, you can use data step PUT statements to write to the log, or the output window. In PC sas the log and the output window but update in real time (different than in EG).

%macro testmsg
  (nloop=5
  ,file=log /* log | print */
   )
;
  %local i ;
  %do i=1 %to &nloop ;
    data _null_ ;
      file &file ;
      x=sleep(3) ;
      put "&i" ;
    run ;
  %end ;
%mend testmsg ;

%testmsg(file=log)
%testmsg(file=print)

Upvotes: 2

Joe
Joe

Reputation: 63424

The log is the correct place to put this. Many tricks exist to help you make this visible.

  • The way the log highlights things in different colors is driven by the keywords NOTE:, WARNING:, and ERROR:. You can use those to your advantage if you don't mind slightly misleading log notes.
  • Turn off excess log messages, or redirect them to another location, during the bulk of the processing. This is very common to be done inside a macro.
  • Use things like this:

Code:

%put ********************************************************;
%put * ITERATION &i BEGINNING                               *;
%put ********************************************************;

All that said, it's certainly possible to do what you're asking; the question is, where is it useful for you to see it.

Two suggestions.

First: ods text will output to the results window, but it requires you to have something else printing (it won't just show up on its own). It's not dissimilar to using title, really.

%macro test;
  %do i=1 %to 10;
    ods text="Running &i. Iteration";
    proc print data=sashelp.class;
    run;
  %end;
%mend test;
%test;

Similarly, you can use ods proclabel to get the Results Explorer window to show the iteration number.

%macro test;
  %do i=1 %to 10;
    ods proclabel="&i. Iteration";
    proc print data=sashelp.class;
    run;
  %end;
%mend test;
%test;

I like the latter more; it makes it easier to see what's going on.

Both, however, have a major limitation: they're not going to let you see what's going on while the macro is executing, generally. You'll need something for this purpose I suspect that will update a file outside of the SAS environment; anything other than the log will often be sort of 'frozen' while the IDE thinks about doing SAS things.

If you're aiming to use this as sort of a 'how far along are we' monitor, you have several options.

First, a SAS/AF window could possibly do the trick - see Aster, NESUG '92 for an example.

Second, write to a file.

filename monitor "c:\temp\monitor.dat";
%macro test;
  %do i=1 %to 100;
    data _null_;
      file monitor mod;
      put "Iteration &i started";
    run;
    *... do stuff ...;
  %end;
%mend test;
%test;

This is basically just making your own secondary log, which seems like perhaps the best compromise here. It does also give you the same information in the log if you have SYMBOLGEN on.

Upvotes: 3

Related Questions