programming guidelines

4
7/21/2019 Programming Guidelines http://slidepdf.com/reader/full/programming-guidelines-56dbaa5ed9f56 1/4 Programming Guidelines SAS Syntax  A SAS program is written in the Editor  window and contains a series of statements that tell SAS what to do (e.g., import a dataset, give a frequency count of a variable). ou can save your program so that it can be edited and reused after it!s written. SAS syntax  is the set of rules that dictate how your program must be written in order for SAS to understand it. "here are some conventions of SAS synta# that new users should $now before getting started. Semicolons %very statement must end with a semicolon. "his generally corresponds to every line ending in a semi&colon, but sometimes your commands or statements will be more than one line and a semicolon is only necessary at the end of the statement. 'mitting the semicolon is the most common mista$e new users ma$e.  Quotation Marks SAS recognies te#t as long as it is enclosed in quotation mar$s (te#t*) or apostrophes (+te#t!). t doesn!t matter which one you choose, but be sure each te#t bloc$ starts and ends with the same one. ou will need to enclose te#t in quotation mar$s or apostrophes if you need to reference values of a character variable, reference a file directory, or assign a title to your output, to name a few e#ample. ou $now you have properly typed te#t values when SAS changes the color of the words into a purplish&pin$ color. 'ne word of caution- if your te#t contains an apostrophe, then you must enclose it with quotation mar$s. "he e#ample te#t string below shows that SAS recognies the first two lines as te#t (indicated by the coloring), but only recognies part of the te#t as such in the third line. Formatting SAS is more rela#ed than other synta#es when it comes to capitaliation, indentation, and line brea$s.

Upload: rmayreddy

Post on 06-Mar-2016

5 views

Category:

Documents


0 download

DESCRIPTION

Programming Guidelines

TRANSCRIPT

Page 1: Programming Guidelines

7/21/2019 Programming Guidelines

http://slidepdf.com/reader/full/programming-guidelines-56dbaa5ed9f56 1/4

Programming GuidelinesSAS Syntax

 A SAS program is written in the Editor  window and contains a series ofstatements that tell SAS what to do (e.g., import a dataset, give a frequencycount of a variable). ou can save your program so that it can be edited and

reused after it!s written.

SAS syntax  is the set of rules that dictate how your program must be written inorder for SAS to understand it. "here are some conventions of SAS synta# thatnew users should $now before getting started.

Semicolons

%very statement must end with a semicolon. "his generally corresponds to everyline ending in a semi&colon, but sometimes your commands or statements will bemore than one line and a semicolon is only necessary at the end of thestatement. 'mitting the semicolon is the most common mista$e new users ma$e.

 

Quotation Marks

SAS recognies te#t as long as it is enclosed in quotation mar$s (te#t*) orapostrophes (+te#t!). t doesn!t matter which one you choose, but be sure eachte#t bloc$ starts and ends with the same one. ou will need to enclose te#t inquotation mar$s or apostrophes if you need to reference values of a charactervariable, reference a file directory, or assign a title to your output, to name a fewe#ample. ou $now you have properly typed te#t values when SAS changes thecolor of the words into a purplish&pin$ color. 'ne word of caution- if your te#t

contains an apostrophe, then you must enclose it with quotation mar$s. "hee#ample te#t string below shows that SAS recognies the first two lines as te#t(indicated by the coloring), but only recognies part of the te#t as such in the thirdline.

Formatting 

SAS is more rela#ed than other synta#es when it comes to capitaliation, indentation, and line brea$s.

Page 2: Programming Guidelines

7/21/2019 Programming Guidelines

http://slidepdf.com/reader/full/programming-guidelines-56dbaa5ed9f56 2/4

• ppercase and lowercase letters are recognied as the same (i.e., SAS is not case&sensitive)

• ndentions or spacing before a statement are ignored

• %#tra lines between statements are ignored

• /ultiple statements on the same line are o$ay, provided they are separated by a semicolon

•  A statement can span more than one line, as long as it ends with a semicolon

0owever, most SAS programmers use capital letters, indentions, and spacing ina way that ma$es it easier for themselves and other users to read andunderstand the program. 0ere is an e#ample of how a typical program would beset up, ma$ing use of indentions and spacing, one statement per line, and capitalletters.

Commentst is good practice to include notes in your program, called comments. 1ommentsare ignored by SAS during e#ecution && they e#ist solely for the benefit of theperson reading the SAS code.

1omments ma$e a writtenprogram more understandable by documenting what is going on in the program

and the purpose of the statements. A well&commented program can help thecreator remember what was done and why. t also helps other users decipherwhat a program is doing. 1omments can be inserted into the program using twoformats, both of which will appear as green te#t in the Editor -

*This is what a comment looks like;

Page 3: Programming Guidelines

7/21/2019 Programming Guidelines

http://slidepdf.com/reader/full/programming-guidelines-56dbaa5ed9f56 3/4

/*This is what a comment looks like*/

The Data Step vs. The Proc Step

 A SAS program is organied into bloc$s of code, called steps. Specifically,the data step is where data creation and manipulation ta$es place, andthe proc  (or procedure) step is where statistical analysis ta$es place.

Statements that occur outside of the data step or proc steps are called globalstatements. 2hen global statements are e#ecuted, their effects are held until theend of the SAS session. 'ne e#ample is the OPTIONS statement, which allows

the user to customie the headings that print when output is generated (amongother things). Another e#ample is theLIBNAME statement, which defines a SAS

library.

%very data step begins with the word DATA  and every proc step begins with the

word PROC. 3or the most part, all data and proc steps should end with

a RUN statement. "he RUN statement tells SAS to e#ecute the preceding step.

"he proc step in the e#ample program above uses the P45" procedure. P4'1P45" is 6ust one of the many different procedures in SAS. %ach procedure hasits own $eywords, options, and language rules.

n contrast, there is only one $ind of data step. "he data step always follows thefollowing format-

DATA  Dataset-Name (OPTIONS );

  .

.

.

RUN;

n the synta# above, DATA  is the $eyword that starts the data step. Dataset-

Page 4: Programming Guidelines

7/21/2019 Programming Guidelines

http://slidepdf.com/reader/full/programming-guidelines-56dbaa5ed9f56 4/4

Name is the name of the dataset that you want to create or manipulate. f you

want to add any of the dataset options, they would go in the parentheses afteryou name the dataset. n between the first and last lines you put your statementsthat create and manipulate the dataset. 5ote the data step ends witha RUN statement and a semicolon.

 

Running a Program

Running a program

 

 After a program is written, clic$ on the running man icon in the toolbar or press37 on your $eyboard to e#ecute your program. Alternatively, if you wish to onlyrun a specific portion of your program, you can highlight the relevant lines ofcode and then clic$ on the running man (or press 37).