Download presentation
Presentation is loading. Please wait.
Published byMarlene Parks Modified over 8 years ago
1
Jessica Bennett, Advance America Barbara Ross, Flexshopper LLC PharmaSUG 2015 Paper #QT06
2
2 Jessica Bennett Fluent in SQL Loves R Learning to embrace SAS Barbara Ross Fluent in SAS Pretty good with SQL Opened R once
3
3 Production Database via SQL & R
4
Convert SQL to SAS 4 Production Database +
5
This should be easy… That’s what PROC SQL is for!
6
a) Concatenating strings SQL Code: SELECT FirstName + ' ' + LastName As FullName FROM Customers MySQL Code: SELECT CONCAT(FirstName, ' ', LastName) As FullName FROM Customers SAS Code - “||” Operator: SELECT FirstName || ' ' || LastName As FullName FROM Customers SAS Code - CATX Function: SELECT CATX(' ',FirstName,LastName) As FullName FROM Customers 6
7
b) Not equals shortcut SQL Code - !=: SELECT * FROM inputds WHERE convert(date, date_created) != '1-5-2014‘ SQL Code - <>: SELECT * FROM inputds WHERE convert(date, date_created) <> '1-5-2014' SAS Code: SELECT * FROM inputds WHERE date_created ^= '05jan2014'd; 7
8
Global macro variables available in SAS the whole session In SQL, variable is only available while query is processing SQL Code: DECLARE @FirstMonday date SET @FirstMonday = '1-5-2014' SELECT * FROM inputds_201411 WHERE date > @FirstMonday SAS Code: %LET FirstMonday='05jan2014'd; PROC SQL; CREATE TABLE tmp AS SELECT * FROM inputds_201401 WHERE date>=&FirstMonday; QUIT; 8
9
- Temporary tables must be dropped with SQL. - SAS stores tables in the temporary work library by default - #TMP causes SQL to hold the temp table in memory the whole session SQL Code: IF OBJECT_ID('tempdb..#alldata') IS NOT NULL DROP TABLE #alldata SELECT * INTO #alldata FROM inputds_201411 SAS Code: PROC SQL; CREATE TABLE alldata AS SELECT * FROM inputds_201411; QUIT; 9
10
- SQL global tables are only available while query is processing (saves memory) SQL Code: DECLARE @TEST TABLE ( First_Name varchar(12), Date_Created datetime ) INSERT INTO @TEST VALUES ('Jessica', '1-9-2015') ('Barbara', '1-5-2015') SELECT * FROM @TEST SAS Code: DATA _null_; SET inputds; RUN; 10
11
In example, inputds1 cust_id is character & inputds2 cust_id is numeric SQL Code: SELECT * FROM inputds1 a INNER JOIN inputds2 b on b.cust_id = a.cust_id SAS Code – Convert both to numeric: PROC SQL; CREATE TABLE new AS SELECT * FROM inputds1, inputds2 ON INPUT(inputds1.cust_id,best32.) = inputds2.cust_id; QUIT; 11
12
SQL SYNTAX:CONVERT(data_type, expression) SAS SYNTAX: PUT(source, format) INPUT(source, informat) In example, SSN is numeric & amount is character (“$34.35”) SQL Code: SELECT RIGHT(‘00000000’ + CONVERT(VARCHAR(9), SSN), 9) as SSN,CONVERT(numeric, REPLACE(amount, '$', '') ) as amount FROM inputds SAS Code: PROC SQL; CREATE TABLE new AS SELECT PUT(SSN,z9.) as SSN,INPUT(amount,dollar20.) as amount FROM inputds; QUIT; 12
13
“d” & “dt” not needed in SQL If declared as a datetime, SQL will default to 0:0:0 SQL Code: SELECT * FROM inputds WHERE Date_Created >= '1-5-2014' -or – SELECT * FROM inputds WHERE Date_Created >= '2014/08/01' SAS Code: PROC SQL; CREATE TABLE tmp AS SELECT * FROM inputds WHERE DATEPART(date_created) >= '05jan2014'd; QUIT; 13
14
SQL Syntax: DATEADD(interval, increment int, expression); DATEDIFF(interval, startingdate, ending_date); SAS Syntax: INTNX(interval, start-from, increment, ); INTCK(interval, start-date, end-date); SQL Code: SELECT DATEADD(week, 2, LastPayDate) as NextPayDate,DATEDIFF(day, LastPayDate, getdate()) as DaysSincePaid FROM inputds SAS Code: PROC SQL; CREATE TABLE tmp AS SELECT INTNX('week',LastPayDate,2,'end') as NextPayDate,INTCK('day',LastPayDate,today()) as DaysSincePaid FROM inputds; QUIT; 14
15
Allowed variable name lengths: SAS - 32 characters Oracle - 30 characters SQL - 128 characters (116 characters in temporary tables) SAS will crop/rename your variables to conform to their naming conventions if accessing SQL through a Libname SAS does not support variable names that start with a number. Spaces allowed in SQL & SAS EG. To have SAS replace spaces with underscores, there is the VALIDVARNAME option SAS Code: SAS OPTIONS VALIDVARNAME=v7; 15
16
There’s more! See the paper for more examples 16
17
There’s more! See the paper for more examples Even then it’s not everything. 17 PROC SQL for SQL Die-Hards Part 2?
18
Biggest difference is changing the way you think about creating tables. SAS is a analytical tool so it creates and changes tables more easily. It also tends to “hold on” to information (like macros and tables). SQL programs are designed to minimize memory usage (hence the create a table then append on practice and only storing while in the current query). 18
19
Name: Barbara Ross Organization: Flexshopper LLC City, State: Boca Raton, FL E-mail: bmharlan@gmail.com Web: www.bharlan.weebly.com Name: Jessica Bennett Organization: Advance America City, State: Spartanburg, SC E-mail: Mary.Jessica.Bennett@gmail.com 19 Download paper & presentation here
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.