Desktop productivity for business analysts and programmers

Export SAS log to external files

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Export SAS log to external files

Hi, All.

My working environment transfered from SAS Base to SAS EG in last week. In my working environment, we need to export LOG and analysis log and get a overall summary. In SAS Base environment, I use following statement to export log file.

DM "log;file filename;"

But in EG, the DM command is not supported.

Does anyonce know any statement or functions in EG equal to above statement?

Proc printto statement not work in my situation as the proc printto does not export log already exists .

Thank you.

 


Accepted Solutions
Solution
‎07-29-2016 09:50 AM
SAS Super FREQ
Posts: 271

Re: Export SAS log to external files

[ Edited ]

Another way you could accomplish is with EG's automation interface.  For example, add the following to the EGScript1.vbs file that gets created when you schedule an EG project:

 

    '-----
    ' export the log for each program
    '-----
    Dim code
    For Each code in prjObject.CodeCollection
        'MsgBox code.Log.Text
        code.Log.SaveAs "c:\temp\" & code.Name & ".log"
    Next

 

Here is an example of the full VBS file (I commented out the running and saving of the project.  Up to you to decide what you want the script to do.):

Option Explicit
Dim app

Call dowork

'shut down the app
If not (app Is Nothing) Then
    app.Quit
    Set app = Nothing
End If


Sub dowork()
    On Error Resume Next
    '----
    ' Start up Enterprise Guide using the project name
    '----
    Dim prjName
    Dim prjObject

    prjName = "C:\Users\cassmi\Desktop\ExportAllLogs.egp"    'Project Name
      
    Set app = CreateObject("SASEGObjectModel.Application.7.1")
    If Checkerror("CreateObject") = True Then
        Exit Sub
    End If
    
    '-----
    ' open the project
    '-----
    Set prjObject = app.Open(prjName,"")
    If Checkerror("app.Open") = True Then
        Exit Sub
    End If
    
        
    '-----
    ' run the project
    '-----
    'prjObject.run
    'If Checkerror("Project.run") = True Then
    '    Exit Sub
    'End If
    
	
    '-----
    ' export the log for each program
    '-----
    Dim code
    For Each code in prjObject.CodeCollection
        'MsgBox code.Log.Text
        code.Log.SaveAs "c:\temp\" & code.Name & ".log"
    Next
            
    '-----
    ' Save the new project
    '-----
    'prjObject.Save
    'If Checkerror("Project.Save") = True Then
    '    Exit Sub
    'End If
    
    '-----
    ' Close the project
    '-----
    prjObject.Close
    If Checkerror("Project.Close") = True Then
        Exit Sub
    End If
       
End Sub

Function Checkerror(fnName)
    Checkerror = False
    
    Dim strmsg
    Dim errNum
    
    If Err.Number <> 0 Then
        strmsg = "Error #" & Hex(Err.Number) & vbCrLf & "In Function " & fnName & vbCrLf & Err.Description
        'MsgBox strmsg  'Uncomment this line if you want to be notified via MessageBox of Errors in the script.
        Checkerror = True
    End If
         
End Function

View solution in original post


All Replies
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Export SAS log to external files

Sorry, why does:

proc printto log="c:\alogfile.txt";

run;

 

Not work?  Just need to specify the filename, and put that before code you need to run, per:

http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/viewer.htm#a001330273.htm

 

Occasional Contributor
Posts: 9

Re: Export SAS log to external files

The situation is that I can't just simply put the proc printto code before SAS code.

I need another way to put log which exsist before my code to external file.

Thanks.Man Embarassed

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Export SAS log to external files

Sorry, your process isn't clear.  You can't run code?  Do the files already exist then?  If so you would need some sort of OS scripting to copy them across.  What is the clear and concise process you are doing, indicate at what point you need to redirect the log (if indeed redirecting the log is what we are talking about).  Can't remember about EG setup, but SAS has autoexec.  You could also create a run_all program separate from what you have, e.g.:
%inc (file=);

  proc printto log="&file..txt";

  run;

  %inc "&file..sas";

  proc printto;

  run;

%mend inc;

%inc "c:\file1";

%inc "c:\file2";

Occasional Contributor
Posts: 9

Re: Export SAS log to external files

