BookmarkSubscribeRSS Feed
LineMoon
Lapis Lazuli | Level 10

I have a sas log and I want to extract from it a sas code

8 REPLIES 8
ballardw
Super User

If you have just run a program in base SAS you can press the F4 key and code will be "recalled" into the program editor.

Otherwise you can highlight lines in the log and copy/paste into the editor, which will likely require some editing to remove notes.

Or save the log to a text file and edit with any editor.

MadhuKorni
Quartz | Level 8

If you want to extract the SAS Code in a log file and store it in a dataset then the below code will help you

In logs the SAS code will be preceded with line numbers. So first pick the lines which are starting with numbers. Here I stored the data in the variable Code1.

If you need only the SAS Code with out the line numbers then use substr to extract only the code leaving the line numbers. Here I stored the same in the variable Code.

There will be blank lines in the code and the blank spaces will be stored in the dataset as observations.We can exclude these observations by using the if condition.

data SASCode;

infile "path\logfilename.txt";

input dat $1000.;

dat = _infile_;

if substr(left(_infile_),1,1) in ('1','2','3','4','5','6','7','8','9','0') then code1 = _infile_;

code = substr(code1,index(code1," "));

keep code;

if code ne " ";

run;

If you want to export the code into any flat file then

proc export data = SASCode outfile = "xxx" dbms= xxx  replace;

run;

helge-h
Calcite | Level 5

If you're looking at a workspace server log, each line may be preceded by a date/time stamp, log level info and a username. Assuming a linux environment, you can use the following command line syntax.

 

Be sure to set the correct log file name and also replace the username (john_doe) in 2 places in the statement.
 
This will generate a file in the same location as the log, with the same name as the log, with a ".sas.recover" extension. Note that this recovered code should be used with caution, and only as reference, because parts might not be included, macros may be expanded, etc based on options that are set.
 
 
logname=SASApp_WorkspaceServer_2019-10-07_hostA_342040.log
grep "john_doe \- [0-9]" ${logname} | grep -v 'The SAS System' |awk -F'john_doe - ' '{print $2}'| sed -E 's/([0-9]+) //' > `dirname ${logname}`/`basename ${logname} .log`.sas.recover
 
This is written for Workspace server logs specifically. To run this on Batch or Connect logs, adjust the regex in the grep statement accordingly. The sed statement can remain as is.
LinusH
Tourmaline | Level 20

What is the real life scenario here, why can't you get the code the regular way? Is it lost?

If the code is very extensive, you should have saved it.

If it's simple code, you can probably recreate it.

Data never sleeps
helge-h
Calcite | Level 5

The real life scenario is: The code is very extensive. An earlier version was saved but significant changes were lost. Deadlines don't allow for recreation of the lost changes and this method to extract the code from the log facilitated meeting the deadline.

PASASMAN
Calcite | Level 5
Real life scenario is I don't want to write 100 lines of data input. When usintg the Import Wizard, SAS generates the code along with formats and informats.
Tom
Super User Tom
Super User

@PASASMAN wrote:
Real life scenario is I don't want to write 100 lines of data input. When usintg the Import Wizard, SAS generates the code along with formats and informats.

If you are using PROC IMPORT to GUESS how to read a delimited file you can also use some other method to perform the guessing and code generation.  Then it will be easier to have the generated code and/or metadata.

 

Try running this macro on your file(s).

https://github.com/sasutils/macros/blob/master/csv2ds.sas

It will leave you with both a text file with the code and a dataset with the metadata.

Plus it will run faster and allow you to override the guessed metadata.

PASASMAN
Calcite | Level 5

To get rid of line numbers:

 

Open the log.

Export as a RTF

Open in Word

Copy All

Paste into Excel

Use Text-to-Columns 

Use Fixed Width

Position line break between line numbers and SAS Code

Voila!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 4410 views
  • 1 like
  • 7 in conversation