How to navigate the guide
This presentation is the property of its rightful owner.
Sponsored Links
1 / 250

How to Navigate the Guide PowerPoint PPT Presentation


  • 53 Views
  • Uploaded on
  • Presentation posted in: General

How to Navigate the Guide. To navigate this SAS Guide, use the PageDown and PageUp buttons on the keyboard. A copy of this PowerPoint document can be downloaded from http://www.biostat.ku.dk/~lts/varians_regression/sasguide.ppt. Preface.

Download Presentation

How to Navigate the Guide

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


How to navigate the guide

How to Navigate the Guide

To navigate this SAS Guide, use the PageDown and PageUp buttons on the keyboard.

A copy of this PowerPoint document can be downloaded from

http://www.biostat.ku.dk/~lts/varians_regression/sasguide.ppt


Preface

Preface

This is The Beginners’ Guide To SAS. The document was originally written by Anna Johansson, MEP, Stockholm.

It has been lightly edited by Peter Dalgaard and Lene Theil Skovgaard for the Ph.D. course on SAS at the Faculty of Health Sciences, University of Copenhagen, May 2002, and later by LTS for the Ph.D. Course in Analysis of Variance and Regression.


Introduction what is sas

IntroductionWhat is SAS?

SAS is a software package for managing large amounts of data and performing statistical analyses.

It was created in the early 1960s by the Statistical Department at North Carolina State University. Today SAS is developed and marketed by SAS Institute Inc. with head office in Cary, North Carolina, U.S.A.


Introduction cont sas in denmark

Introduction (cont.)SAS in Denmark

The Danish subdivision of SAS Institute provides consulting and a wide range of courses. It is located in Copenhagen.

SAS Institute A/S

Købmagergade 7-9

1150 Kbh. K

Tel: 70 28 28 70

Fax: 70 28 29 91

Email: [email protected]


Introduction cont the sas system

Introduction (cont.)The SAS System

The SAS System is mainly used for

  • Data Management (about 80% of all users)

  • Statistical Analysis (about 20% of all users)

    The power of SAS lies in its ability to manage large data sets. It is fast and has many 5statistical and non-statistical features.

    The disadvantage of SAS is its steep learning curve. It takes quite a bit of an effort to get started. User-friendly interfaces do exist, though.


Introduction cont

Introduction (cont.)

Start af SAS på kursussalen:

  • Flyt på musen (eller tænd maskinen)

  • Login er kursusxx

  • Password skifter

  • Vælg START, efterfulgt af STATISTIK og SAS 8.2


Introduction cont getting started

Introduction (cont.)Getting Started

A very good start is to enter the SAS Online Training.

Choose in the menu Help + Getting Started with the SAS Software, then click on the “book”.


Introduction cont sas files

Introduction (cont.)SAS Files

If your data is not yet in a SAS data set, you access the raw data by creating a SAS data set from it.

Once you have made the SAS data set, you use SAS programs to analyse, manage and/or present the data.

SAS data sets can be permanent or temporary. A special library called WORK is created on start-up and deleted on exit.


Introduction cont sas programming

Introduction (cont.)SAS Programming

SAS programming works in two steps:

Data Step

1. reads data from file

2. makes transformations and adds new variables

3. creates SAS Data Set

Proc Step

4. uses the SAS Data Set

5. produces the information we want, such as tables, statistics, graphs, web pages


Introduction cont data and proc steps

Introduction (cont.)Data and Proc Steps

Example of a SAS program:

data work.main;

set work.original;

age=1997-birthyr; Data Step

bmi=weight/(height*height);

run;

proc print data=work.main;

var id age bmi;

run;

Proc Steps

proc means data=work.main;

var age bmi;

run;


Introduction cont sas modules

Introduction (cont.)SAS Modules

The SAS system is made up of several modules, each used for different purposes.

This Guide deals only with the SAS BASE

and the GRAPH modules, giving knowledge on basic data management and simple statistical analyses.

Other modules are SAS/Stat (statistical analyses), SAS/Access (data base applications), SAS/Graph, SAS/Assist (menu-driven info system), SAS/FSP (data entry and retrieval), SAS/Connect (remote submit), etc.


Introduction cont sas at biostat dept

Introduction (cont.)SAS at Biostat Dept.

We primarily use SAS on a Unix server

whereas these notes assume that the programs are run locally on a PC

The basic programming is the same regardless of what platform you use. This is one of the big advantages of SAS.

We do tend to prefer running SAS non-interactively though.


The sas environment windows

The SAS EnvironmentWindows

The main feature of SAS is its division of the main window into two halves. The left part is a navigator of SAS libraries and Results (from the Output window).

The right part is divided into three separate windows:

  • Program window or Enhanced Editor

  • Log window

  • Output window


The sas environment cont windows

The SAS Environment (cont.)Windows

The log and output windows are always opened by default when you start SAS (although they may be hidden behind each other).

The program window and the Enhanced Editor are two different windows but they are used for the same purpose, i.e. writing code and executing it. One of them will open by default.

Other windows are also available and are opened on request

(use View), for instance the Graphics window.


The sas environment cont windows1

The SAS Environment (cont.)Windows

(The program window is a reminiscent of the older SAS version 6. The Enhanced Editor is a new feature of version 8, and is more user-friendly, since it colours the code and works more like an ordinary text editor.)


The sas environment cont windows2

The SAS Environment (cont.)Windows

To check which windows are opened, choose Window in the menu. At the bottom there is a list of opened windows.

The active window is indicated by a . A star * after the window name indicates that the file has not been saved since its latest alteration.

If you are missing any of the windows (Enhanced Editor, Log, Output), you can open it by choosing in the menu

View + window-name


The sas environment cont windows3

The SAS Environment (cont.)Windows

You switch between the windows by choosing

Window + ENHANCED EDITOR

Window + OUTPUT

Window + LOG

in the menu.


The sas environment cont windows4

The SAS Environment (cont.)Windows

The window location on the screen can be changed by choosing

Window + Tile

Window + Cascade

or by pulling the lower right corner of the window with the mouse.

When you exit SAS, the window setting will be kept for the next session (unless someone else ...).


The sas environment cont enhanced editor program window

The SAS Environment (cont.)Enhanced Editor / Program Window

In the Enhanced Editor you write the SAS programs.

The programs tell SAS to produce the data sets, tables, statistics, etc.

A program consists of data steps and proc steps.

A SAS program is executed (submitted) by choosing Run + Submit in the menu (or by clicking on the “Running Man” icon, fourth from the right in the menu).


The sas environment cont output and log windows

The SAS Environment (cont.)Output and Log Windows

The result of a program execution is printed to the Output window. There you will find the prints, tables and reports, etc.

A log file is printed to the Log window.

The log file contains information about the execution, whether it was successful or not. It usually points out your mistakes with warning and error messages so that you can correct them.


The sas environment cont example sas log

The SAS Environment (cont.)Example: SAS Log

65 proc gplot data=work.influnce;

66 plot di*pred / vaxis=axis1 haxis=axis1;

ERROR: Variable DI not found.

NOTE: The previous statement has been deleted.

67 run;

Make a habit of checking the Log window after every execution.

Even if SAS has accepted and executed the program, you may have made a methodological error. Check the note on how many observations were read, and if there were any missing values.


The sas environment cont example sas output

The SAS Environment (cont.)Example: SAS Output

patientens alder

Cumulative

ALDER Frequency Frequency

__________________________________

0 - 24 41 41

25 - 44 176 217

45 - 64 77 294

65- 25 319


The sas environment cont file types

The SAS Environment (cont.)File Types

These files are created by SAS:

  • .sas file (SAS program)

  • .log file (Log)

  • .lst file (Output)

    The SAS data sets are saved as .sd7 or .sas7bdat files.

    (Other file types, e.g. catalogs, are also used and created by SAS, but we will not pursue this any further.)


The sas environment cont using the sas system

The SAS Environment (cont.)Using the SAS System

You work with SAS using

  • Menus and Toolbar

  • Command Line

  • Key Functions F1-F12


The sas environment cont example

The SAS Environment (cont.)Example

Three different ways to Open a File in the Enhanced Editor:

1. Menus: choose File + Open

2. Toolbar: press the icon for “Open”

3. Command line: write

include ‘N:\temp\bp.sas’

and press Enter.


The sas environment cont commands and keys

The SAS Environment (cont.)Commands and Keys


The sas environment cont write and read

The SAS Environment (cont.)Write and Read

In the Enhanced Editor you can

  • create new, or edit existing, programs

  • submit programs

  • save programs (an unsaved file is marked with * after the file name)

    You can NOT edit the log file or the output file in their windows. They are only readable. If you wish to edit these files, save them and use the Enhanced Editor or Word.


Sas syntax statements

SAS syntaxStatements

The SAS code (syntax) consists of statements (sætninger). Statements mostly begin with a keyword (nøgleord), and they ALWAYS end with a SEMICOLON.

data work.cohort;

set course.males98;

run;

proc print data=work.cohort;

run;

Examples of keywords: data, set, run, proc.


Sas syntax cont statements

SAS syntax (cont.)Statements

SAS statements can begin and end anywhere on a line.

data work.cohort;

One or several blanks can be used between words.

data work.cohort;

One or several semicolons can be used between statements.

data work.cohort;;;

;


Sas syntax cont statements1

SAS syntax (cont.)Statements

The statement can begin and end on different lines.

data

work.cohort;

SAS will not object to several statements on the same line. However, it is not considered good programming to have more than one statement per line. It makes the code difficult to read. Avoid this!

data work.cohort; set course.males98; run;


Sas syntax cont indenting to improve readability

SAS syntax (cont.)Indenting to improve readability

Improve the readability of your program by adding more space to the code (= indenting).

Begin data steps and proc steps in the first position, as far left as possible. The ending run statement should also be in the first position.

All statements in between should start a few blanks in from the left margin.

This creates blocks of data steps and proc steps, and you can easily see where one ends and another begins.


Sas syntax cont example of indenting

SAS syntax (cont.)Example of Indenting

data work.height;

infile 'h:\mep\rawdata_height.txt';

input name $ 1-20

kon 21

alder 22-23

height 24-30;

if kon=0 and (height ne .) then

do;

if 0<height<81.75 then lnapprx=50;

else

if 81.75<=height then lnapprx=100;

end;

else lnapprx=.;

run;


Sas syntax cont indenting

SAS syntax (cont.)Indenting

Within statements it is also VERY useful to use indenting. Put similar syntactic words in the same position below each other.

