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
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
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
%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.
%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 <$>
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 (
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
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) <$>
Advanced Functions
LAG
Copyright © 2019 SAS Institute Inc. Cary, NC, USA. All rights reserved.