Top 10 - Best SAS Tutorial Documents - Examples and Tips - A Statistician's List

Daily sas code snippets are posted in the site: 

Keywords: SAS macros, SAS SQL, SAS Table, SAS Data Step, ODS, Data Mining Automation, SAS Tips, SAS Examples, SAS Reference for PROC Statements, Analytics Automation

Section: SAS SQL:

SAS SQL has some special advantages; before you know that let us understand how to do the same thing in standard SAS codes and SAS SQL codes

A nice SAS SQL basics notes with examples

Top 10 SAS SQL examples:

A great simple SAS SQL teaching document

A nice article about INTO Clause

Undocumented and Hard to find SQL Features

Part of this new book - Proc SQL: Beyond the Basics Using SAS - By Kirk Paul Lafler where a great collection of codes (To make sense you may benefit well buying the book - look at SAS site for the book)

PROC SQL - Minimal Example Codes  - /*A good collection of PROC SQL Examples from the above references*/

Section: SAS MACROS:

A great simple start to macro writing

Sugi 28:  Nine steps to Start Writing Macros

A place of general collection of SAS starting points, SAS data sets, Teaching kits with examples from different subjects is:

A classic macro example from "Nine Steps..." from above:

%macro vacation(dsn=expenses,varlist=_all_); 


proc sql noprint;

Select        mean(RoomRate),



into :average,:date,:mon

from &dsn;


%if &mon=6 or &mon =7 or &mon =8

   %then %do;

proc print data=&dsn;

title "Lowest Priced Hotels in the

%upcase(&dsn) Data Set";

footnote "On &date";

var &varlist;

where RoomRate<=&average;




   %else %do;

proc print data=&dsn;

title "All Room Information in the

%upcase(&dsn) Data Set";

footnote "On &date";

var &varlist;






options symbolgen mprint mlogic;


%vacation( )



Managing storage and recall of macros



Some not so commonly known sas codes

  • date conversion (from character yymmdd to sas date example

       ch_auto_open_date_1_f=input(ch_auto_open_date_1,yymmn6.); /* note the n in the informat */


  • date conversion (from character yyyy-mm-dd to sas date


  • using do loop in a macro to read text based file names and its contents as data:



There are three ways to store macro programs for future use: 

1) Store the source code in an external file and use this statement to pull it into a SAS program:


2) Use the Autocall Facility to search predefined source libraries for macro definitions. These libraries can consist of external files or SAS catalogs.

 3) Store the compiled macros. This does NOT save the SOURCE code! Always maintain the source code separately. To store the compiled macro :

 libname dlib ‘library-path’;

options mstored sasmstore=dlib;

%macro mname(parameters) / store




To access this stored macro :

libname dlib ‘library-path’;

options mstored sasmstore=dlib;



Exporting and Importing Data




Importing data from Excel:   What if the data is access?



            DATAFILE= "E:\SPSS\Howell.xls"

            DBMS=EXCEL2000 REPLACE;





















Section: Automating SAS outputs for summary measures in Data Mining:


Knowing output dataset names in various PROCS in SAS STAT:


1.     Use the command outside of proc statements; 


(before the specific proc statement) ODS TRACE ON;

(after that proc statement              ) ODS TRACE OFF:


2.     Run the program and look at the log and find dataset name of output files from any Procs; Check the sas output datasets to clearly understand name of the file and the contents;


3.     Now take out those ODS Trace statments; introduce the following before the PROC statment:


4.     ODS listing Name (file name from ODS trace)=New name (it could have a specific path); /* storing it in a particular path and will not be lost after the SAS run)


5.     ODS listing close;


6.     Now do the proc contents to know the column names and attributes.


7.     In the next run use the sas dataset name as a dataset to access, to pull out your elements selectively, and to format the outputs to your needs;


This is extremely useful for automation and for large scale data mining problems.


For some more details look into




Section: The SAS reference:


The general latest SAS PROC document for reference


This document also gives ODS dataset names for every Proc STAT step; If you use this you do not need to use the method given in the above section.



Finally my own sweet little codes library: I will be posting all the niceties learned in the last 15 years of experience (obviously large application specific codes are not posted here, but quick sample codes only, but enough number for novices to use and regular users like me to recall - );


Also, to bring together all my and my colleagues experience in using SAS and Statistical Methods, you may visit my Blog: Statistical Methods and Applications; your comments are most welcome, and your contributions are most, most welcome.



Top 10 questions a statistician should have answers at the fingers tip.


1. Application of set theory

2. Principles of combination and permutation

3. Application of basic probability problems

4. Application of Bayesian Theorem

5. Application of rate of growths and decay problems

6. Application of three different types of averages, dispersion measures

7. Application of top 5 probability distributions

8. Application of linear model principles and top 10 formulae for various principles in linear models

9. Challenges and alternative methods for OLS

10. Sampling problems and design principles