Use blank lines a lot!

Markers of blocks should be placed in the same position below one another (e.g. data-run, proc-run, if-else, do-end).


Sas data sets what is a sas data set

SAS Data SetsWhat is a SAS Data Set?

A SAS data set is a special file type (.sas7bdat) which consists of a descriptive part and a data part.

The DESCRIPTIVE part includes

  • general information, such as data set name, date of creation, number of observations and variables etc.

  • variable information, such as variable name, type (character or numeric), format, length, label etc.


Sas data sets cont the data

SAS Data Sets (cont.)The Data

The DATA part is the data values.

Data is organised with observations in the rows and variables in the columns.


Sas data sets cont descriptive part

SAS Data Sets (cont.)Descriptive Part

Proc CONTENTS prints the descriptive part of a data set.

The CONTENTS Procedure

Data Set Name: PPT_EX8.MAIN Observations: 64

Member Type: DATA Variables: 9

Engine: V8 Indexes: 0

Created: 17:17 Tuesday, August 7, 2001 Observation Length: 72

Last Modified: 17:17 Tuesday, August 7, 2001 Deleted Observations: 0

Protection: Compressed: NO

Data Set Type: Sorted: NO

Label:

-----Alphabetic List of Variables and Attributes-----

# Variable Type Len Pos Format Informat

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

2 BIRTHYR Num 8 0 BEST8. F8.

5 CASE_1 Num 8 24

1 ID Char 8 56 $8. $8.

4 LENGTH Num 8 16 BEST8. F8.

3 WEIGHT Num 8 8 BEST8. F8.

6 age Num 8 32 4.

8 bmi Num 8 48

9 generatn Char 5 64

7 height Num 8 40


Sas data sets cont data part

SAS Data Sets (cont.)Data Part

Proc PRINT prints the data part of a data set.

OBS ID BIRTHYR WEIGHT HEIGHT AGE BMI

1 001 1954 62 1.65 43 22.7732

2 002 1956 68 1.67 41 24.3824

3 003 1956 65 1.72 41 21.9713

4 004 1962 56 1.68 35 19.8413

5 005 1954 58 1.59 43 22.9421

6 006 1953 52 1.62 44 19.8141

7 007 1955 69 1.75 42 22.5306

8 008 1955 75 1.73 42 25.0593

9 009 1960 82 1.7 37 28.3737

10 010 1962 68 1.72 35 22.9854

11 011 1961 65 1.68 36 23.0300

12 012 1954 62 1.69 43 21.7079

13 013 1956 58 1.68 41 20.5499

14 014 1962 61 1.64 35 22.6800

15 015 1958 58 1.63 39 21.8300

16 016 1959 62 1.65 38 22.7732

17 017 1962 59 1.64 35 21.9363

18 018 1957 73 1.8 40 22.5309


Sas data sets cont create a data set

SAS Data Sets (cont.)Create a Data Set

A SAS data set is created from

  • SAS data set (.sas7bdat file)

  • raw data file (.txt file)

  • another external file through importing (EXCEL file, etc.)

    or by

  • manually entering the data


Sas data sets cont create a data set1

SAS Data Sets (cont.)Create a Data Set

To use an existing data set, a .sas7bdat file, is the most common way to create a SAS data set.

How to create a SAS data set from a raw data file is described in chapter Read Raw Data Into SAS.

Importing non-SAS data is not trivial. Use File + Import Data. Ask for help if you run into trouble.

- or use the program STAT-Transfer


Sas data sets cont create a data set2

SAS Data Sets (cont.)Create a Data Set

The easiest way to manually enter data into SAS is via the Viewtable facility (see later on in this chapter).

You can also use the CARDS or DATALINES statement (chapter Read Raw Data Into SAS).


Sas data sets cont existing sas data set sas7bdat

SAS Data Sets (cont.)Existing SAS Data Set (.sas7bdat)

Create a SAS data set from an existing SAS data set:

data work.main;

set work.original;

statements;

run;

This will yield an exact copy of the old data set “original”. The name of the copy is “main”.

Usually we wish to change the new data set, by adding programming statements after the SET statement.


Sas data sets cont naming data sets

SAS Data Sets (cont.)Naming Data Sets

PLEASE, use descriptive names for your data sets.

It is not considered clever to name your data sets: final1, final2, final3, etc.

Other names to avoid are: new, old, mydata, analys, your-name, etc.

More on this topic in the chapter Naming Data Sets and Variables.


Sas data sets cont viewtable

SAS Data Sets (cont.)Viewtable

The Viewtable facility is a user-friendly tool to look at your data set without using data steps or proc steps.

You enter the Viewtable window by issuing the “viewtable” command in the Command line.

This will yield a window very similar to EXCEL, with cells, rows and columns.


Sas data sets cont viewtable1

SAS Data Sets (cont.)Viewtable

It is very easy to create a data set in the Viewtable window. Just enter the data manually into the cells. The variable names are created by clicking on the column header and following the instructions.

When you click to save the data set, it is saved into a .sas7bdat file, which may then be used in any data step or proc steps in the Enhanced Editor.


Sas data sets cont viewtable2

SAS Data Sets (cont.)Viewtable

If you wish to open an existing data set into the Viewtable window, just issue the command

“viewtable name-of-data- set”

and it will open.

You can also open a data set from the Explorer window in the window area to the left. Just navigate to the right library and double click on the data set icon.


Sas data sets cont variables

SAS Data Sets (cont.)Variables

There are two types of variables in SAS: character (char) and numerical (num).

The type refers to the values the variable have.

Examples of a variable called MONTH:

A character variable: MONTH with values ‘Jan’, ‘Feb’, …, ‘Dec’.

A numerical variable: MONTH with values 1, 2, … , 12.


Sas data sets cont variables1

SAS Data Sets (cont.)Variables

The values of a character variable are between quotes ‘’. When the value is printed, all characters within the quotes are printed.

Typical character values are letters, while numerical values always are digits.

Character variables may include digits as well.


Sas data sets cont variables2

SAS Data Sets (cont.)Variables

Missing values of a character variable are represented by a blank, while a period “.” (punktum) denotes missing values of a numeric variable.

Character values can be 32767 characters long at most.

(200 characters in version 6)

Good rule: Never use char variables to store numeric values. For example, always store Patient Number as a numeric.


Sas data sets cont naming variables

SAS Data Sets (cont.)Naming Variables

Variable names (e.g. age, bmi) and data set names (e.g. main, original)

  • can be 32 characters (letters, underscores and digits) long at most

  • can be uppercase or lowercase or mixed (mAiN = MaIn)

  • must start with a letter (A-Z) or an underscore (_), not a digit


Sas data libraries what is a sas data library

SAS Data LibrariesWhat is a SAS Data Library?

A SAS data library is the catalogue where your data sets are stored.

A data library is like a drawer in a filing cabinet. The cabinet may have several drawers representing several different libraries.

A data set is a file within a drawer. A drawer may contain several files.


Sas data libraries cont figure sas data libraries

SAS Data Libraries (cont.)Figure: SAS Data Libraries


Sas data libraries cont work and sasuser

SAS Data Libraries (cont.)WORK and SASUSER

Two data libraries are created automatically by SAS:

WORK and SASUSER.

The WORK library is a temporary library. All its contents are deleted when you exit SAS. If you wish to keep your data sets, do not put them in the WORK library.

The SASUSER library is a permanent library. All its contents are kept when you exit SAS.


Sas data libraries cont work and sasuser1

SAS Data Libraries (cont.)WORK and SASUSER

The physical location of the permanent SASUSER library is under ‘C:\’.

It is not especially clever to save all the SAS programs and data sets in the same folder.

Generally we wish to store them in separate folders for separate projects or papers.

Therefore, it is possible to create your own permanent libraries.


Sas data libraries cont libraries and folders

SAS Data Libraries (cont.)Libraries and Folders

A library is a physical folder anywhere on your hard disk or server disk, or even floppy disk.

The physical folder for the SASUSER library on Anna’s computer is ‘C:\Winnt\Profiles\annaj\…\V8’.

Similarly, if you wish to store data sets in a particular folder, you can create a library by using a libname statement.


Sas data libraries cont libnames

SAS Data Libraries (cont.)Libnames

SAS data sets have names of the form

work.original

WORK is the library where the data set is stored.

ORIGINAL is the data file (.sas7bdat) in that library.

The two components of the name are separated by a period (punktum).


Sas data libraries cont libnames1

SAS Data Libraries (cont.)Libnames

More formalised, SAS data sets have names of the form

libref.filename

The libref (library reference) is the name of the library.

The filename is the name of data file (.sas7bdat).


Sas data libraries cont the work library

SAS Data Libraries (cont.)The WORK Library

For the WORK library you can omit the libref WORK, and simply write “original” as the data set name.

If no other libname is used, all data sets and formats (see chapter Formats) created during a SAS session are saved to the WORK library.

Be aware that since WORK is a temporary library, all its contents are deleted when you exit SAS.


Sas data libraries cont libname statement

SAS Data Libraries (cont.)Libname Statement

A libref is defined by a libname statement, which links the libref to the physical location of a folder.

Libname statements are of the form

libname libref ‘location-of-folder ’;

Libname statements are written outside data steps and proc steps, generally at the top of a program. Once it has been submitted the libref will remain defined until you exit SAS.


Sas data libraries cont libname statement1

SAS Data Libraries (cont.)Libname Statement

Example of SAS program

libname sas engine ‘.../phd/artikel1/sasdata’;

data sammenlign;

set sas.glostrup;

proc ...


Sas data libraries cont librefs

SAS Data Libraries (cont.)Librefs

The LIBREF is just a link.

It makes the data set easier to access since you do not need to specify the complete location (P:\catalogue\sub-catalogue(s)...\filename.sas) in the data and proc steps.

When a LIBREF is deleted, i.e. the SAS session ends, the folder it refers to still exists.


Submitting a sas program submit a program

Submitting a SAS ProgramSubmit a Program

To submit (= execute) a SAS program:

  • Menus: choose Run + Submit, or

  • Toolbar: press the icon with “the running man”, or

  • Command line: issue “submit” command.

    To halt a submitted program press CTRL + BREAK. You may have to press it a few times.


Submitting a sas program cont submit a program

Submitting a SAS Program (cont.)Submit a Program

When a program is submitted each step (data or proc) is executed one at a time.

The contents of a data step is performed on each observation one at a time (i.e. creation of new variables etc.).

Each step generates log to the Log window. Output (if any)

is generated to the Output window.


