0

This is the first time I have had an issue with mis-matched data types on an INNER JOIN. One thing to note, I am not able to change the data types of the source columns.

Here is an example of the proc SQL I am trying to run:

CREATE TABLE SC_STEP_1 AS
SELECT (INPUT(A.ACCOUNT_NBR, $32.) AS A.ACCOUNT_NBR_INT),
A.OPEN_DT,
A.STATUS_CD,
A.STATUS_DT
FROM ACCOUNT A
INNER JOIN ACCOUNT_DETAIL B
ON A.ACCOUNT_NBR_INT=B.ACCOUNT_ID
WHERE A.STATUS_DT>=2022-03-14 AND A.STATUS_CD="Z";
quit;

The first attempt without trying to match the data types resulted in data mis-match errors. A.ACCOUNT_NBR is a CHAR(32) but the column only contains numbers. B.ACCOUNT_ID is an INTEGER that only contains numbers.

This was the error block of running the above to try and match data types

50         SELECT (INPUT(A.ACCOUNT_NBR, $32.) AS A.ACCOUNT_NBR_INT),
                                              __
                                              22
                                              76
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, ), *, **, +, -, /, <, <=, <>, =, >, >=, ?, AND, BETWEEN, 
              CONTAINS, EQ, EQT, GE, GET, GT, GTT, IN, IS, LE, LET, LIKE, LT, LTT, NE, NET, NOT, NOTIN, OR, ^, ^=, |, ||, ~, ~=.  

ERROR 76-322: Syntax error, statement will be ignored.

I have gotten so many other errors just drawing at straws with the syntax from "Expecting Format" to "Expecting a quoted string" to even just "Expecting ), -" and I have no idea how to continue or how to make this work. Based on the research and googling it today, I have found many examples that seem like they should work but I cannot get any syntax to pass and documentation I have found has been light at best.

Hopefully someone can point me in the right direction or if I have to attack this from a completely different way.

Update: Based on comments, I have attempted the following instead by loading the data into a temp table:

proc sql;
CREATE TABLE SC_STEP_11 (ACCOUNT_NBR_INT integer, OPEN_DT date, STATUS_CD CHAR(1), STATUS_DT date);
quit;

proc sql;
SELECT A.ACCOUNT_NBR,
A.OPEN_DT,
A.STATUS_CD,
A.STATUS_DT,
INTO WORK.SC_STEP_11
FROM ACCOUNT A
WHERE A.STATUS_DT>=2022-03-14 AND A.STATUS_CD="Z";
quit;

The table generates fine but I am getting a "Expecting a :." error:

61         INTO WORK.SC_STEP_11
                ____
                79
                76
ERROR 79-322: Expecting a :.

ERROR 76-322: Syntax error, statement will be ignored.
  • `CREATE TABLE table AS SELECT ...` is not valid SQL Server t-sql. Are you sure you're using SQL Server and not MySQL? – squillman Jun 14 '22 at 17:28
  • It is DB_MSSQL type. I tagged it as sql-server as "MSSQL" is mentioned in the definition. – TylerAriasi Jun 14 '22 at 17:36
  • So, that's maybe you're problem. It shows you a syntax error exactly where SQL Server would show you an error. Your SQL statement is invalid for SQL Server. – squillman Jun 14 '22 at 17:40
  • Right, so in my question I have tried many different ways of altering the syntax and adding the missing pieces that the Log is saying I am missing but still without success which is why I am asking what that syntax should be. – TylerAriasi Jun 14 '22 at 17:47
  • Got it. This should help you with SQL Server syntax for creating a table based on the results of a query. https://stackoverflow.com/questions/16683758/how-to-create-a-table-from-select-query-result-in-sql-server-2008 – squillman Jun 14 '22 at 17:49
  • @squillman So I gave that a try but now I am getting a "Error 79-322: Expecting a :." when I try to INSERT into my new temp table. I have no idea where the colon needs to go and no documentation on the SELECT ... INTO syntax has a colon that I can find. – TylerAriasi Jun 14 '22 at 18:17
  • You should edit your question to include the new approach you have tried with the new error message. We can't help debug a statement that we can't see. – squillman Jun 14 '22 at 18:27
  • @squillman added the updated attempt and error message. – TylerAriasi Jun 14 '22 at 18:35
  • `SELECT INTO` creates the table automatically, the table can't exist already. You need to remove the `CREATE TABLE` bit. Also, you need to surround date strings in single quotes `2022-03-14`. – squillman Jun 14 '22 at 18:40
  • Also, understand that what you are creating is NOT a temp table. It will be a physical table that will persist (unless this is run in a transaction and is rolled back). – squillman Jun 14 '22 at 18:45
  • And I still question that you are actually using SQL Server in the backend. Just because your config says DB_MSSQL doesn't mean that is what you are really trying to connect to. – squillman Jun 14 '22 at 18:46
  • @squillman I have dropped the SC_STEP_11 table and it does not exist anymore. I am still getting a missing ":" error at the same location. I updated with single quotes but without the quotes, the original query (without the insert, just a basic query) ran fine with desired results. – TylerAriasi Jun 14 '22 at 18:58
  • As long as you have dropped the table in the database and removed the code to create it, I don't know what the "missing :" error means. That is not from SQL Server so that's about as far as I can help, unfortunately. – squillman Jun 14 '22 at 19:01
  • 1
    `A.STATUS_DT>=2022-03-14` What you think is a date constant / literal is actually a mathematical expression involving subtraction of 3 values. A date literal is a string of a particular format - i.e., '20220314') `A.STATUS_DT,` This has an extra comma after the last column in the SELECT list - remove it. `A.STATUS_CD="Z";` String constants are delimited with single quote characters, not double quote characters - as far as TSql is concerned. – SMor Jun 14 '22 at 19:05
  • Why are you using `input(xx,$32.)`? Why not just use `substrn(xx,1,32)`? If you want to convert from character to numeric you need to use a numeric informat and not a character informat. `input(xx,32.)`. Also if your numbers need more than 15 digits you cannot convert them into numbers in SAS. They will be too large to be stored exactly. So instead convert the numbers (on the remote non-SAS side) into character strings and then you can preserve the value exactly. – Tom Jun 14 '22 at 20:05

1 Answers1

0
  • INTO in this context is invalid in SAS, it's used to create macro variables not tables. Use CREATE TABLE instead.
  • Date literals/constants need to be specified as DATE9 format, enclosed in quotes with a d after, e.g. '14Mar2022'd

Your SAS code should be:

proc sql;
create table sc_step_11 as
SELECT A.ACCOUNT_NBR,
A.OPEN_DT,
A.STATUS_CD,
A.STATUS_DT,
FROM ACCOUNT A
WHERE A.STATUS_DT>= '14Mar2022'd AND A.STATUS_CD="Z";
quit;

What happens if you run something like the following, fixing the other query issues.

PROC SQL;
CREATE TABLE SC_STEP_1 AS
SELECT INPUT(A.ACCOUNT_NBR, $32.) AS ACCOUNT_NBR_INT,
A.OPEN_DT,
A.STATUS_CD,
A.STATUS_DT
FROM ACCOUNT A
INNER JOIN ACCOUNT_DETAIL B
ON INPUT(A.ACCOUNT_NBR, $32.) = B.ACCOUNT_ID
WHERE A.STATUS_DT>='14Mar2022'd AND A.STATUS_CD="Z";
quit;
Reeza
  • 20,510
  • 4
  • 21
  • 38
  • This is what I ended up having to go with although it does not answer my original question. I was looking into converting one data type into another so that a join can be completed since Table A's and Table B's linking column do not match data type – TylerAriasi Jun 14 '22 at 19:27
  • See edited new query. Not sure why you think you can't convert types but definitely can't alias with a. for the new column. – Reeza Jun 14 '22 at 19:30