<<

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 ModifyingBasic Queries Columns

PROC SQL ; SELECT col-name SELECT column-1 <, ...column-n> LABEL= LABEL=’column label’ FROM input-table SELECT col-name FORMAT= FORMAT=format. <,...col-name>; Creating a SELECT col-name AS

SQL Query Order of Execution: new column new-col-name Filtering Clause Description WHERE CALCULATED 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 Inserting rows INSERT INTO table <(column-list)> into tables VALUES (value<,...value>);

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 DESCRIBE TABLE (value1, value2, ...) <,...table-name-n>; WHERE col-name LIKE “_string%” DROP TABLE table-name-1 DROP TABLE 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>; Remerging Summary Statistics DROP VIEW DROP VIEW view-name-1 <,...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 B A FROM table-A INNER JOIN table-B A SELECT ON A.Key=B.Key; FROM table-A INTERSECT SELECT Full Join B FROM table-B; SELECT 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 A Right Join FROM table-A EXCEPT SELECT SELECT 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 Left Join FROM table-A UNION SELECT SELECT B A B FROM table-A LEFT JOIN table-B FROM table-B; ON A.Key=B.Key; The OUTER UNION operator selects all rows from both tables.

Creating Macro Variables A SELECT Storing a value in a macro variable using SQL: FROM table-A OUTER UNION SELECT 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 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 >)

Produces a substring of character Syntax Description %SUBSTR(argument, string (argument) by extracting position, the specified number of Creates a macro variable that is %GLOBAL macro-variable-1 <,length>) characters (length) beginning available during the execution ...macro-variable-n; 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

Defining a Macro Syntax Description %EVAL(arithmetic or Evaluates arithmetic and logical expressions using integer %MACRO macro-name <(parameter-list)>; logical expression) arithmetic. macro-text %MEND ; %SYSEVALF(expression Evaluates arithmetic and logical The parameter-list can be: <,conversion-type>) expressions using floating-point or arithmetic. %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>; ¯o-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; SELECT column1<,column2,...> %SUPERQ (argument) mnemonic operators at macro execution but prevents further INTO :macro-variable-1<,:macro-variable-2,...> resolution of the value. FROM table-1 | view-1 Masks special characters and %BQUOTE(character-string | mnemonic operators in a resolved text-expression) ; value at macro execution. QUIT; %QUPCASE(character-string | Converts values to uppercase and text-expression) returns a result that masks special characters and mnemonic operators. DATA Step Interface

%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 ; <,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

BasicArrays Queries Hash Objects

Defining an array ARRAY array-name<[number-of-array-elements]> Key Key Data Data Data <$> <_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 (); 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'>); Two-Dimensional Arrays object-name.DEFINEDONE( ); 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 Regular Expressions

PROC FORMAT; Perl Regular Expressions Metacharacters PICTURE format-name <(format-options)> Metacharacter Description /.../ Starting and ending delimiter ; (...) Enables grouping RUN; | Denotes the OR situation \d Matches a digit (0-9) Options \D Matches a non-digit such as letter \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 Creating Custom Numeric Formats * Matches the preceding character 0 or more times + 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) <$> ; 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(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.