Submitting a sas program cont check the log

Submitting a SAS Program (cont.)Check the Log

ALWAYS browse the Log window after a submission!

If the submission is stopped by errors the data set is unchanged and you might do incorrect analyses on an old data set. You will only see it has been stopped by looking at the log.

(We emphasise this, since we are too well experienced with the consequences of the opposite.)


Submitting a sas program cont error messages in the log

Submitting a SAS Program (cont.)Error Messages in the Log

Note (blue): information which do not indicate errors. They are usually informative to rule out any methodological errors in you programming.

Warning (green): points out errors which SAS could correct itself. The execution was performed with these changes. Still you should check whether it was done properly. Example: misspelled keywords.


Submitting a sas program cont error messages in the log1

Submitting a SAS Program (cont.)Error Messages in the Log

Error (red): serious errors which SAS could not handle. The execution was stopped. These errors must be corrected by you. Example: forgotten semicolons, invalid options, misspelled variable names.

Especially if you are updating data sets, be aware that red errors mean NO updating!


Submitting a sas program cont unbalanced quotes

Submitting a SAS Program (cont.)Unbalanced Quotes

A special type of syntactic error is unbalanced quotes (‘’).

Quotes must come in pairs. If they do not, the execution will keep on running forever. You halt it by submitting

’;


Submitting a sas program cont enhanced editor

Submitting a SAS Program (cont.)Enhanced Editor

When you press submit, all the code in the Enhanced Editor is executed.

If you only wish to submit a limited number of rows of the program code, mark it and press submit.


The data step data statement

The Data StepData Statement

Data sets are created through a data step. The data step begins with a DATA statement.

General form of the DATA statement:

data SAS-data-set;

The SAS-data-set is a name of the form

libref.filename


The data step cont data statement

The Data Step (cont.)Data Statement

To create a data set ORIGINAL in the temporary library WORK:

data work.original;

When using the temporary WORK library it is possible to skip the work prefix and just write:

data original;


The data step cont create variables

The Data Step (cont.)Create Variables

A new variable is created by:

variable=expression ;

The expression may consist of numbers, other variables and operators such as

+ addition

- subtraction

* multiplication

/ division

** exponentiation (potensopløftning)


The data step cont examples

The Data Step (cont.)Examples

data work.main;

set work.original;

age=1997-birthyr;

height=height/100;

bmi=weight/(height*height);

run;


The data step cont functions

The Data Step (cont.)Functions

Useful are the predefined functions in SAS, such as

exp(argument)exponential function

log(argument)natural logarithm

int(argument)the integer part of a numeric argument

There are also non-mathematical SAS specific functions. A list of useful functions may be found in the SAS manual SAS Language (pp 521-616).


The data step cont examples1

The Data Step (cont.)Examples

data work.main;

set work.original;

highest=max(height1, height2, height3);

birthyr=year(brthdate);

total=sum(x1,x2,x3,x4,x5);

run;


The data step cont variable names

The Data Step (cont.)Variable Names

If you are creating a series of variables, such as repeated measurements, put the order number at the end of the name, e.g. x1, x2, x3, since

total=sum(x1,x2,x3,x4,x5)  total=sum(of x1-x5)

The use of the notation x1-x5 is widely accepted in many expressions and procedures. It will not work if the order number is in the middle of the name.

Also see the chapter Naming Data Sets and Variables.


The proc steps procedures

The Proc StepsProcedures

A procedure (proc) is a predefined function that operate on data sets. By specifying the predefined statements in the procedure you can adapt it to your needs and wishes.

Examples of procedures:

proc contents prints the descriptive part of a data set

proc print prints the data part of a data set

proc freq creates frequency tables, etc.

proc means calculates means and other statistics


The proc steps cont data step vs proc step

The Proc Steps (cont.)Data Step Vs. Proc Step

Usually, for beginners as well as among advanced users, the data step is more comprehensible as a concept. Not seldom is extensive programming done in the data step, when the same result easily could have been obtained through a simple option in a procedure.

As a rule, operations on observations (within rows) are done in the data step, e.g. adding two variables together to make a third.

Operations on variables (within columns) are done in a proc step, e.g. taking the mean of a variable.


The proc steps cont proc contents

The Proc Steps (cont.)Proc CONTENTS

The CONTENTS procedure prints out the descriptive part of a data set.

The descriptive part includes:

  • General information: data set name, number of observations, number of variables, etc.

  • Variable information: variable name, type, length, position, format, label, etc.


The proc steps cont proc contents1

The Proc Steps (cont.)Proc CONTENTS

The general form of the CONTENTS procedure:

proc contents data=SAS-data-set;

run;

Example:

libname course‘h:\SasAtMEP\Course’;

proc contents data=course.main;

run;


The proc steps cont proc print

The Proc Steps (cont.)Proc PRINT

The PRINT procedure prints out the data part of a data set.

It is possible to choose which variables to print. If none are chosen, all variables will be printed.

The first column is the OBS column, which indicates observation. If there are more variables than will fit into the output window, the output is split and the exceeding variables printed on the following page. The OBS column is reprinted to indicate observation.


The proc steps cont proc print1

The Proc Steps (cont.)Proc PRINT

The general form of the PRINT procedure:

proc print data=SAS-data-set;

run;

OR to print a specified list of variables from the data set

proc print data=SAS-data-set;

var variable1 variable2 variable3 ... ;

run;


The proc steps cont proc print2

The Proc Steps (cont.)Proc PRINT

Examples:

proc print data=course.main;

run;

proc print data=course.main;

var age bmi;

run;


The proc steps cont proc freq

The Proc Steps (cont.)Proc FREQ

The FREQ procedure is mainly used to create frequency tables, although it has a wide range of statistical features as well.

It creates both one-way and multiple-way tables.

(FREQ is pronounced “frek” in Danish and “freek” in English.)


The proc steps cont proc freq1

The Proc Steps (cont.)Proc FREQ

The general form of FREQ procedure:

proc freq data=SAS-data-set;

tables var1;

run;

OR for a two-way table

proc freq data=SAS-data-set;

tables var1 *var2 / nopercent norow nocol;

run;


The proc steps cont proc freq2

The Proc Steps (cont.)Proc FREQ

Example one-way table:

proc freq data=course.main;

tables age;

run;

The SAS System

Cumulative Cumulative

AGE Frequency Percent Frequency Percent

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

35 4 22.2 4 22.2

36 1 5.6 5 27.8

37 1 5.6 6 33.3

43 3 16.7 17 94.4

44 1 5.6 18 100.0


The proc steps cont proc freq3

The Proc Steps (cont.)Proc FREQ

Example two-way table:

proc freq data=course.main;

tables case*age/nopercent nocol norow;

run;

The nopercent option suppresses the printing of cell percentages.

Nocol and norow suppress column and row cell percentages respectively.


The proc steps cont proc freq4

The Proc Steps (cont.)Proc FREQ

The SAS System

TABLE OF AGE BY CASE_1

AGE CASE_1

Frequency‚ 0‚ 1‚ Total

ƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆ

35 ‚ 3 ‚ 1 ‚ 4

ƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆ

36 ‚ 1 ‚ 0 ‚ 1

ƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆ

37 ‚ 0 ‚ 1 ‚ 1

ƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆ

...

ƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆ

44 ‚ 0 ‚ 1 ‚ 1

ƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆ

Total 9 9 18


The proc steps cont proc sort

The Proc Steps (cont.)Proc SORT

The SORT procedure sorts the data set according to a chosen variable. The sorted data set replaces the unsorted data set, unless you define an OUT data set.

The SORT procedure can sort by several variables, in ascending (default) or descending (option) order.

Missing values are defined as minus infinity, i.e. less than all other numeric values.


The proc steps cont proc sort1

The Proc Steps (cont.)Proc SORT

The general form of the SORT procedure:

proc sort data=SAS-data-set out=SAS-data-set;

by variables;

run;

OR if you want descending order

proc sort data=SAS-data-set out=SAS-data-set;

by descending variables;

run;


The proc steps cont proc sort2

The Proc Steps (cont.)Proc SORT

Example:

proc sort data=course.main out=course.sortage;

by case_1 age;

run;

This will yield a data set called course.sortage, where the observations are sorted by case_1 and within each category of case_1 by age.


The proc steps cont proc sort3

The Proc Steps (cont.)Proc SORT

There is no result in the Output window from proc SORT, but a proc PRINT of data set course.sortage gives:

OBS ID BIRTHYR WEIGHT HEIGHT AGE BMI CASE_1

1 010 1962 68 1.72 35 22.9854 0

2 014 1962 61 1.64 35 22.6800 0

3 017 1962 59 1.64 35 21.9363 0

4 011 1961 65 1.68 36 23.0300 0

...

9 012 1954 62 1.69 43 21.7079 0

10 004 1962 56 1.68 35 19.8413 1

11 009 1960 82 1.7 37 28.3737 1

12 002 1956 68 1.67 41 24.3824 1

13 003 1956 65 1.72 41 21.9713 1

14 007 1955 69 1.75 42 22.5306 1

...

17 005 1954 58 1.59 43 22.9421 1

18 006 1953 52 1.62 44 19.8141 1


The proc steps cont proc means

The Proc Steps (cont.)Proc MEANS

The MEANS procedure calculates basic statistics. By default the statistics are

N = number of non-missing observations

Mean = mean value, average

Std Dev = standard deviation

Minimum = minimum value

Maximum = maximum value

Optional statistics include Nmiss (number of missing observations), range (maximum-minimum), etc.


The proc steps cont proc means1

The Proc Steps (cont.)Proc MEANS

The general form of MEANS procedure:

proc means data=SAS-data-set;

run;

This will yield summary statistics on all variables in the data set.

Missing values are excluded from the analysis.


The proc steps cont proc means2

The Proc Steps (cont.)Proc MEANS

Example:

proc means data=course.main;

run;

The MEANS Procedure

Variable N Mean Std Dev Minimum Maximum

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

BIRTHYR 62 1959.35 3.3294354 1953.00 1967.00

WEIGHT 63 61.7460317 6.5129356 47.0000000 80.0000000

LENGTH 64 1.6675000 0.0617213 1.4800000 1.8000000

CASE_1 64 0.4687500 0.5029674 0 1.0000000

age 62 37.6451613 3.3294354 30.0000000 44.0000000

height 64 1.6675000 0.0617213 1.4800000 1.8000000

bmi 63 22.1982718 1.9262282 17.9591837 29.3847567

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƑƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

Naturally, the character variable ID is not displayed.


The proc steps cont proc means3

The Proc Steps (cont.)Proc MEANS

You can modify the proc MEANS code to suit your wishes:

  • To specify the number of decimals used in the printout add the option MAXDEC.

  • If you are only interested in a selection of variables, use a VAR statement.


The proc steps cont proc means4

The Proc Steps (cont.)Proc MEANS

Example:

proc means data=course.main maxdec=2;

var age bmi;

run;

Variable N Mean Std Dev Minimum Maximum

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

AGE 18 39.44 3.24 35.00 44.00

BMI 18 22.65 1.95 19.81 28.37

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ


The proc steps cont proc means5

The Proc Steps (cont.)Proc MEANS

In proc MEANS it is possible to calculate statistics on subgroups of the data set, e.g. the mean bmi and age for cases and controls separately.

There are two different ways to deal with subgroup statistics, depending on what output you are interested in:

  • BY statement

  • CLASS statement


The proc steps cont proc means6

The Proc Steps (cont.)Proc MEANS

The BY statement:

proc means data=course.main maxdec=2;

var age bmi;

by case_1;

run;

The BY statement requires that the data set has previously been sorted according to the BY variable.


The proc steps cont proc means7

The Proc Steps (cont.)Proc MEANS

Result from BY statement:

CASE_1=0

The MEANS Procedure

Variable N Mean Std Dev Minimum Maximum

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

age 32 37.50 3.08 33.00 44.00

bmi 34 22.09 1.95 17.96 29.38

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

CASE_1=1

Variable N Mean Std Dev Minimum Maximum

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

age 30 37.80 3.62 30.00 44.00

bmi 29 22.33 1.92 19.61 27.34

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ


The proc steps cont proc means8

The Proc Steps (cont.)Proc MEANS

The CLASS statement:

proc means data=course.main maxdec=2;

var age bmi;

class case_1;

run;

The CLASS statement does NOT require any sorting.


The proc steps cont proc means9

The Proc Steps (cont.)Proc MEANS

Result from CLASS statement:

The MEANS Procedure

N

CASE_1 Obs Variable N Mean Std Dev Minimum Maximum

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

0 34 age 32 37.50 3.08 33.00 44.00

bmi 34 22.09 1.95 17.96 29.38

1 30 age 30 37.80 3.62 30.00 44.00

bmi 29 22.33 1.92 19.61 27.34

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ


The online help quick help on syntax

The Online HELPQuick Help on Syntax

SAS has a very good ONLINE HELP. In this help you can get full information on syntax. For more theoretical issues you should use the paperback manuals or the Online Documentation (see later on how to use them).

The online help is accessed through the Command line and command “help”.

help print

help means

(Do not write “proc” before the process name.)


The online help cont using the online help

The Online HELP (cont.)Using the Online Help

When a help command is issued the HELP Window will open with topics:

Introduction

Syntax

Additional Topics (occasionally)

To get full information on how to write the code, choose SYNTAX.


The online help cont example

The Online HELP (cont.)Example

As an example, access online help for proc MEANS.

“help means” + choose SYNTAX

These are all the possible statements that proc MEANS accept. If you want to know more about a specific statement just click on it and read:


The online help cont example1

The Online HELP (cont.)Example

PROC MEANS: Syntax

PROC MEANS <option(s)> <statistic-keyword(s)>; BY <DESCENDING> variable-1 <... <DESCENDING> variable-n><NOTSORTED>;

CLASS variable(s) </ option(s)>;

FREQ variable;

ID variable(s);

OUTPUT <OUT=SAS-data-set> <output-statistic-specification(s)>

<id-group-specification(s)> <maximum-id-specification(s)>

<minimum-id-specification(s)> </ option(s)> ;

TYPES request(s);

VAR variable(s) < / WEIGHT=weight-variable>;

WAYS list;

WEIGHT variable;


The online help cont explanation to the online help text

The Online HELP (cont.)Explanation to the Online Help Text

  • underlined word = keyword referring to a statement (statements within a procedure are optional, the PROC and the RUN statements are required)

  • black word = required if the corresponding keyword is used

  • words within “< >” = optional, not required

  • words separated by “|” = possible choices of values for a specific option


The online help cont example2

The Online HELP (cont.)Example

If you click on the “PROC MEANS”, a list of possible options will be displayed.

Among them is the MAXDEC= option which we have already used. The equal sign is required. Next to MAXDEC= is the black word “number”. If you use the MAXDEC option you are required to fill in a number corresponding to the maximum number of decimals to be displayed.

(The exact conventions depend on which version of the help you use…. –pd)


Labels what are labels

LabelsWhat are Labels?

Each variable has a variable name (e.g. birthyr) and a LABEL (e.g. Year of Birth). The label is how the variable is written on the output. By default the label = variable name unless you specify it.

To define and assign a label, use the LABEL statement.

label variable1 = ‘label-name1’

variable2 = ‘label-name2’

...

;


Labels cont what are labels

Labels (cont.)What are Labels?

Labels can be 256 characters long at most.

The output from proc CONTENTS include a column with labels for all the variables in the data set.

To delete a label simply define the label equal to space:

label variable1 = ‘ ‘;


Labels cont permanent or temporary labels

Labels (cont.)Permanent or Temporary Labels

Labels can be assigned inside a data step or a proc step.

Labels assigned in a data step are permanent. They are also transferred to new data sets.

Labels assigned in a proc step are temporary. A temporary label replaces a permanent label throughout the execution of the procedure step.

Most common are permanent labels defined in the data step.


Labels cont example

Labels (cont.)Example

Assigning permanent labels in a data step:

data course.main;

set course.original;

age=1997-birthyr;

height=height/100;

bmi=weight/(height*height);

label birthyr=‘Year of Birth’

age=‘Alder’

height=‘Højde’

bmi=‘BMI’;

run;


Labels cont example1

With label:

Year of

OBS Birth

1 1954

2 1956

3 1956

4 1962

5 1954

6 1953

7 1955

...

18 1957

Without label:

OBS BIRTHYR

1 1954

2 1956

3 1956

4 1962

5 1954

6 1953

7 1955

...

18 1957

Labels (cont.)Example


Formats what are formats

FormatsWhat are Formats?

Formats are used on variable values to

  • display the values differently from the raw values (e.g. with fewer decimals, or as dates)

  • group the values (values 0-25=low, values 26-100=high)

    There are predefined formats in SAS which you may use, but you can also create your own formats. The procedures are designed to handle formats and use them accordingly.


Formats cont assign formats

Formats (cont.)Assign Formats

To assign formats you use the FORMAT statement inside a data step (permanently) or a proc step (temporarily).

The general form of the FORMAT statement is

format variable1 format1.;

The following yields a value with two digits, a decimal point and two decimals (5 positions, of which two are decimals):

format bmi 5.2;


Formats cont example permanent assignment

Formats (cont.)Example Permanent Assignment

data course.main;

set course.original;

age=1997-birthyr;

height=height/100;

bmi=weight/(height*height);

format age 4.0

bmi 4.2

birthyr best4.;

run;


Formats cont example temporary assignment

Formats (cont.)Example Temporary Assignment

proc print data=course.main;

var birthyr age bmi;

format age 4.0

bmi 4.2

birthyr best5.;

run;

Usually, the format statement is at the end of the data or proc step together with the label statement.


Formats cont predefined sas formats

Formats (cont.)Predefined SAS Formats

Formats are all of the form (where < > indicates optional and is not to be typed in):

format-name<w>.<d>

w indicates maximum number of positions used to display the value

d indicates optional number of decimals in a numeric format


Formats cont predefined sas formats1

Formats (cont.)Predefined SAS Formats

Formats for character variables need a $ sign in the first position:

$format-name<w>.<d>

All formats, numeric or character, MUST contain a period (. punktum), either at the end or before the d value.

See examples.


Formats cont predefined sas formats2

Formats (cont.)Predefined SAS Formats

w.d = numeric values at most w positions long, and d of these positions are decimals

$w. = character values w positions long

COMMAw.d = numeric values with commas and decimal points: 12,345.67

BESTw. = chooses the best notation with w positions for numeric values

The period (.) occupies one position in all of these formats.


Formats cont example

Formats (cont.)Example


Formats cont user defined formats

Formats (cont.)User-defined Formats

There are situations when the predefined formats do not suffice.

An example, you wish to group the BMI values into three categories; underweight, normal weight, overweight.

There is no predefined format to meet your demands in this situation. The solution is to create your own format.


Formats cont user defined formats1

Formats (cont.)User-defined Formats

To use your own formats you must

  • define the format

  • assign the format

    Several variables may be assigned to the same format

    and

    A variable may be assigned to different formats in different procedures


Formats cont proc format defines formats

Formats (cont.)Proc FORMAT defines formats

Formats are defined through the FORMAT procedure.

proc format;

value format-name range1 = ‘label’

range2 = ‘label’

... ;

run;

The labels must be inside quotes (‘’).


Formats cont user defined formats2

Formats (cont.)User-defined Formats

Format names are like any other SAS names, however they must not end in a number.

A format for a character variable must have a dollar sign $ as its first character.

Format names do NOT end with a period (.) in proc FORMAT. The period is only used when assigning the format in a data or proc step.


Formats cont example user defined formats

Formats (cont.)Example: User-defined Formats

A case/control format (case_1f) and a BMI format (bmif).

proc format;

value case_1f 0=‘Case’

1=‘Control’

other=‘Other’;

value bmif low-20.0=‘Underweight’

20.0-25.0=‘Normal weight’

25.0-high=‘Overweight’

other=‘Other’;

run;


Formats cont example user defined formats1

Formats (cont.)Example: User-defined Formats

Above, a value of 20.0000 would fall into Underweight, but 20.0001 would fall into Normal weight.

The first true range alternative is used for a value of a variable assigned by the format.


Formats cont special format values

Formats (cont.)Special Format Values

other = all other values, including missing values

low = the lowest value (minimum) of the variable assigned to the format, including missing values. (For character formats low does not include missing values.)

high = the highest value (maximum) of the variable assigned to the format


Formats cont assigning user defined formats

Formats (cont.)Assigning User-defined Formats

User-defined formats are assigned by a FORMAT statement, exactly as with the predefined formats.

proc freq data=course.main;

tables bmi;

format bmi bmif.;

run;

Cumulative Cumulative

BMI Frequency Percent Frequency Percent

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

Underweight 2 11.1 2 11.1

Normal weight 14 77.8 16 88.9

Overweight 2 11.1 18 100.0


Formats cont assigning user defined formats1

