Extracting sas code from sas log

Super Contributor
Posts: 371

Extracting sas code from sas log

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

Grand Advisor
Posts: 10,210

Re: Extracting sas code from sas log

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.

Posts: 65

Re: Extracting sas code from sas log

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 " ";


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

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


Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation