Vous êtes sur la page 1sur 5

SAS® Programming 2: Data Manipulation Techniques

Quick Reference

Syntax

Create Multiple Data Sets BY-Group Processing in the DATA Step


DATA <SAS-data-set-name …
DATA output-SAS-data-set;
SAS-data-set-name-n>;
SET input-SAS-data-set;
BY BY-variable …;
<additional SAS statements>
Explicit Output RUN;

OUTPUT <SAS-data-set(s)>;
Specify FIRST. and LAST. Variables

Conditional Processing FIRST.BY-variable


LAST.BY-variable
SELECT <(select-expression)>;
WHEN-1 (when-expression-1
<…, when-expression-n>) Column Input
statement;
WHEN-n (when-expression-1
INPUT variable <$> startcol-endcol . . . ;
<…, when-expression-n>)
statement;
<OTHERWISE statement;>
END; Formatted Input

INPUT column-pointer-control variable


SAS Data Set Options informat …;
INPUT @n variable informat …;
INPUT +n variable informat …;
SAS-data-set(KEEP=variable-1 <variable-2 …
variable-n>)
SAS-data-set(DROP=variable-1 <variable-2 …
variable-n>) INFILE Statement Options
SAS-data-set(OBS=n)
SAS-data-set (FIRSTOBS=n) INFILE 'raw-data-file' DSD;
INFILE 'raw-data-file' MISSOVER;
INFILE 'raw-data-file' END=variable;
Prevent Reinitialization of a Variable

RETAIN variable-name <initial-value> …; Single and Double Trailing @


INPUT Specifications

Create an Accumulating Variable INPUT specifications … @;


INPUT specifications … @@;
variable + expression;

1
Copyright © 2010 SAS Institute Inc., Cary, NC, USA. All rights reserved.
SAS® Programming 2: Data Manipulation Techniques

SET Statement Options


Descriptive Statistics Functions
SET SAS-data-set END=variable <options>;
SUM(argument-1,argument-2,…,argument-n)
MEAN(argument-1,argument-2,…,argument-n)
Concatenation Functions MIN(argument-1,argument-2,…,argument-n)
MAX(argument-1,argument-2,…,argument-n)
CAT(string-1, … ,string-n) N(argument-1,argument-2,…,argument-n)
CATS(string-1, … ,string-n) NMISS(argument-1,argument-2,…,argument-n)
CATT(string-1, … ,string-n) CMISS(argument-1,argument-2,…,argument-n)
CATX(separator, string-1, … ,string-n)

Array Function
Concatenation Operator
DIM(array_name)
string1 !! string2

Display Messages and Variable Values


Character Functions in the Log

CHAR(string,position) PUTLOG <specifications>;


COMPRESS(source<,chars>) PUTLOG 'text';
FIND(string,substring<,modifiers,start>) PUTLOG variable-name=;
LEFT(argument) PUTLOG variable-name=format-namew.;
LENGTH(argument) PUTLOG _ALL_;
LOWCASE(argument)
PROPCASE(argument <,delimiter(s)>)
RIGHT(argument) DEBUG Option in the DATA Statement
SCAN(string,n<,'delimiter(s)'>)
STRIP(argument)
SUBSTR(string,start<,length>) DATA data-set-name / DEBUG;
TRANWRD(source,target,replacement)
TRIM(argument)
UPCASE(argument) Iterative DO Loops

DO index-variable=start TO stop
Truncation Functions <BY increment>;
iterated SAS statements
ROUND(argument<,round-off-unit>) END;
CEIL(argument) DO index-variable=item-1 <,…item-n>;
FLOOR(argument) iterated SAS statements
INT(argument) END;

Conversion Functions

INPUT(source,informat)
PUT(source,format)

2
SAS® Programming 2: Data Manipulation Techniques

Join Tables with PROC SQL