Thanks man. What I need is to redirect the log file after I run my program. but I can't add proc printto to statement before my program. The real picture is that I need to check my log after my run my program. This can be easily achieved in SAS BASE through DM command. But I don't find any sort of ways in EG.
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Export SAS log to external files

Ok, I thnk I see.  You run a program (can't run anything else other than that), then once that is run you want to take a copy of the log.  In which case you will need to know where the default log is stored, it should be a text file somewhere (unless EG keeps it in memory).  You can also re-direct the log in the autoexec - as EG starts up, to a file you want, then you can copy paste that file as you want and it will not interfere with your other program.  

Occasional Contributor
Posts: 9

Re: Export SAS log to external files

Yeah, exactly. You gave me some hints. I think I need to find where the temporary log file is, then I can use SAS program to analysis it. Thanks man.
SAS Super FREQ
Posts: 271

Re: Export SAS log to external files

EG stores the log files temporarily on disk in the Windows %temp% directory in a path with this format:

 

%temp%\SEG<uniqueID>\<uniqueID>\result.log

 

For example:

C:\Users\cassmi\AppData\Local\Temp\SEG65492\dd40c683fece4362b2d3e17463b91689\result.log

 

You could write a script to copy them from here if redirecting the log in autoexec or via proc printto will not meet your needs.

 

Note: EG's project log is also stored in the same path format and file name, so you'd probably want your script to differentiate somehow.

 

Casey

Trusted Advisor
Posts: 1,213

Re: Export SAS log to external files

Just wanted to say I had this same question/problem about trying to access the log programmitally.  Like you, in interactive PC SAS, I used DM commands to save the current log to a file and then post-process it. 

 

Helpful to know that the EG log file is stored in a temp file on the PC.  But my guess is this still wouldn't work for my use case.  That is, in interactive PC SAS (and batch SAS) I can submit all together:

*data step;
*proc step;
*blah blah blah;
%logPostProcess()

And %LogPostProcess will process the log from steps submitted before it.  In EG connected to a remote SAS session, I would assume the code has to complete executing before the log file gets back to the local PC.  And as I type I realize even if the log file came back to my local PC in real time, it wouldn't matter, because my %logPostProcess executing on the remote server couldn't read it.

 

 

I have ended up using the PROC PRINTTO approach when I want to do log scanning in EG.  It's a bid of a hassle, but works okay.  You can set up pre-code and post-code to run the PROC PRINTTO before and after every EG submission (or on EG conection to server).  So you don't necessarily need to revise the main code.  My process is like:

  1. PROC PRINTTO redirects log to a file.
  2. Main code runs.
  3. PROC PRINTTO redirects log back to log window.
  4. Post process log file.
  5. Read the log file and stream in back to log window (so I can see log in EG log window).

 

That said, I'm only using EG for development work.  It was important to me to keep an approach that works for EG and Interactive PC SAS and batch jobs and stored process jobs.

 

I think this would be easier to manage if it were possible to specify ALTLOG option during a SAS session.  So you could just turn ALTLOG on and off any time you want to get a file copy of the log.  If you agree, please vote up:

https://communities.sas.com/t5/SASware-Ballot-Ideas/Allow-ALTLOG-to-be-specified-on-OPTIONS-statemen...

 

--Q.

Occasional Contributor
Posts: 9

Re: Export SAS log to external files

Hi, Quentin. Thank you.
I think we met the same issue.
I appreciated the way you provided. I think it's a good solution.
But I am a little confused "You can set up pre-code and post-code to run the PROC PRINTTO before and after every EG submission" as i'm a newbee in SAS EG.

Could you please explain how to achieve this task?

Thank you.

 

Trusted Advisor
Posts: 1,213

Re: Export SAS log to external files

Hi,

 

Warning: I haven't played with this approach, but I think it should work. 

 

In recent versions of EG (I'm using 6.1) if you go to tools->Options->SAS Program there are options where you can specify

  1. Submit SAS Code when server is connected
  2. Insert custom SAS code before submitted code
  3. Insert custom SAS code after submitted code

 

You can think of Submit SAS Code when server is connected as similar to an autoexec.  When EG starts, it connects to a server, and that code will run automatically.    In general, when you submit code in EG, EG adds a bunch of wrapper code to communicate with the server etc.  The insert custom SAS code before/after submitted code allows you to specify additional SAS code that should be added to that wrapper.  That code would be executed every time you submit code in EG.

 

So my thought is set Submit SAS Code when server is connected to be:

filename mylog "%sysfunc(pathname(work))/MyLog.log";

Set Insert custom SAS code before submitted code to be:

 

proc printto log=mylog new;
run;

Set Insert custom SAS code after submitted code to be:

 

proc printto log=log;
run;

data _null_;
  infile mylog;
  input;
  *log scanning etc;

  putlog _infile_;  *send contents of mylog back to log window;
run;

 

When you have a setup like that, any time you submit code from EG it would:

  1. Start a proc printto block.
  2. Execute the submitted code
  3. Close the proc printto block, scan the log, and write the log file to the log window.

 

So instead of taking the PC SAS approach of using DM commands to copy the content of log window to a file when you want to log scan, you take the opposite approach of always writing the log to a file.    I think something like that could work. 

Occasional Contributor
Posts: 9

Re: Export SAS log to external files

Thank you so much Quentin.

The solution you provided is very creative and useful.

I think it will work and I will post this in this post once I have finished my experiment.

 

Occasional Contributor
Posts: 5

Re: Export SAS log to external files

Click on the Log Tab(Next to Program) , Under Log you see the options export , Sent to ...............Click on export ---> Export as ....Then you save the file to desired destination

Occasional Contributor
Posts: 9

Re: Export SAS log to external files

Yeah, this is actually a way. But in this way I have to click this button every time, It is time-consuming.
Solution
‎07-29-2016 09:50 AM
SAS Super FREQ
Posts: 271

Re: Export SAS log to external files

[ Edited ]

Another way you could accomplish is with EG's automation interface.  For example, add the following to the EGScript1.vbs file that gets created when you schedule an EG project:

 

    '-----
    ' export the log for each program
    '-----
    Dim code
    For Each code in prjObject.CodeCollection
        'MsgBox code.Log.Text
        code.Log.SaveAs "c:\temp\" & code.Name & ".log"
    Next

 

Here is an example of the full VBS file (I commented out the running and saving of the project.  Up to you to decide what you want the script to do.):

Option Explicit
Dim app

Call dowork

'shut down the app
If not (app Is Nothing) Then
    app.Quit
    Set app = Nothing
End If


Sub dowork()
    On Error Resume Next
    '----
    ' Start up Enterprise Guide using the project name
    '----
    Dim prjName
    Dim prjObject

    prjName = "C:\Users\cassmi\Desktop\ExportAllLogs.egp"    'Project Name
      
    Set app = CreateObject("SASEGObjectModel.Application.7.1")
    If Checkerror("CreateObject") = True Then
        Exit Sub
    End If
    
    '-----
    ' open the project
    '-----
    Set prjObject = app.Open(prjName,"")
    If Checkerror("app.Open") = True Then
        Exit Sub
    End If
    
        
    '-----
    ' run the project
    '-----
    'prjObject.run
    'If Checkerror("Project.run") = True Then
    '    Exit Sub
    'End If
    
	
    '-----
    ' export the log for each program
    '-----
    Dim code
    For Each code in prjObject.CodeCollection
        'MsgBox code.Log.Text
        code.Log.SaveAs "c:\temp\" & code.Name & ".log"
    Next
            
    '-----
    ' Save the new project
    '-----
    'prjObject.Save
    'If Checkerror("Project.Save") = True Then
    '    Exit Sub
    'End If
    
    '-----
    ' Close the project
    '-----
    prjObject.Close
    If Checkerror("Project.Close") = True Then
        Exit Sub
    End If
       
End Sub

Function Checkerror(fnName)
    Checkerror = False
    
    Dim strmsg
    Dim errNum
    
    If Err.Number <> 0 Then
        strmsg = "Error #" & Hex(Err.Number) & vbCrLf & "In Function " & fnName & vbCrLf & Err.Description
        'MsgBox strmsg  'Uncomment this line if you want to be notified via MessageBox of Errors in the script.
        Checkerror = True
    End If
         
End Function
☑ This topic is SOLVED.

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

Discussion stats
  • 17 replies
  • 2254 views
  • 3 likes
  • 6 in conversation