0% found this document useful (0 votes)
34 views

Advanced SAS Programming Syntax Reference Guide

Sas programming syntaxes references

Uploaded by

adpadarsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Advanced SAS Programming Syntax Reference Guide

Sas programming syntaxes references

Uploaded by

adpadarsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SQL Processing with SAS® Tip Sheet

This tip sheet is associated with the SAS® Certified Professional Prep Guide Advanced Programming Using SAS® 9.4. For more information, visit www.sas.com/certify

Basic Queries Basic Queries


Modifying Columns

PROC SQL <options>;


LABEL= SELECT col-name
SELECT column-1 <, ...column-n>
LABEL=’column label’
FROM input-table
<WHERE expression>
SELECT col-name
<GROUP BY col-name> FORMAT=
FORMAT=format.
<HAVING expression>
<ORDER BY col-name> <DESC> <,...col-name>;
Creating a SELECT col-name AS
SQL Query Order of Execution: new column new-col-name

Filtering WHERE CALCULATED


Clause Description new columns new-col-name
SELECT Retrieve data from a table

FROM Choose and join tables


Modifying Rows
WHERE Filter the data

GROUP BY Aggregate the data INSERT INTO table


SET column-name=value
HAVING Filter the aggregate data <, ...column-name=value>;
ORDER BY Sort the final data INSERT INTO table <(column-list)>
Inserting rows
VALUES (value<,...value>);
into tables
INSERT INTO table <(column-list)>
Managing Tables SELECT column-1<,...column-n>
FROM input-table;

CREATE TABLE table-name Eliminating SELECT DISTINCT


CREATE TABLE (column-specification-1<, duplicate rows col-name<,...col-name>
...column-specification-n>);
WHERE col-name IN
DESCRIBE TABLE table-name-1 (value1, value2, ...)
DESCRIBE TABLE
<,...table-name-n>;
WHERE col-name LIKE
“_string%”
DROP TABLE DROP TABLE table-name-1
WHERE col-name BETWEEN
<,...table-name-n>; Filtering
value AND value
rows
WHERE col-name IS NULL
WHERE date-value
Managing Views “<01JAN2019>”d
WHERE time-value
“<14:45:35>”t
CREATE VIEW CREATE VIEW table-name AS query; WHERE datetime-value
“<01JAN201914:45:35>”dt
DESCRIBE VIEW view-name-1
DESCRIBE VIEW
<,...view-name-n>;

DROP VIEW DROP VIEW view-name-1 Remerging Summary Statistics


<,...view-name-n>;
SELECT col-name, summary function(argument)
FROM input table;

Copyright © 2019 SAS Institute Inc. Cary, NC, USA. All rights reserved.
SQL Processing with SAS® Tip Sheet
This tip sheet is associated with the SAS® Certified Professional Prep Guide Advanced Programming Using SAS® 9.4. For more information, visit www.sas.com/certify

Joins Summary Set Operators


Inner Join The INTERSECT operator selects unique rows that are
common to both tables.
SELECT <list>
A B FROM table-A INNER JOIN table-B SELECT <list>
A
ON A.Key=B.Key; FROM table-A INTERSECT
SELECT <list>
Full Join B FROM table-B;

SELECT <list>
A B FROM table-A FULL JOIN table-B The EXCEPT operator selects unique rows from table A that
ON A.Key=B.Key; are not found in table B.
SELECT <list>
A
Right Join FROM table-A EXCEPT
SELECT <list>
SELECT <list> B FROM table-B;
A B FROM table-A RIGHT JOIN table-B
ON A.Key=B.Key; The UNION operator selects unique rows from both tables.

A SELECT <list>
Left Join FROM table-A UNION
SELECT <list> SELECT <list>
B FROM table-B;
A B FROM table-A LEFT JOIN table-B
ON A.Key=B.Key;
The OUTER UNION operator selects all rows from both tables.

Creating Macro Variables A


SELECT <list>
Storing a value in a macro variable using SQL: FROM table-A OUTER UNION
SELECT <list>
SELECT col-name-1 <,...col-name-n> B
FROM table-B;
INTO:macrvar_1<,...macvar-n>
FROM input-table;
Storing a list of values in a macro variable using SQL:
SELECT col-name-1 <,...col-name-n> Accessing DBMS Data
INTO:macrvar_1 SEPARATED BY ‘delimiter’
FROM input-table;
The SQL pass-through facility enables you to code in
Viewing the value of the macro variable in the SAS Log: the native DBMS SQL syntax and pass the query to the
%PUT &=macvar; database.
PROC SQL;
CONNECT TO DBMS-name <AS alias>
Subqueries (DBMS-connection-options);
SELECT col-name
SELECT col-name, FROM CONNECTION TO DBMS-name|alias (dbms-query);
(SELECT function(argument) DISCONNECT FROM DBMS-name|alias;
FROM input-table) QUIT;
FROM input-table;