Conditional DO Loops
PROC SQL;
SELECT column-1, column-2…
DO WHILE (expression);
FROM table-1, table-2...
<iterated SAS statements> <WHERE join-condition(s)>
END; <additional clauses>;
QUIT;
DO UNTIL (expression);
<iterated SAS statements>
END;
PROC TRANSPOSE

Iterative DO Loop with a Conditional PROC TRANSPOSE DATA=input-data-set


Clause <OUT=output-data-set>
<NAME=variable-name>
<PREFIX=variable-name>;
DO index-variable=start TO stop <BY BY variable(s) <NOTSORTED>;
increment> VAR variable(s);
UNTIL | WHILE (expression); ID variable;
<iterated SAS statements> RUN;
END;

Create a Macro Variable


ARRAY Statement
%LET macro-variable=value;
ARRAY array-name {dimension} <$> <length>
<array-elements> <(initial-value-list)>;
Display Macro Variable Values
Query a Table with PROC SQL
%PUT text;
%PUT _USER_;
PROC SQL; %PUT _ALL_;
SELECT column-1, column-2… %PUT _AUTOMATIC_;
FROM table-1...
<WHERE expression>
<additional clauses>;
Reference a Macro Variable
QUIT;

&macro-variable-name.
Create a Table with PROC SQL

PROC SQL;
CREATE TABLE table-name AS
SELECT var-1, var-2…
FROM table-1...
<WHERE expression>
<additional clauses>;
QUIT;

3
SAS® Programming 2: Data Manipulation Techniques

DATA Step Debugger Commands


The following table lists the DATA Step Debugger commands by category.

Category Command Description


Controlling Program GO Starts or resumes execution of the DATA step
Execution
JUMP Restarts execution of a suspended program
STEP Executes statements one at a time in the
active program
Controlling the HELP Displays information about debugger
Windows commands
SWAP Switches control between the SOURCE
window and the LOG window
Manipulating DATA CALCULATE Evaluates a debugger expression and displays
Step Variables the result
DESCRIBE Displays the attributes of one or more
variables
EXAMINE Displays the value of one or more variables
SET Assigns a new value to a specified variable
Manipulating BREAK Suspends program execution at an executable
Debugging Requests statement
DELETE Deletes breakpoints or the watch status of
variables in the DATA step
LIST Displays all occurrences of the item that is
listed in the argument
TRACE Controls whether the debugger displays a
continuous record of the DATA step
execution
WATCH Suspends execution when the value of a
specified variable changes
Tailoring the ENTER Assigns one or more debugger commands to
Debugger the ENTER key
Terminating the QUIT Terminates a debugger session
Debugger

4
SAS® Programming 2: Data Manipulation Techniques

SAS Informats

Examples of SAS Informats


Informat Definition
$w. reads standard character Informat Raw Data Stored
data. Value Value
w.d reads standard numeric COMMA7.0 $12,354 12345
data. DOLLAR7.0
COMMAw.d reads nonstandard COMMAX7.0 $12.345 12345
DOLLARw.d numeric data and removes DOLLARX7.0
embedded commas,
EUROX7.0 €12.345 12345
blanks, dollar signs,
percent signs, and dashes. COMMA6.2 $12345 123.45
DOLLAR6.2
COMMAXw.d reads nonstandard
numeric data and removes COMMA10.2 $12345.567 12345.567
embedded periods, DOLLAR10.2
blanks, dollar signs,
percent signs, and dashes.
Examples of SAS Informats for Date
EUROXw.d reads nonstandard Values
numeric data and removes
embedded characters in Informat Raw Data Stored
European currency. Value Value
MMDDYY6. 010160 0
MMDDYY8. 01/01/60 0
MMDDYY10. 01/01/1960 0
DDMMYY6. 311260 365
DDMMYY8. 31/12/60 365
DDMMYY10. 31/12/1960 365
DATE7. 31DEC59 -1
DATE9. 31DEC1959 -1