Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Structure of SAS program

Step: data step (to generate new data set) / proc step (to present, sort and analyze data)

A simple data step:

data overview; /* to generate a new data set called overview */


infile cards; /* read in a file starting from cards statement */
length country $30; /*length of country can be as long as 30*/
input Country $ Year gdp pop; /* read in those variables, use $ to
make it a character variable*/
cards;
Canada 2007 87943 1900
Canada 2006 89345 1876
Canada 2005 97712 1873
Australia 2007 85711 2412
Australia 2006 91028 2356
Australia 2005 101124 2342
China 2007 146018 11948
China 2006 151337 11542
China 2005 152035 11182
India 2007 64269 9023
India 2006 63558 8943
India 2005 62854 8876
;
run;

A simple proc step:

proc print data=overview;


run;

No di inc ion be een ppe o lo e ca e e cep i hin SAS name can onl con ain le e
number and underscore (_) and cannot be started with a number.

Another simple proc step:

PROC SORT data=overview out=overview_sorted;


by COUNTRY YEAR;
run;

1
Statement: The steps are made up of smaller units of code called statements. Every SAS statement
ends with a semicolon. There are four types of statements:

data step statements belong to the data step (infile, input, length statements, etc.)

proc step statements belong to the proc step (proc print statement, by statement)

global statements are global in scope and may be used anywhere in the SAS program. They do
not need to belong to a step.

macro statements are part of the SAS macro language, which is not covered by the base
certificate

Global Statements:

*title/footnote statement will take effect only if it is before


execution of the proc step;
title "SAS Xue Bu Hao";
footnote 'Yao Fan Yao Dao Lao ;
*libname statement set up a library for permanent SAS data set or
excel workbook, will discuss immediately in the next topic;
libname saslib "path of the library";
libname qtrdata "qtr1.xls";

Option: Within statement in data step or proc step, there could be options. For example, data = in
proc print is an option.

Options can also be global ar i h op ion ha change he global e ing from heir defa l
behavior.

options PAGESIZE=1 PAGENO=2 NONUMBER NOCENTER OBS=50;

Pagesize: size (length) of a SAS result page (no longer in effect for later version of SAS)

Pageno: reset page number of a SAS result report (no longer in effect for later version of SAS)

Nonumber: suppress page number (no longer in effect for later version of SAS)

Nocenter: do not center the result

2
Comments: There are two ways to make comments in SAS:

*out= is an option in proc sort statement;


/*I am proud of the following code!*/

SAS Data Overview

The SAS data set has a rectangular form


Row represents an individual entity and is called an observation or record.
Column represents an attribute or type of information and is called a
variable/feature/attribute.
The intersection of row and column contains a value.

Temporary SAS data is saved in WORK library and will disappear when SAS restarts. (something with
work library reference like: work.retail, work.account or without library reference like retail,
account)

Permanent SAS data is saved in the library defined in LIBNAME statement and will not disappear.
(something like disk.retail, raw.account, always with library reference).

Example1
The following SAS program is submitted at the start of a new SAS session:

libname sasdata 'SAS-data-library';


data sasdata.sales;
set sasdata.salesdata;
profit=expenses-revenues;
run;
proc print data=sales;
run;

The SAS data set Sasdata.Salesdata has ten observations. Which one of the following explains why a
report fails to generate?

a. The DATA step fails execution.


b. The SAS data set Sales does not exist.
c. The SAS data set Sales has no observations.
d. The PRINT procedure contains a syntax error.

You might also like