How to access SAS sample programs

Have you ever wanted to run a sample program from the SAS documentation or wanted to use a data set that appears in the SAS documentation? You can: all programs and data sets in the documentation are distributed with SAS, you just have to know where to look!

Sample data in the SASHELP library

The data that are used in the SAS documentation are obtained in one of two ways: from a data set in the SASHELP library, or from a DATA step.

Many documentation examples use data in the SASHELP library, such as the Class, Iris, and Cars data sets. To use these data sets, specify the two-level name (libref.DataSetName) on the DATA= option of a procedure. For example, to display descriptive statistics about numerical variables in the Iris data, use the following call to the MEANS procedure:

title "Descriptive statistics for SASHELP.Iris";
proc means data=sashelp.iris;
run;

In other examples, the data is presented as a DATA step. However, sometimes the DATA step is so long that all of the data do not appear in the documentation. For example, the documentation of the QUANTREG procedure contains the following truncated DATA step:

data ozone;
  days = _n_;
  input ozone @@;
datalines;
0.0060 0.0060 0.0320 0.0320 0.0320 0.0150 0.0150 0.0150 0.0200 0.0200
0.0160 0.0070 0.0270 0.0160 0.0150 0.0240 0.0220 0.0220 0.0220 0.0185
0.0150 0.0150 0.0110 0.0070 0.0070 0.0240 0.0380 0.0240 0.0265 0.0290
   ... more lines ...   
0.0220 0.0210 0.0210 0.0130 0.0130 0.0130 0.0330 0.0330 0.0330 0.0325
0.0320 0.0320 0.0320 0.0120 0.0200 0.0200 0.0200 0.0320 0.0320 0.0250
0.0180 0.0180 0.0270 0.0270 0.0290
;

The complete data is contained in the sample program for this example. The next sections show how to access sample programs.

Access sample programs from the Help menu

You can access SAS sample programs by using the Help menu inside the SAS Windowing Environment, which is the default GUI environment. The following image shows a way to access the SAS/STAT sample programs by using the SAS GUI:

Access sample programs from the installation directory

Michael A. Raithel, in a SAS Tip on sasCommunity.org, points out that sample programs exist for all products, and gives examples of directories in which you can find them. For example, on my desktop PC, which is running SAS 9.3, I can browse to

C:Program FilesSASHomeSASFoundation9.3ProductNamesample
in order to see the sample programs.

You need to replace ProductName with the name of a SAS product, such as stat or ets. But how can you find the path of the "root directory" for your SAS installation? You can run the following DATA step in order to find it:

data _NULL_;
   %put %sysget(sasroot);
run;
     C:\Program Files\SASHome\SASFoundation\9.3

This is the value of sasroot that Michael refers to in his article. As Michael points out, it can be useful to browse programs in the sample directory, especially for products that you are trying to learn. The names of the files can be somewhat cryptic, so you should to use a search tool to find the correct file. For example, if you search the SAS/STAT sample directory for the term "data ozone," you will discover that the DATA step for the PROC QUANTREG example is in the file qregex4.sas. Therefore, the complete path for the PROC QUANTREG example on my PC is

     C:\Program Files\SASHome\SASFoundation\9.3\stat\sample\qregex4.sas

I'd like to be able to end this post by saying "the examples for PROC IML are great and help you learn how to program in the SAS/IML language." Unfortunately, many of the SAS/IML examples (which are based on the documentation) are old and do not contain many comments or explanatory text. But don't dispair. For Getting Started examples, read The DO Loop every Monday.

tags: Getting Started, SAS Programming, Tips and Techniques

7 Comments

  1. Posted February 6, 2012 at 11:14 am | Permalink

    Rick:

    Great topic! When I was getting started I thought having some sample data was the most useful for some of my crazier ideas. Here's how to get the UFO sightings data - which is fun to play with: National UFO Reporting Center data .

    Did you know that more people in Charlotte, NC see UFO's than Raleigh-Durham area? I'm just saying ...

    Tricia

  2. Roger Chenoweth
    Posted February 10, 2012 at 12:55 pm | Permalink

    I would just like to add that there are also lots of great samples on http://support.sas.com. Analytic samples (including lots of great IML samples) can be found on the Statistics and Operations Research Examples page and general samples (including even more analytic samples) can be searched on the Samples and SAS Notes page.

  3. Posted February 15, 2012 at 10:13 am | Permalink

    Thanks Rick for your tips. I will forward them to my clients.

    Sunil

  4. Anonymous
    Posted February 20, 2012 at 7:22 am | Permalink

    Hi

    You do not need a data step to run the macro statement:
    %put %sysget(sasroot);

    If you want to do the same thing using data step code, it makes more sence to do it like this:
    data _null_;
    sasroot = sysget('sasroot');
    put sasroot;
    run;

    Regards

  5. Brian
    Posted March 2, 2012 at 10:23 am | Permalink

    Hello Rick,

    I am fairly new to SAS programming (I have passed the Base SAS Certification Exam) but am truly limited to Data step type manipulations at this point. I am having a difficult time finding references online to assist (in a very methodical way) me in programmatically creating transition matrices (for example in a Credit Risk Cohort approach framework.) Charlie Huang (http://www.sasanalysis.com/2010/12/scorecard-to-create-transition-matrices.html) has provided a wonderful example of code for the actual transition matrix generation, but I am having trouble preparing the data to be used in his code. Any chance you could blog a bit about this exercise? Thanks a million!

    • Posted March 2, 2012 at 10:43 am | Permalink

      Do you know about the SAS Discussion Forums at http://communities.sas.com/ ? There's one for SAS/IML, one for DATA step programming, etc. I don't see any SAS/IML in your or Charlie's example, so you might try the Macro/SQL/Data Step forum at http://communities.sas.com/community/sas_macro_facility_data_step_and_sas_language_elements
      These are a valuable resource for people like you who are learning SAS.

      If you really want to do this in SAS/IML, you can post your question to the SAS/IML forum, but you'll need to provide more details about what you have and what you want. Data for a short example will encourage more people to take a look at the question.

  6. Peter
    Posted February 16, 2013 at 11:27 am | Permalink

    built into most environments where SAS is launched are the product specific sample programs and data. They are hiding below the surface, just waiting for the adventurer (including you)!
    Just try running the code:

    proc contents data= sampsio._all_ nods details; run ;

    Although the library SAMPSIO does not reveal itself before you use it, it is probably sitting there quietly waiting to be investigated.

    This will save you any fuss with SASROOT
    (until you really need it)

    ;-)

One Trackback

  1. [...] always found this situation annoying. As I wrote in a previous article, you can access all sample programs from within the SAS Windowing Environment, but I've always wished that the sample data were available [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <p> <pre lang="" line="" escaped=""> <q cite=""> <strike> <strong>