SQL Processing with SAS® Tip Sheet
Total Page:16
File Type:pdf, Size:1020Kb
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 <options>; SELECT col-name SELECT column-1 <, ...column-n> LABEL= 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 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 <list> B A FROM table-A INNER JOIN table-B A SELECT <list> 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 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 <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 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>; %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>; ¯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. <TRIMMED> FROM table-1 | view-1 Masks special characters and %BQUOTE(character-string | <WHERE expression> mnemonic operators in a resolved text-expression) <other clauses>; 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 </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 BasicArrays Queries 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