Formats (cont.)Assigning User-defined Formats

proc means data=course.main maxdec=1;

class bmi;

var age;

format bmi bmif.;

run;

The MEANS Procedure

Analysis Variable : age

N

bmi Obs N Mean Std Dev Minimum Maximum

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

Underweight 7 7 37.9 3.3 35.0 44.0

Normal weight 51 49 37.6 3.4 30.0 44.0

Overweight 5 5 38.0 3.3 35.0 42.0

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ


Formats cont assigning user defined formats2

Formats (cont.)Assigning User-defined Formats

As shown above, user-defined formats are assigned in the exact same way as the SAS formats:

format variable1 format1.;


Titles and footnotes titles

Titles and FootnotesTitles

You can add titles to the output with a TITLE statement. A TITLE statement is one of the global statements which do not have to be included in a data step or a proc step (other global statements are the LIBNAME and OPTIONS statements) .

The form of the TITLE statement is

title ‘here-you-write-the-title’;

The title must be surrounded by quotes (’).


Titles and footnotes cont example

Titles and Footnotes (cont.)Example

title ‘BMI Body Mass Index’;

proc freq data=course.main;

tables bmi;

format bmi bmif.;

run;

BMI Body Mass Index

Cumulative Cumulative

BMI Frequency Percent Frequency Percent

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

Underweight 2 11.1 2 11.1

Normal weight 14 77.8 16 88.9

Overweight 2 11.1 18 100.0


Titles and footnotes cont delete titles

Titles and Footnotes (cont.)Delete Titles

A title will stay defined, and be printed to all output, until it is changed, or deleted.

To delete a title simply write

title;


Titles and footnotes cont several titles

Titles and Footnotes (cont.)Several Titles

It is also possible to have second titles below the main title.

A maximum of 10 titles can be used simultaneously.

title1 ‘here-you-write-the-first-title’;

title2 ‘here-you-write-the-second-title’;

...

title10 ‘here-you-write-the-tenth-title’;


Titles and footnotes cont several titles1

Titles and Footnotes (cont.)Several Titles

The unnumbered title statement, is equal to the title1 statement.

It is possible to have, for example, title2 undefined or deleted while title3 is defined. It will result in a gap between title1 and title3 on the printout representing title2.

However, when you delete say title3, all titles beneath it (title4-title10) will also be deleted.

title3 ;


Titles and footnotes cont example1

Titles and Footnotes (cont.)Example

title ‘BMI Body Mass Index’;

title2 ‘Women 35-45 yrs’;

proc freq data=course.main;

tables bmi;

format bmi bmif.;

run;


Titles and footnotes cont example2

Titles and Footnotes (cont.)Example

BMI Body Mass Index

Women 35-45 yrs

Cumulative Cumulative

BMI Frequency Percent Frequency Percent

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

Underweight 2 11.1 2 11.1

Normal weight 14 77.8 16 88.9

Overweight 2 11.1 18 100.0


Titles and footnotes cont titles window

Titles and Footnotes (cont.)Titles Window

A shortcut to defining titles is the Titles window.

Issue the “title” command in the Command line.

The Titles window will open, with all your current title definitions. From here the titles can be changed directly by editing.

The disadvantage of this shortcut is that you can NOT save the title definitions, as you could have if you had written them in code. When a program is rerun later after many title changes, the titles will not be as originally.


Titles and footnotes cont footnotes

Titles and Footnotes (cont.)Footnotes

Footnotes work in the exact same way as titles. The only difference is that footnotes are written at the bottom of the printout.

footnote ‘here-you-write-the-footnote’;

To delete a footnote write

footnote;


Titles and footnotes cont example3

Titles and Footnotes (cont.)Example

footnote ‘BMI Body Mass Index’;

footnote2 ‘Women 35-45 yrs’;

proc freq data=course.main;

tables bmi;

format bmi bmif.;

run;


Titles and footnotes cont example4

Titles and Footnotes (cont.)Example

Cumulative Cumulative

BMI Frequency Percent Frequency Percent

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

Underweight 2 11.1 2 11.1

Normal weight 14 77.8 16 88.9

Overweight 2 11.1 18 100.0

BMI Body Mass Index

Women 35-45 yrs


Titles and footnotes cont footnotes window

Titles and Footnotes (cont.)Footnotes Window

To open the Footnotes window and edit footnotes directly, issue the command “footnote” in the Command line.

________________________________________________

There are lots of additional features to titles and footnotes available, such as fonts, sizes and orientation, etc. See the manual SAS/GRAPH Software Vol. I.


Subsetting a data set subsets of data

Subsetting a Data SetSubsets of Data

Often one wants to use only a subset of a data set, e.g. persons older than 60 years, women, cases etc.

This is particularly useful when performing data cleaning, and you only want to print the observations with extreme values of a variable, say blood pressure > 200.


Subsetting a data set cont where option

Subsetting a Data Set (cont.)WHERE option

In procedures you use the WHERE data set option to subset the data set.

proc print data=SAS-data-set(where=(expression));

run;

The WHERE data set option may be used in any procedure. It can also be used in data steps, although it is less usual.

data course.cases;

set course.main(where=(case_1=1));

run;


Subsetting a data set cont where option1

Subsetting a Data Set (cont.)WHERE option

The expression must be a logical one, resulting in true or false.

Only observations for which the expression is truewill be used in the proc step.

Examples of expressions are:

where=(birthyr gt 1950)

where=(1947<birthyr<1950)

where=(birthyr=1947)


Subsetting a data set cont conditional operators

Subsetting a Data Set (cont.)Conditional Operators

Possible conditional operators (use sign or abbreviation):

= eq equal to

^= ne not equal to

> gt greater than

< lt less than

>= ge greater than or equal to

<= le less than or equal to


Subsetting a data set cont logical operators

Subsetting a Data Set (cont.)Logical Operators

You can also combine several expressions by using logical operators:

ANDif all expressions are true then the

compound expression will be true,

else it is false

ORif any of the expressions are true, then the

compound expression will be true,

else it is false


Subsetting a data set cont examples

Subsetting a Data Set (cont.)Examples

proc freq data=course.main(where=(birthyr<1950 and case_1=1));

... ;

run;

proc means data=course.main(where=((birthyr<1950 or

birthyr>1953)

and

(case_1 ne .)));

... ;

run;

We recommend that you use parentheses as much as you can. It will enhance the reading as well as reduce the errors. There is no limit to how many pairs you are allowed to use.


Subsetting a data set cont special operators

Subsetting a Data Set (cont.)Special Operators

Special operators to use with the WHERE option:

IS MISSINGtrue if value is missing

BETWEEN - ANDtrue if value falls within the range

Examples:

where=(birthyr is missing)  where=(birthyr=.)

where=(birthyr between 1950 and 1952) 

 where=(1950<=birthyr<=1952)


Subsetting a data set cont special operators1

Subsetting a Data Set (cont.)Special Operators

Special character operators to use with the WHERE option:

LIKE true if value is identical to the argument

where=(lastname like ‘Johnsson’);


Subsetting a data set cont special operators2

Subsetting a Data Set (cont.)Special Operators

The power of the LIKE operator is that you can use different types of matching arguments:

‘Smi%’matches all character values that begin

with “Smi”

‘%th’matches all character values that end

with “th”


Subsetting a data set cont special operators3

Subsetting a Data Set (cont.)Special Operators

‘Smi_’matches all character values that are

four characters long and begin with

“Smi”

‘Smi_ _’matches all character values that are

five characters long and begin with

“Smi”


Subsetting a data set cont special operators4

Subsetting a Data Set (cont.)Special Operators

Another special operator is CONTAIN.

CONTAIN yields true if any string of the value matches the argument.

where=(lastname contains ‘nss’);

All last names with the letters “nss” in them will be selected.


Subsetting a data set cont create a subset

Subsetting a Data Set (cont.)Create a Subset

Sometimes you want to make a new data set from a subset.

These are two ways to accomplish this:

data course.cases(where=(case_1=1));

set course.main;

run;

which will delete the redundant observations at the end of the data step.


Subsetting a data set cont create a subset1

Subsetting a Data Set (cont.)Create a Subset

data course.cases;

set course.main(where=(case_1=1));

run;

which will delete the redundant observations at the beginning of the data set.

However, a good advice is to minimise the number of data sets as much as possible. The fewer data sets the better! There is nearly always a procedure to use on the main data set instead of creating a new data set.


Subsetting a data set cont where statement

Subsetting a Data Set (cont.)WHERE statement

There is a third way too, the WHERE statement:

data course.cases;

set course.main; where (case_1=1);

run;

Note that no equal sign is used in the WHERE statement!

Apart from the equal sign, the WHERE statement works as the WHERE option.

May also be written if case_1=1;


Subsetting a data set cont obs and firstobs

Subsetting a Data Set (cont.)OBS and FIRSTOBS

In both data and proc steps you can use the OBS data option to subset a specific range of observations.

To subset the ten first observations:

proc print data=course.main(obs=10);

To subset observations 5 to 10:

proc print data=course.main(firstobs=5 obs=10);

Note that no WHERE is used!


Subsetting a data set cont subsetting if

Subsetting a Data Set (cont.)Subsetting IF

There is one occasion when a WHERE statement will not work, namely if you wish to condition on a variable which you create in that same data step.

Instead you can use a subsetting IF statement:

if (your-expression);


Subsetting a data set cont example

Subsetting a Data Set (cont.)Example

data course.less20yr;

set course.main;

age75=1975-birthyr;

if (age75 < 20);

run;

This will yield a data set where all the observations were younger than 20 yrs in 1975.


Subsetting a data set cont delete observations

Subsetting a Data Set (cont.)Delete Observations

It is possible to delete both observations and variables from a data set.

Observations are deleted through the DELETE statement:

if expression then delete;

(The IF-THEN statement is explained in detail later on.)


Subsetting a data set cont example1

Subsetting a Data Set (cont.)Example

data course.after65;

set course.main;

if birthyr<1965 then delete;

run;

This will yield a data set where all observations with birthyr<1965 have been deleted.


Subsetting a data set cont delete vs where vs if

Subsetting a Data Set (cont.)DELETE vs. WHERE vs. IF

The same result could have been obtained through a WHERE statement, or by a subsetting IF

if birthyr>=1965;

However, if variable BIRTHYR is created in the same data step, then you need to use IF or a WHERE data set option on the target data set:

data course.after65(where=(birthyr>65));

...;

run;


Subsetting a data set cont delete variables

