Pages

Thursday, August 1, 2013

Produce a table of contents with Dataset Name, Var, and Obs

options ps=77 ls=132 nodate pageno=1;
title 'FORD data files';
proc sql;
describe table dictionary.tables;
select memname, memlabel, nobs, nvar, modate from dictionary.tables
where libname='DT';
quit;

Choosing the Most Efficient Procedure

There’s often several ways to accomplish the same task with SAS. The question is, are there any guidelines to determine the most efficient method?

It’s impossible to say with certainty that one method or procedure is always more efficient than another because it usually depends on several factors unique to each installation.

But there are a few general guidelines to follow: Procedures designed to perform one specific task are usually more efficient than multi-purpose procedures.

For example, you can use PROC PRINT or PROC SQL to print out a dataset:
proc print data=work.enroll;
run;

proc sql;
select *
from work.enroll;

Since PROC PRINT is specifically designed for printing, it is usually more efficient than using PROC SQL for that purpose.

The Important Exception: PROC DATASETS PROC DATASETS is a utility procedure for managing SAS files that can be used to:
• Copy SAS files from one SAS library to another
• Rename, repair, append or delete SAS files
• List the SAS files in a SAS library
• List the attributes of a SAS dataset
• Manipulate passwords
• Modify dataset attributes
• Modify variables in SAS datasets
• Create and delete indexes
• Create and manage audit files
• Create and delete integrity constraints

Even though PROC DATASETS is a multi-purpose procedure, it tends to be more efficient than using a data step to change dataset attributes, because PROC DATASETS does not process the data portion of the dataset - just the data attributes. Since the data isn’t read in, the amount of time saved when changing dataset attributes on large datasets can be significant.

Appending Files Efficiently

There are a couple of methods you can use to concatenate data sets. In these examples, work.step2 is appended to work.step1:

With a SET statement in a data step:
data work.step1;
set work.step1
work.step2;
run;

With the Datasets procedure:
proc datasets;
append base=work.step1
data=work.step2;
run;

When you use a SET statement, SAS must process all the observations in work.step1 and work.step2.

The APPEND statement bypasses the processing of the data in the original data set (work.step1) and adds the observations in work.step2 directly to the end of the original.

As a result, using the APPEND statement can be more efficient that using a SET statement if the BASE= dataset is large, and all variables in both data sets have the same variables and they are the same length and type.

The APPEND statement is very useful if you frequently add observations to SAS data sets.

Using SAS DATA Step Views to Conserve Data Storage Space

A SAS data view is a type of SAS data set that retrieves data values from other files. A SAS data view only contains descriptor information such as the data types and lengths of the variables, plus information that is required for retrieving data values from other SAS data sets or from files that are stored in other software vendors' file formats.

SAS data views are of member type VIEW.

In most cases, you can use a SAS data view as though it were a SAS data file. Example 1 shows how to create a view:

Example 1:

data sasuser.at_risk / view=sasuser.at_risk;
set work.students;
where at_risk = '1';
run;

The VIEW= option tells SAS to compile (but not execute) the source program and to store the compiled code in the input DATA step view.

You can use a DESCRIBE statement in a DATA step to write a copy of the source code for a data set view to the SAS log:

Example 2:
data view=sasuser.at_risk;
describe;
run;

The log from this data step will show the source code used to create the view.

SAS Data View Benefits
You can save disk space by storing a view definition, which stores only the instructions for where to find the data and how it is formatted, not the actual data.

Views can ensure that the input data sets are always current because data is derived from views at execution time.

Since views can select data from many sources, once a view is created, it can provide prepackaged information without the need for additional programming.

Views can reduce the impact of data design changes on users. For example, you can change a query that is stored in a view without changing the characteristics of the view's result.
With SAS/CONNECT software, a view can join SAS data sets that reside on different host computers, presenting you with an integrated view of distributed data.
You can use views as input to other DATA steps or PROC steps and in combination with other data sources using PROC SQL.

When to Use SAS Data Views
Consider the following in order to determine whether a SAS data file or a SAS data view is better for your purposes:
Data files use additional disk space; data views use additional processing time.
Data file variables can be sorted and indexed prior to use; data views must process data in its existing form during execution.

Quick Debugging of Syntax Errors

If you are working on a lengthy program you can save some time debugging it by first submitting it with zero observations:

OPTION OBS=0;

Your job will run almost instantaneously and identify syntax errors.

%macro skip

When working on a lengthy SAS program, there are a couple of ways to block out parts of your code so they don’t execute.

For example, you can use a forward slash and an asterisk:
/* SAS code to skip */

SAS has an automatic macro for blocking out lines of code, the SKIP macro. The advantage of this method is that it allows you to collapse the code while you’re working on other parts of the program:

- %macro skip;
SAS code to skip
:
:
:
%mend skip;

Macros are one example of collapsable code sections, which enable you to expand or collapse sections of code. An expanded section is indicated by a minus sign in the margin.

To collapse a section of code, click the minus sign. The results are:

+ %macro skip;

A collapsed section is indicated by a plus sign in the margin. To expand a section, click on the plus sign.

Using PROC DOWNLOAD for Non-SAS Files

You can use proc download for files that are not SAS data sets by using infile and outfile statements. In this example, a file named myfile.pdf is downloaded to c:\temp:
filename dest 'c:\temp\myfile.pdf';


rsubmit;

filename source '/home/sas/myfile.pdf';

proc download infile=source
outfile=dest;
run;

endrsubmit;
signoff;