SELECT col-name, <,...col-name>


FROM input-table
WHERE col-name
(SELECT function(argument)
FROM input-table)

Copyright © 2019 SAS Institute Inc. Cary, NC, USA. All rights reserved.
SAS® Macro Language Processing
This tip sheet is associated with the SAS® Certified Professional Prep Guide Advanced Programming Using SAS® 9.4. For more information, visit www.sas.com/certify

SAS Macro Facility Macro Character Functions

SAS Syntax Description


Macro
Determines the position of the
Facility %INDEX (source, string) first character of a string within
SAS macro SAS program another string.
program code
%SCAN (argument, n, Searches the argument and
<charlist < returns the nth word.
Creating Macro Variables ,modifiers>>)

Produces a substring of character


Syntax Description %SUBSTR(argument, string (argument) by extracting
position, the specified number of
%GLOBAL macro-variable-1 Creates a macro variable that is <,length>) characters (length) beginning
...macro-variable-n; available during the execution at the specified starting position.
of the entire SAS session.

%LET variable=value; Creates a macro variable and %UPCASE(character-string| Converts lowercase characters
assigns it a value. text-expression) in the argument to uppercase.
Creates a macro variable that
%LOCAL macro-variable-1
is available only during the
...macro-variable-n;
execution of the macro
where it is defined. SAS Functions with Macro Variables

Syntax Description
Defining a Macro
%EVAL(arithmetic or Evaluates arithmetic and logical
logical expression) expressions using integer
%MACRO macro-name <(parameter-list)>;
arithmetic.
macro-text
%MEND <macro-name>;
%SYSEVALF(expression Evaluates arithmetic and logical
The parameter-list can be: <,conversion-type>) expressions using floating-point
<positional-parameter-1, ...positional-parameter-n> or arithmetic.
<keyword-1=value-1, ....keyword-n=value-n>
%SYSFUNC(function
Executes SAS functions or
(argument-1
user-written functions in the
<...argument-n>)
macro facility.
Calling a Macro <,format>)

%macro-name
%macro-name(positional-parameter-1,
...positional-parameter-n) Troubleshooting Macro Variable References
%macro-name(keyword-1=value-1, ....keyword-n=value-n)
Enables you to write your own messages to the SAS log.
%PUT text;
Referencing a Macro Variable Deletes the specified variables from the macro global symbol
table.
Use the name of the macro variable with an ampersand. %SYMDEL macro-variable-1 <...macro-variable-n></option>;
&macro-variable;

Copyright © 2019 SAS Institute Inc. Cary, NC, USA. All rights reserved.
SAS® Macro Language Processing
This tip sheet is associated with the SAS® Certified Professional Prep Guide Advanced Programming Using SAS® 9.4. For more information, visit www.sas.com/certify

Masking Special Characters Options

Syntax Description OPTIONS MCOMPILENOTE= NONE | NOAUTOCALL | ALL;


OPTIONS MPRINT | NOMPRINT;
Hides the usual meaning of a
%STR(argument) semicolon(;) so it appears as OPTIONS MLOGIC | NOMLOGIC;
constant text. OPTIONS MAUTOSOURCE | NOAUTOSOURCE;
Hides the usual meaning of an
%NRSTR (character-string) ampersand (&) or a percent sign Creating Macros in SQL
(%) so they appear as constant text.

Masks all special characters and PROC SQL NOPRINT;


mnemonic operators at macro SELECT column1<,column2,...>
%SUPERQ (argument)
execution but prevents further INTO :macro-variable-1<,:macro-variable-2,...>
resolution of the value. <TRIMMED>
FROM table-1 | view-1
Masks special characters and <WHERE expression>
%BQUOTE(character-string |
mnemonic operators in a resolved <other clauses>;
text-expression)
value at macro execution. QUIT;
%QUPCASE(character-string | Converts values to uppercase and
text-expression) returns a result that masks special DATA Step Interface
characters and mnemonic operators.
%QSUBSTR(argument, Produces a substring of a CALL SYMPUTX(macro-variable-name, value
position character string. <,symbol-table>);
<,length>)
PUT(source,format.);
%QSCAN(argument, n Searches for a word and masks
<,charlist<, special characters and mnemonic
modifiers>>) operators. Advanced Macro Techniques
%QSYSFUNC(function( Executes functions and masks
arguments) special characters and mnemonic %INCLUDE file-specification </SOURCE2>;
<,format>) operators.
DOSUBL(text-string);

Default Autocall Library


Conditional Processing %LOWCASE(argument)
%QLOWCASE(argument)
%IF expression %THEN text;
<%ELSE text>; %LEFT(argument)
%TRIM(argument)
%IF expression %THEN %DO;
%CMPRES(argument)
text and/or macro language statements;
%END; %DATATYP(argument)
%ELSE %DO;
text and/or macro language statements;
%END;

%DO index-variable=start %TO stop <%BY increment>;