Subsetting a Data Set (cont.)Delete Variables

To delete variables use either of the KEEP and DROP statements.

keep variable1 variable2 ... ;

drop variable1 variable2 ... ;

The KEEP statement will yield a data set where all variables NOT listed in the KEEP statement list are deleted.

The DROP statement will yield a data set where all variables listed in it are dropped.


Subsetting a data set cont delete variables1

Subsetting a Data Set (cont.)Delete Variables

You use KEEP and DROP in the same way as WHERE, either as a data set option, or as a statement in a data step.

Thus KEEP and DROP data set options can be used in procedures as well.

Be aware that you can NOT use both KEEP and DROP in the same data step.


Subsetting a data set cont examples1

Subsetting a Data Set (cont.)Examples

KEEP statement:

data course.half;

set course.main;

keep birthyr age case_1;

run;


Subsetting a data set cont examples2

Subsetting a Data Set (cont.)Examples

KEEP data set option:

data course.half(keep=birthyr age case_1);

set course.main;

run;

or

data course.half;

set course.main(keep=birthyr age case_1);

run;


If then else if then else

IF-THEN-ELSEIF-THEN-ELSE

Quite often one wishes to create a variable of the form

1 if age<30

X =

0 else

The value of X depends on the value of variable age.


If then else cont if then else statement

IF-THEN-ELSE (cont.)IF-THEN-ELSE Statement

To define the X variable in SAS is simple. The code is as follows:

data course.groups;

set course.main;

if (age<30) then X=1;

else X=0;

run;

Note, that since a missing value is interpreted as negative infinity, it will fall under X=1. More about this problem shortly.


If then else cont if then else statement1

IF-THEN-ELSE (cont.)IF-THEN-ELSE Statement

The general form of the IF-THEN-ELSE statement is

if expression1 then expression2;

else expression3;

Expression1 must be a logical expression yielding either true or false.

If expression1 is true than expression2 is calculated.

If expression1 is false then expression3 is calculated.


If then else cont example

IF-THEN-ELSE (cont.)Example

It is very useful to use several IF-THEN-ELSE statements in a series.

if (age<30) then generatn=1;

else

if (30<=age<60) then generatn=2;

else

if (60<=age) then generatn=3;

else generatn=.;


If then else cont example1

IF-THEN-ELSE (cont.)Example

Say that the age=67, then (age<30) will yield false, and we will go right to the ELSE line.

There the expression is a new IF-THEN-ELSE statement where we continue our execution.

(30<=age<60) is false too, and so we continue at the next ELSE line.

There the expression (60<=age) is true, and consequently we calculate the THEN-expression generatn=3.


If then else cont good programming

IF-THEN-ELSE (cont.)Good Programming

This definition of the variable generatn was perfect in the sense that it included all possible values of age in the IF-expressions.

A good IF-THEN-ELSE statement should!

If all the IF-expressions yield FALSE (that is, if you have not included all possible values of the conditioning variable), then the new variable will automatically be set to missing.


If then else cont good programming1

IF-THEN-ELSE (cont.)Good Programming

There should always be an IF-expression taking care of missing values of the conditioning variable (age in our example).

Usually the missing value option is put at the end of a long series of IF-THEN-ELSE statements, as the final ELSE-statement.

However, this is not always appropriate. In our example age<30 will be true for a missing value of age (missing = negative infinity).


If then else cont good programming2

IF-THEN-ELSE (cont.)Good Programming

This problem is avoided if an extra IF-THEN-ELSE statement is added at the beginning:

if (age eq .) then generatn=.;

else

if (age<30) then generatn=1;

else

if (30<=age<60) then generatn=2;

else generatn=3;

This will prevent any missing values from falling under the expression (age<30).


If then else cont else statement

IF-THEN-ELSE (cont.)ELSE Statement

Question:

What happens if I don’t use the ELSE statement in the IF-THEN statement?

I.e.

if (age eq .) then generatn=.;

if (0<age<30) then generatn=1;

if (30<=age<60) then generatn=2;

if (60<=age) then generatn=3;


If then else cont else statement1

IF-THEN-ELSE (cont.)ELSE Statement

Answer:

Nothing. The result is the same as before.

However, the ELSE statement is a far better alternative when it comes to complicated IF-THEN statements (see the next picture).

Make a habit of using the ELSE statement.


If then else cont else statement2

IF-THEN-ELSE (cont.)ELSE Statement

The difference between IF-THEN and IF-THEN-ELSE:

As soon as an IF statement is found to be true, the following IF statements in the series of IF-THEN-ELSE statements are not calculated.

If you only use IF-THEN statements, all IF statements will be checked, even after you have found the true one.

Have you by any chance not made them exclusive of each other, the latest assignment will be in effect.


If then else cont else statement3

IF-THEN-ELSE (cont.)ELSE Statement

Example of a faulty definition:

if (age eq .) then generatn=.;

if (age<30) then generatn=1;

if (age<60) then generatn=2;

if (60<=age) then generatn=3;

All persons younger than 60 years will be have generatn=2!


If then else cont do end

IF-THEN-ELSE (cont.)DO-END

If you wish to assign several variables in a IF-THEN-ELSE statement you can use a DO-END statement.

data course.main;

set course.original;

if (age eq .) then

do;

generatn=.;

credit=.;

end;

else

if (age<30) then

do;

generatn=1;

credit=1;

end;

...;

run;


If then else cont do end1

IF-THEN-ELSE (cont.)DO-END

The general form of the DO-END statement is

IF expression THEN

DO;

statements

END;

ELSE

DO;

statements

END;


Set and merge joining data sets

Set and MergeJoining Data Sets

Previously we have taken a subset of a data set. It is also possible to join (concatenate and merge) data sets together.

Concatenate= adding observations (“lægge dataset efter hinanden”)

Merge = adding variables, match data sets (“lægge dataset ved siden af hinanden”)


Set and merge cont set

Set and Merge (cont.)SET

To concatenate two or several data sets, use the SET statement.

data SAS-data-set;

set data-set1 data-set2 data-set3 ...;

run;

The top observations in the SAS-data-set will be from data-set1, the next observations from data-set2 and so on.


Set and merge cont set1

Set and Merge (cont.)SET

If there are different variables in the data sets, missing values will be generated for the observations which did not have the variables before the concatenation..

Variables not present in all the data sets will be found to the far right of the concatenated data set.


Set and merge cont example

Set and Merge (cont.)Example

We wish to add three observations to our data set course.main. The three observations are stored in data set course.extraobs.

data course.concatenate;

set course.main course.extraobs;

run;


Set and merge cont example1

Set and Merge (cont.)Example

Concatenate.sas7bdat:

OBS ID BIRTHYR WEIGHT AGE BMI ALDER

1 001 1954 62 43 22.77319 .

2 002 1956 68 41 24.38237 .

3 003 1956 65 41 21.97134 .

4 004 1962 56 35 19.84127 .

5 005 1954 58 43 22.94213 .

6 006 1953 52 44 19.81405 .

7 007 1955 69 42 22.53061 .

8 008 1955 75 42 25.05931 .

9 009 1960 82 37 28.3737 . course.main

10 010 1962 68 35 22.9854 .

11 011 1961 65 36 23.03005 .

12 012 1954 62 43 21.70792 .

13 013 1956 58 41 20.54989 .

14 014 1962 61 35 22.67995 .

15 015 1958 58 39 21.82995 .

16 016 1959 62 38 22.77319 .

17 017 1962 59 35 21.93635 .

18 018 1957 73 40 22.53086 .

19 029 . . . 28.59 48

20 030 . . . 22.14 51 course.extraobs

21 031 . . . 23.85 42


Set and merge cont example2

Set and Merge (cont.)Example

Since alder = age it would be nice to transform the three alder values to age values. Use a RENAME data set option.

rename=(old-var-name=new-var-name);

In the example:

data course.concatenate;

set course.main

course.extraobs(rename=(alder=age));

run;


Set and merge cont example3

Set and Merge (cont.)Example

OBS ID BIRTHYR WEIGHT AGE BMI

1 001 1954 62 43 22.77319

2 002 1956 68 41 24.38237

3 003 1956 65 41 21.97134

4 004 1962 56 35 19.84127

5 005 1954 58 43 22.94213

6 006 1953 52 44 19.81405

7 007 1955 69 42 22.53061

8 008 1955 75 42 25.05931

9 009 1960 82 37 28.3737 course.main

10 010 1962 68 35 22.9854

11 011 1961 65 36 23.03005

12 012 1954 62 43 21.70792

13 013 1956 58 41 20.54989

14 014 1962 61 35 22.67995

15 015 1958 58 39 21.82995

16 016 1959 62 38 22.77319

17 017 1962 59 35 21.93635

18 018 1957 73 40 22.53086

19 029 . . 48 28.59

20 030 . . 51 22.14 course.extraobs

21 031 . . 42 23.85


Set and merge cont merge

Set and Merge (cont.)MERGE

To merge or match two or several data sets, use the MERGE statement:

data SAS-data-set;

merge data-set1 data-set2 data-set3 ... ;

by variable;

run;

The first variables in the SAS-data-set will be from data-set1, the next variables from data-set2 etc.


Set and merge cont merge1

Set and Merge (cont.)MERGE

The BY variable (matching variable) is usually an identification variable unique to all observations and present in all data sets to be merged.

The data sets must be sorted according to the matching variable before MERGE is used.

If there are different observations in the data sets, missing values will be generated for observations not present in other data sets. These observations will be found at the end of the merged data set.


Set and merge cont example4

Set and Merge (cont.)Example

We wish to add a variable to our data set course.main.

The new variable is the number of children each woman has, variable CHILD.

This variable is stored together with the variable ID in a data set called course.extravar. ID is our matching variable, unique for all observations.


Set and merge cont example5

Set and Merge (cont.)Example

proc sort data=course.main course.extravar;

by id;

run;

proc sort data=course.extravar;

by id;

run;

data course.merged;

merge course.main course.extravar;

by id;

run;


Set and merge cont example6

Set and Merge (cont.)Example

merged.sas7bdat:

OBS ID BIRTHYR WEIGHT AGE BMI CHILD

1 001 1954 62 43 22.7732 1

2 002 1956 68 41 24.3824 0

3 003 1956 65 41 21.9713 2

4 004 1962 56 35 19.8413 2

5 005 1954 58 43 22.9421 0

6 006 1953 52 44 19.8141 3

7 007 1955 69 42 22.5306 1

8 008 1955 75 42 25.0593 2

