I have a sas log and I want to extract from it a sas code
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.
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;
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.
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.
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 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.
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!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.