text
%END;

Copyright © 2019 SAS Institute Inc. Cary, NC, USA. All rights reserved.
Advanced SAS® Programming Techniques
This tip sheet is associated with the SAS® Certified Professional Prep Guide Advanced Programming Using SAS® 9.4. For more information, visit www.sas.com/certify

Basic Queries
Arrays Hash Objects
Defining an array
ARRAY array-name<[number-of-array-elements]> Key Key Data Data Data
<$> <length> <array-elements>
<_TEMPORARY_> <(initial-values)>;
col_A col_B Data Data Data
Referencing an array
array-name[element-number];

samparray

1st element 2nd element 3rd element

PDV samparray[1] samparray[2] samparray[3]

EmpID Salary Bonus Raise_Percent A hash object is an in-memory table that contains
key and data components.

Hash Object and Iterator Process


The number of elements must be enclosed in Declaring hash object or hash iterator object:
either parentheses ( ), braces { }, or brackets [ ].
DECLARE hash object-name
(<argument_tag-1:value-1, ...>);
Unknown Number of Elements
DECLARE hiter object-name
Use an asterisk (*) within your brackets
(’hash-object-name’);
when defining an array.
Use the DIM function to return the number of Defining a hash object:
elements in an array. object-name.ADD( );
object-name.DEFINEKEY('key-1' <, ...'key-n'>);
DIM(array-name);
object-name.DEFINEDATA('data-1' <, ...'data-n'>);
object-name.DEFINEDONE( );
Two-Dimensional Arrays
object-name.OUTPUT( );
ARRAY array-name
[number-of-rows,number-of-columns]; Using a hash object:
array samplearray[3,2]; object-name.FIND( );

The example above creates an array named Retrieving a hash object with a hash iterator object:
SampleArray which has 3 rows and 2 columns. object-name.FIRST( );
object-name.LAST();
object-name.NEXT();
object-name.PREV( );

Copyright © 2019 SAS Institute Inc. Cary, NC, USA. All rights reserved.
Advanced SAS® Programming Techniques
This tip sheet is associated with the SAS® Certified Professional Prep Guide Advanced Programming Using SAS® 9.4. For more information, visit www.sas.com/certify

Picture Formats Perl Regular Expressions


PROC FORMAT; Perl Regular Expressions Metacharacters
PICTURE format-name <(format-options)>
Metacharacter Description
<value-range-set-1= ’template-value’ (template-options)>
/.../ Starting and ending delimiter
<value-range-set-n= ’template-value’ (template-options)>;
(...) Enables grouping
RUN;
| Denotes the OR situation
\d Matches a digit (0-9)
Matches a non-digit such as letter
Options \D
\s Matches a whitespace character
\w Matches a group of characters
Creating Custom Date, Time, Datetime Formats . Matches any character
DATATYPE=DATE | TIME | DATETIME [...] Matches a character in brackets
enables the use of directives in the picture as a template to [^...] Matches a character not in brackets
format date, time, or datetime values. ^ Matches the beginning of the string
$ Matches the end of the string
DEFAULT=length \b Matches a word boundary
specifies the default length of the picture. \B Matches a non-word boundary
* Matches the preceding character 0 or more times
Creating Custom Numeric Formats
+ Matches the preceding character 1 or more times
MULT|MULTIPLIER=n ? Matches the preceding character 0 or 1 times
specifies a number to multiply the value by. {n} Matches exactly n times
\ Overrides the next metacharacter such as a ( or ?)
PREFIX=’prefix’
specifies a character prefix to place
in front of the formatted value. PRXMATCH Function
ROUND PRXMATCH function searches for a pattern match and
rounds the value to the nearest integer returns the position at which the pattern is found.
before formatting.
PRXMATCH (Perl-regular-expression, source);

Creating Functions PRXPARSE Function


PRXPARSE function returns a pattern identifier number that is
PROC FCMP OUTLIB=libref.table.package; used by other PRX functions and call routines.
FUNCTION function-name(arguments) <$> <length>; pattern-ID-number=PRXPARSE (Perl-regular-expression);
. . . programming statements . . .
RETURN(expression); PRXCHANGE Function
ENDSUB;
PRXCHANGE function performs a substitution for a
QUIT;
pattern match
Using Custom Functions PRXCHANGE (Perl-regular-expression, times, source)
OPTIONS CMPLIB=libref.table | (libref.table-1...libref.table-n)

Advanced Functions
LAG<n>(column);
COUNT(string, substring<,modifiers>);
COUNTC(string, character-list<,modifiers>);
COUNTW(string, <,delimiters><,modifiers>);
FIND(string, substring<,modifiers><,start-position>);
FINDC(string, character-list<,modifiers> <,start-position>);
FINDW(string, word<,delimiters><,modifiers><,start-position>);

Copyright © 2019 SAS Institute Inc. Cary, NC, USA. All rights reserved.

You might also like