9 009 1960 82 37 28.3737 4

10 010 1962 68 35 22.9854 .

11 011 1961 65 36 23.0300 2

...

17 017 1962 59 35 21.9363 2

18 018 1957 73 40 22.5309 0

19 029 . . 48 28.59 0

20 030 . . 51 22.14 3

21 031 . . 42 23.85 1

course.main course.extravar


Read raw data into sas raw data

Read Raw Data Into SASRaw Data

Sometimes you have a raw data file you want to read into SAS. The data are not stored in a SAS data set, or .sas7bdat file.

Perhaps data are of the form:

001 Anderson 1954 62 43

002 Askelund 1956 68 41

003 Bengtson 1956 65 41

004 Carner 1962 56 35

005 Holmfors 1954 58 43

006 Johnsson 1953 52 44

...

016 Torberg 1959 62 38

017 Turner 1962 59 35

018 Öhlund 1957 73 40


Read raw data into sas cont list input format

Read Raw Data Into SAS (cont.)List Input Format

This form of data is called list input format. It means that each data value is separated by a blank.

List input formatted data have the following characteristics:

  • all values contain eight or fewer characters

  • values are separated by one or several blanks

  • missing values (both numeric and character) must be written as a single period (.)

  • numerical values must include all necessary decimal points


Read raw data into sas cont input and cards

Read Raw Data Into SAS (cont.)INPUT and CARDS

To create a data set using list input formatted data, write

data course.nameincl;

input id $ name $ birthyr weight age;

cards;

001 Anderson 1954 62 43

002 Askelund 1956 68 41

003 Bengtson 1956 65 41

...

016 Torberg 1959 62 38

017 Turner 1962 59 35

018 Öhlund 1957 73 40

;

run;


Read raw data into sas cont input and cards1

Read Raw Data Into SAS (cont.)INPUT and CARDS

In the INPUT statement you list the variables in the same order as they are presented.

If a variable is a character variable, put a dollar sign ($) after the name.

When there is no dollar sign, then SAS interprets the variable as a numeric.


Read raw data into sas cont input and cards2

Read Raw Data Into SAS (cont.)INPUT and CARDS

The CARDS statement indicates that data lines are to follow.

It is written below the INPUT statement.

Note that no semicolons are present at the end of each data line, ONLY at the end of all the data. It marks the end of data entry.

Equivalent to CARDS is the keyword DATALINES.


Read raw data into sas cont column input format

Read Raw Data Into SAS (cont.)Column Input Format

If the data you wish to read into a data set is specified in columns (= positions), you may use the column input format.

001 Andersson 1954 62 43

002 Askelund 1956 68 41

003 Bengtsson 1956 65 41

004 Carner 1962 56 35

005 Holmfors 1954 58 43

006 Johansson 1953 52 44

...

016 Torberg 1959 62 38

017 Wernersson 1962 59 35

018 Öhlund 1957 73 40

----+----1----+----2----+----3


Read raw data into sas cont column input format1

Read Raw Data Into SAS (cont.)Column Input Format

Each variable occupies a specified number of characters or positions (1 position = 1 column).

The first variable ID is in columns 1-3, NAME in 5-12 (including blanks at the end), BIRTHYR in 13-16 etc.

The ruler below the data is a SAS ruler. It is often used in the Log window.

Each column is indicated by “-”. The digit 1 indicates column 10, 2 column 20 etc. The + indicates columns 5, 15, 25 etc.


Read raw data into sas cont column input format2

Read Raw Data Into SAS (cont.)Column Input Format

To create a data set using column input formatted data, write

data course.nameincl;

input id $ 1-3 name $ 5-12

birthyr 13-16 weight 18-19 age 21-22;

cards;

001 Andersson 1954 62 43

002 Askelund 1956 68 41

003 Bengtsson 1956 65 41

...

016 Torberg 1959 62 38

017 Wernersson 1962 59 35

018 Öhlund 1957 73 40

;

run;


Read raw data into sas cont column vs list input format

Read Raw Data Into SAS (cont.)Column vs. List Input Format

The only difference from the code for list input formatted data is that you must specify the columns in the INPUT statement. (ID $ 1-3, name $ 5-12, etc.)

Differences in data characteristics:

  • values must not contain more characters than are specified in the input statement

  • missing values are written as blank for character variable, period (.) for numerical


Read raw data into sas cont data from txt file

Read Raw Data Into SAS (cont.)Data from .TXT File

To read data (list or column formatted) from external text files (.txt) use an INFILE statement.

data course.nameincl;

infile ‘h:\SAS-folder\data-file.txt’;

input id $ 1-3

name $ 5-12

birthyr 13-16

weight 18-19

age 21-22

;

run;


Read raw data into sas cont data from txt file1

Read Raw Data Into SAS (cont.)Data from .TXT File

The INPUT statement is written as before, depending on whether you have column or list formatted data in the text file.

No CARDS (or DATALINES) statement is used.

The INFILE statement must be written above the INPUT statement.


The sas manuals the sas manuals

The SAS ManualsThe SAS Manuals

There is a heap of heavy manuals for SAS, often not easy to read for a beginner but indispensable when you get more experienced.

These are the most useful of them:

SAS Language, Reference

SAS Procedures Guide

To lower the threshold of SAS manuals, you can begin by reading the thin (!) SAS Language and Procedures, Introduction. Most of its contents are presented in this guide, so it is easy to compare.


Importing and exporting files

Importing and Exporting Files

It is also possible to import data from other systems file formats, including Excel and Access, as well as delimited file types.

Use the entries on the Files menu for this.

Or use the program STAT/transfer.


The sas manuals cont manuals vs online help

The SAS Manuals (cont.)Manuals vs. Online Help

The SAS manuals/Online Documentation work similarly to the Online Help (see chapter The Online HELP).

The Online Help is easy to use when you know what you are looking for, a specific syntactic problem, options or keywords.

The manuals are best used when you are looking for a solution to a bigger SAS problem, such as which procedure to use, why does it not work as I thought it would, or what is SAS actually doing.


Comments commenting programs

CommentsCommenting Programs

Comments in SAS programs are code that will not be executed when the program is submitted.

It is often useful to comment out parts of a program.

Also it is good to add comments which explain the code, such as new variables, dates at changes, general purpose of the program, etc.


Comments cont general comment

Comments (cont.)General Comment

Comments in programs should be inside /* and */ .

/* comment */

It is NOT possible to have comments within comments

/* comment /* comment */ comment */


Comments cont statement comment

Comments (cont.)Statement Comment

Another way to comment out text is to use a *.

*age=1996-birthyr;

All text after the * and until a semicolon is reached will be commented.

This will avoid the comment-inside-comment problem, but it is more cumbersome, since you must comment every statement.


Comments cont run cancel

Comments (cont.)RUN CANCEL

If you wish to prevent a data step or procedure from being executed you can change the RUN statement to

RUN CANCEL;


Naming data sets and variables data set and variable names

Naming Data Sets and VariablesData Set and Variable Names

The name of a data set or a variable should reflect its contents.

It is not obvious to an outsider that the variable “rel51yrd” refers to “relapse 5 years after first diagnosis”. A better name would perhaps be “relapse5”.

Add comments in the data step to clarify what the variables are!


Naming data sets and variables cont data set and variable names

Naming Data Sets and Variables (cont.)Data Set and Variable Names

  • can be 32 characters (letters, underscores and digits) long at most

  • can be uppercase (versaler) or lowercase (gemena) or mixed (OrIgInAl = oRiGiNaL)

  • must start with a letter (A-Z) or an underscore (_), not a digit


Naming data sets and variables cont data set and variable names1

Naming Data Sets and Variables (cont.)Data Set and Variable Names

The increased character limit to 32 characters in version 8.0 is welcomed by many SAS users (formerly it was only 8 characters in version 6.12).

But a long name is not always to prefer either. It makes the code tiresome to read, not to mention write.

A good idea is therefore to try to keep to a maximum of 8 to 12 characters also in future. Use labels.


Naming programs program file names

Naming ProgramsProgram File Names

Contrary to the SAS data set and variable names, there is no limit to 32 characters when you name the program files.

Do not hesitate to use long names, although the same rules as for the variables names apply.

Be consistent, no cryptic abbreviations.


Categorising variables dichotomous variables

Categorising VariablesDichotomous Variables

The most common categorising is dichotomization, a variable that only takes two values, i.e. woman/man, case/control, etc.

Dichotomised variables should take values 1 and 0

(not 1 and 2).

Example: 1 if overweight

0 if not


Categorising variables cont dichotomous variables

Categorising Variables (cont.)Dichotomous Variables

Dichotomous variables are often called indicator variables, since the 1 indicate a characteristic and the 0 does not.

It is therefore clever to name dichotomous variables after the “1” characteristic.

Example:

1 if BMI>25

fat =

0 otherwise


Categorising variables cont defining dichotomous variables

Categorising Variables (cont.)Defining Dichotomous Variables

This is a how you may define the 1/0 variable in a data step:

data course.main;

set course.original;

if (bmi ne .) then

do;

if bmi>25 then fat=1;

else fat=0;

end;

else fat=.;

run;

Or simply fat=(bmi>25); ....but


Categorising variables cont dummy variables

Categorising Variables (cont.)Dummy Variables

Some procedures can not use variables with several categories, only dichotomised, so you need to create 1/0 variables (dummy variables) for each category.

Example:

A BMI variable is categorised into three categories, using a format:

< 20

bmi = 20-25

> 25


Categorising variables cont dummy variables1

Categorising Variables (cont.)Dummy Variables

The three new dummy variables should have bmi as a prefix:

1 if bmi<20 1 if 20<bmi<25 1 if bmi>25

bmi1= bmi2= bmi3=

0 else 0 else 0 else

With the same prefix as the mother variable it will be very easy to understand their origin.

The suffices 1, 2 and 3 are much easier to comprehend than say bmilow, bmimiddl and bmihigh.


Categorising variables cont example

Categorising Variables (cont.)Example

This is one way to define dummy variables in a data step:

data course.main;

set course.original;

if (bmi ne .) then /* Checks for non-missing values */

do;

if bmi<20 then bmi1=1;

else bmi1=0;

if (20<=bmi<=25) then bmi2=1;

else bmi2=0;

if bmi>25 then bmi3=1;

else bmi3=0;

end;

else /* If missing value */

do;

bmi1=.;

bmi2=.;

bmi3=,;

end;

run;


Categorising variables cont example to avoid

Categorising Variables (cont.)Example to Avoid

This is BAD way to define dummy variables (well, Anna doesn’t like it….):

data course.main;

set course.original;

if (bmi ne .) then

do;

bmi1=(bmi<20);

bmi2=(20<=bmi<=25);

bmi3=(bmi>25);

end;

run;


Categorising variables cont example to avoid1

Categorising Variables (cont.)Example to Avoid

First of all, what does it mean?

Expressions (bmi<20), (20<=bmi<=25) and (bmi>25) are logical expressions. They are either TRUE or FALSE.

SAS interprets the logical value TRUE as the numeric value 1, and FALSE as 0.

So, if bmi<20 say, then bmi1=1, bmi2=0 and bmi3=0.


Organise your programming structuring sas files

Organise Your ProgrammingStructuring SAS Files

Here is a suggestion of how to organise your SAS programs.

It is not presumed to be The Ultimate Structure, but it is a good start to get organised.

All ideas to improve this structure are most welcome.


Organise your programming cont we want to avoid this

Organise Your Programming (cont.)We Want To Avoid This

The most common SAS programming is:

You make a program file which begins with a data step reading original.sas7bdat. In the data step you do the changes and adds, create formats, and then the procs follow using this data set.

The reason why people write programs like this is probably because it is the natural way to do it. First data, then analysis.


Organise your programming cont we want to avoid this1

Organise Your Programming (cont.)We Want To Avoid This

The problem with this programming is:

All programs create new data sets.

So whenever you want to update the data you must update ALL the program files.

Eventually you will not know which ones you have updated, and which you have not.


Organise your programming cont our idea

Organise Your Programming (cont.)Our Idea

To avoid this time consuming dilemma, I suggest the following file structure:

Separate the data steps from the procs by saving them to different files. Data files and analysis files.

Preferably, create only ONE main data set where you make ALL your changes, and then use it in every proc file.

Whenever you make a change to the data, all the analyses are changed accordingly (when rerun).


Organise your programming cont data files

Organise Your Programming (cont.)Data Files

Four files handles the data:

the original data file saved as a

1..sas7bdat file

the data set programming is in

2.main.sas

which creates

3.main.sas7bdat

formats are created by code in the file

4.formats.sas


Organise your programming cont analysis files

Organise Your Programming (cont.)Analysis Files

Other files can be divided into three groups:

  • data cleaning files

  • analysis files

  • published analysis files

    However, these groups are not clearly defined. Some files could fall into several groups.

    We have yet to come up with a clever structure of these files. Suggestions are kindly accepted.


Organise your programming cont organising the data files

Organise Your Programming (cont.)Organising the Data Files

The original data file (a SAS data set or an external data base file), should NOT be used when you run SAS.

SAS executions may go wrong and create permanent changes to the data.

Start by making a copy of the original data file.


Organise your programming cont sas7bdat file

Organise Your Programming (cont.).sas7bdat File

If the original data file is a .sas7bdat file, save the copy as copy-of-name.sas7bdat (or a similar name).


Organise your programming cont non sas file

Organise Your Programming (cont.)Non-SAS File

If the original data file is not a SAS data file (.sas7bdat) then you should create a SAS data file (name.sas7bdat) from it.

This is not always trivial and you may need help from the Analysis group.

Golden Rule: NEVER try to import THE original file. Always make another COPY of it before you import it to SAS. Imports can crash.


Organise your programming cont do not alter original data

Organise Your Programming (cont.)Do Not Alter Original Data

You now have the original data stored as the SAS data file name.sas7bdat.

Golden Rule No 2: The file name.sas7bdat should ONLY be altered whenever the original data file is altered!

It is an exact copy of the original data and should remain so.


Organise your programming cont main sas

Organise Your Programming (cont.)Main.sas

From name.sas7bdat we create the data set main.sas7bdat in the program called main.sas. This is our version of main.sas:

/***

your-libname.main creates the main.sas7bdat data set for project ‘project-name’.

Created: 2001-08-07 by Anna Johansson

Used by: all analysis files, otherwise mentioned

***/

data your-libname.main;

set your-libname.name;

/* Changes and adds (new variables) */

continues ...


Organise your programming cont main sas1

Organise Your Programming (cont.)Main.sas

/* Assign variable labels */

label var1=‘label1’

var2=‘label2’

...;

/* Assign formats */

format var1format1.

Var2format2.

...;

run;

proc contents data=your-libname.main;

run;

proc print data=your-libname.main;

run;

/* END of main.sas */


Organise your programming cont main sas2

Organise Your Programming (cont.)Main.sas

The comment header of main.sas includes some general information. It is optional, but very useful.

The data set your-libname.main is created from your-libname.name in the data step that follows.

Changes and adds to the original data are made.

At the end, labels and formats are assigned to the variables. These lists become longer as the work progress.


Organise your programming cont main sas3

Organise Your Programming (cont.)Main.sas

After the data step, two procedures, CONTENTS and PRINT, are found.

The logs and outputs they generate are used to check whether the main.sas7bdat data set was created satisfactorily.

If you have a large data set it might be a good idea to put a restriction to the PRINT procedure, so that only a fraction of the data is printed.

proc print data=course.main(obs=20);


Organise your programming cont formats sas

Organise Your Programming (cont.)Formats.sas

The formats assigned at the end of the main data step are created in a file we call formats.sas:

proc datasets library=your-libname memtype=(catalog);

delete formats / memtype=catalog;

run;

proc format library=your-libname fmtlib;

value bmif low-20.0=‘Underweight’

20.0-25.0=‘Normal weight’

25.0-high=‘Overweight’

other=‘Other’;

continues ...


Organise your programming cont formats sas1

Organise Your Programming (cont.)Formats.sas

value case_1f 0=‘Case’

1=‘Control’

other=‘Other’;

value yes10no 0=‘No’

1=‘Yes’

.=‘Missing’;

run;

/* END of formats.sas */


Organise your programming cont formats sas2

Organise Your Programming (cont.)Formats.sas

All formats are saved permanently to a format catalogue (see section on Formats).

The first part (proc datasets) will delete the existing SAS format catalogue.

Make sure the occurrences of your-libname are correct.


Organise your programming cont formats sas3

Organise Your Programming (cont.)Formats.sas

The formats are created by proc FORMAT. Here the format names are bmif, case_1f and yes10no.

The name should reflect the variable it refers to.

Occasionally there are more general formats like “yes10no” (0=‘No’, 1=‘Yes’). They may have a general name and be assigned to several variables.

A good idea is to place the format definitions in alphabetical order in formats.sas.


Organise your programming cont working with several formats

Organise Your Programming (cont.)Working With Several Formats

Often you switch between several formats for the same variable, for instance when you are checking different ways to divide categories.

Do not use one format which you change and redefine, but several formats and change the assignment of them (either in main.sas or in the proc you are using, see the next picture).

These formats should have similar names, like bmi3grp, bmi5grp, bmi7grp. (Note that a format name can not end with a digit.)


Organise your programming cont working with several formats1

Organise Your Programming (cont.)Working With Several Formats

The permanent assignment in main.sas should reflect the format you wish to keep as the permanent. It is possible to temporarily change a format in ANY procedure by adding a FORMAT statement.

Example:

proc freq data=course.main;

tables bmi*case_1;

format bmi bmi5grp.;

run;


Organise your programming cont working with several formats2

Organise Your Programming (cont.)Working With Several Formats

To delete all format assignments simply assign no format:

proc freq data=course.main;

tables bmi*case_1;

format bmi ;

run;


Organise your programming cont when must i rerun main sas

Organise Your Programming (cont.)When Must I Rerun Main.sas?

Whenever you have

  • made a change to the data step, or

  • changed the permanent format assignment in main.sas

    you must rerun main.sas.

    You do not need to rerun main.sas when you have changed the formats.sas, UNLESS you have also changed the assignments in main.sas.


Organise your programming cont organising the analysis files

Organise Your Programming (cont.)Organising the Analysis Files

Organising the proc files (analysis files) is more complicated.

How are they naturally grouped?

By the tables presented in the article?

By the different procs (MEANS in one file, FREQ in another)?

The only advise I have is to give the files good names, and perhaps add dates in comments. And use comments, since the proc files are the documentation of your analysis work.


Organise your programming cont documentation

Organise Your Programming (cont.)Documentation

Remember:

The SAS files are the documentation of your data analysis.

Documentation should be easy to access and understand.

You should be able to reproduce all published results from the original data file.


Appendix list of useful procedures

AppendixList of Useful Procedures

PROC APPEND = concatenates data sets (compare SET statement)

PROC CATALOG = administrates SAS Catalogues

PROC CHART = creates simple charts, bar charts, block charts, pie charts, etc.

PROC COMPARE = compares the contents of two data sets, or variables within a data set

PROC CONTENTS = prints the descriptive part of a data set

PROC COPY = copies a SAS Data Library or parts of it

PROC CORR = calculates correlation coefficients

PROC DATASETS = lists, renames, deletes data sets etc.


Appendix cont list of useful procedures cont

Appendix (cont.)List of Useful Procedures (cont.)

PROC FORMAT = defines formats and informats permanently or temporarily

PROC FREQ = creates frequency tables and basic statistical tests

PROC MEANS = creates descriptive statistics

PROC OPTIONS = lists all current SAS System options values

PROC PLOT = creates simple Y-X plots

PROC PRINT = prints the data part of a data set

PROC PRINTTO = defines destination of output and log, e.g. when you wish to use an output as input for another procedure


Appendix cont list of useful procedures cont1

Appendix (cont.)List of Useful Procedures (cont.)

PROC RANK = calculates ranking of different sorts

PROC REPORT = creates better layout for output

PROC SORT = sorts data sets by one or several variables

PROC SQL = implementing query language SQL, used to retrieve data sets

PROC STANDARD = standardises variables to a given mean and variance

PROC SUMMARY = descriptive statistics in form of summations

PROC TABULATE = descriptive statistics in tables

PROC TRANSPOSE = transposes data sets, i.e. transforms rows to columns, and vice versa


Appendix cont list of useful procedures cont2

Appendix (cont.)List of Useful Procedures (cont.)

PROC UNIVARIATE = creates descriptive distribution statistics and plots, histograms etc.


Acknowledgements

Acknowledgements

Anna wishes to thank the following MEP colleagues for their support.

Hanna Svensson, Statistician, Doctoral Student

Lena Brandt, Statistician

Paul Dickman, Senior Statistician

Gunnar Petersson, Programmer

-- and Peter and Lene wish to thank Anna!!


  • Login