Converting text field to date in SAS SQL - sql

I am trying to convert a text field from a SAS table with the form '2014-12-31' into a another SAS table as a date field. The below does not work:
proc sql outobs=50;
create table Dbtr_Clnt_Generl_Inf as
select FACS_Schema_ID '',
'DBACCT*'n as ACCOUNT_NUM '',
DBLSTDTI as Date_Listed format=date09.
from sqlsrv10.Acct_Dbtr_Clnt_Generl_Inf;
quit;
I get the following error:
ERROR: Character expression requires a character format.

You use INPUT to convert types in SAS.
proc sql outobs=50;
create table Dbtr_Clnt_Generl_Inf as
select FACS_Schema_ID '',
'DBACCT*'n as ACCOUNT_NUM '',
input(DBLSTDTI,yymmdd10.) as Date_Listed format=date09.
from sqlsrv10.Acct_Dbtr_Clnt_Generl_Inf;
quit;
That is most likely the correct informat based on your question.

Related

Upload and read dataset SAS - SQL Server

I am uploading a dataset to MS SQL server, and I did it in two ways
first one:
proc sql;
create table lib.dataset as
select ID, date_reg from havingdata
;
Quit;
Here date format is YYMMDD10.
All went well, and date format also looks good on SQL server. However When i access the same from SAS, the date format changes to Date9.
2.I created the table directly on SQL server
create table dbo.dataset
(
ID char(11) NOT NULL,
date_reg date,
);
And then inserted the values from SAS
proc sql;
insert into lib.dataset
select ID, date_reg from havingdata
;
quit;
all went well, but again the same problem when I access the data from SAS, the date format changes from YYMMDD10. to Date9,
How can I get the same format as of in the SQL Server.
the libname I use is
libname lib odbc noprompt="dsn=myserver; database=dab" schema=dbo
bulkload=yes;

Error while running Hive command with DATE as of the colume name

create table Book_inf2(OID int, date timestamp, CUSTOMER_ID string, AMOUNT
int) row format delimited fields terminated by ',';
Error which I got:
FAILED: ParseException line 1:32 missing Identifier at 'date' near
'date' in create table statement line 1:37 mismatched input
'timestamp' expecting ) near 'date' in create table statement
Note: I am new to the Hive, please help me to get understand.
Date is a reserved keyword in hive that's the reason why you are facing issue
However hive allows to use reserved keywords as field names, but that's not the best practice to use them.
To fix the issue:
Surround date field name with backtick's
`
Try with below create table statement
hive> create table Book_inf2(OID int, `date` timestamp, CUSTOMER_ID string, AMOUNT int) row format delimited fields terminated by ',';

Create table in SAS using DB2 timestamp

We've recently gotten the accelerator (IDAA) working on our DB2, which I mainly access using SAS.
This requires us, due to network issues, to create tables first, before inserting rows.
My problem is creating a table with the correct timestamp format, I can create the table using a select statement, but this is very slow, but here I can see the format in SAS is DATETIME30.6
But if I try something like:
RSUBMIT prod_acc;
Proc delete data=user.table1; run; %PUT &sqlxrc &sqlxmsg;
proc sql inobs=MAX stimer feedback noerrorstop;
connect to db2(ssid=server);
create table user.table1
(
date datetime30.6
,reference char(16)
,transact char(20)
,alias char(60)
,amount decimal(15,2)
,currency char(3)
);
%PUT &sqlxrc &sqlxmsg;
quit;
run;
Which gives the following in the log
(
15 date datetime30.6
-----------
1 22
200
WARNING 1-322: Assuming the symbol DATE was misspelled as datetime30.
ERROR 22-322: Syntax error, expecting one of the following: a quoted string,
an integer constant, ), ',', CHECK, DISTINCT, FORMAT, INFORMAT, LABEL, LEN,
LENGTH, NOT, PRIMARY, REFERENCES, TRANSCODE, UNIQUE, ^, ~.
ERROR 200-322: The symbol is not recognized and will be ignored.
And if I look in DB2, the column has the type timestmp which SAS don't recognize as a type.
(
31 date timestmp
--------
22
76
ERROR 22-322: Syntax error, expecting one of the following: CHAR, CHARACTER, DATE, DEC,
DECIMAL, DOUBLE, FLOAT, INT, INTEGER, NUM, NUMERIC, REAL, SMALLINT, VARCHAR.
ERROR 76-322: Syntax error, statement will be ignored.
Tried googling and found a lot of different versions of answers, but nothing I can see is relevant to this, the closest was something about manually creating the format, but I can't figure out how to do that.
Any ideas?
It is probably more natural in SAS to define a table's structure using a DATA step rather than PROC SQL.
data userdb.table1;
stop;
length date 8 reference $16 transact $20 alias $60 amount 8 currency $3 ;
format date datetime30.6 amount 15.2 ;
run;
If your libref is pointing to a database then you should be able to use DBTYPE= dataset option to tell SAS what data types to use for your fields in the external database. At least it works for Teradata. These dataset options should work inside PROC SQL also.
proc delete data=userdb.table1; run;
data userdb.table1
(dbtype=
( date='timestamp'
reference='varchar(16)'
transact='varchar(20)'
alias='varchar(60)'
amount='decimal(15,2)'
currency='char(3)'
)
);
stop;
length date 8 reference $16 transact $20 alias $60 amount 8 currency $3 ;
format date datetime30.6 amount 15.2 ;
run;
Can't you just:
create table user.table1
(
"date" TIMESTAMP(6)
,reference char(16)
,transact char(20)
,alias char(60)
,amount decimal(15,2)
,currency char(3)
);
? Remember, in DB2, date is a reserved word, and then it's always safe to put that into double quotes. Alternatively, use a non-reserved word for the column name, like dt or so.
These two lines are incongruous:
connect to db2(ssid=server);
create table user.table1
The first creates a connection for a pass-through query, while the latter creates the table using the libname engine. In this case your first statement is irrelevant as it's not used; you should remove it (unless you use it later and just left it in by mistake in your example).
Since you used the libname syntax, you must follow SAS syntax rather than DB2. There is no specification for datetime type in the create table statement, specifically under the column-definition documentation page. Instead you have this list to choose from:
CHARACTER | VARCHAR <(width)> indicates a character column with a column width of width. The default column width is eight characters.
INTEGER | SMALLINT indicates an integer column.
DECIMAL | NUMERIC | FLOAT <(width<, ndec>)> indicates a floating-point column with a column width of width and ndec decimal
places.
REAL | DOUBLE PRECISION indicates a floating-point column.
DATE indicates a date column.
The way I find best to specify datetime (meaning, most likely to work as you expect) is not to use date but numeric, and then use the format argument to define it as datetime.
proc sql;
create table table1
( date num format=datetime30.6
,reference char(16)
,transact char(20)
,alias char(60)
,amount decimal(15,2)
,currency char(3)
);
quit;
However, I would suggest your best choice is to use passthrough to create the table, so you can use DB2 syntax - since you're creating a table there, not in SAS itself.

can't insert mmddyy10. format proc sql

I have a table with column as_of_date that is formatted as MMDDYY10. in SAS 7.1
proc sql;
INSERT INTO mytable (as_of_date)
VALUES (12/31/2016);
run;
and I get the following error:
ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant,
a missing value, ), +, ',', -, MISSING, NULL, USER.
ERROR 200-322: The symbol is not recognized and will be ignored.
Note: if I change the value to 12/31/16 it still does not work. If I put quotes around it ('12/31/2016') I get the error:
ERROR: Value 1 of VALUES clause 1 does not match the data type of the corresponding column in the object-item list (in the SELECT
clause).
If I insert it without slashes (12312016) it is inserted without errors as ********
You need to use the DDMMMYYYY format within quotes and the d modifier:
proc sql noprint;
INSERT INTO mytable (as_of_date)
VALUES ("31dec2016"d);
quit;
Another way to look at it is SAS is looking for the numeric value underneath a date format. You can check the actual value and use the following code to get the same result:
data check;
date = "31dec2016"d;
run;
proc sql noprint;
INSERT INTO mytable (as_of_date)
VALUES (20819);
quit;

Change Date format during SQL

I am moving data to a new database and need to so some formatting during the move to try and save me time.
The current DB saves the date as YYYYMMDD and the new DB wants it in mm/dd/yyyy. Can I do this during the move or will I have to format after?
Thank you all!
You cold use the format function . So I am not sure how you are getting the data to sql 2014 but once the data is there you could use this command.
This is an example selecting from a table that has a date and altering its format .
use AdventureWorks2012
go
select modifieddate as 'original format', FORMAT ( modifieddate, 'd', 'en-US' ) AS 'New format mm/dd/yy'
from [Sales].[SalesOrderDetail]
Query Result
If your data is not format and its just a string you could use the format command to add the separators .
This code create a table with a date as an INT, the selects the data and formats it as a data time into another table .
CREATE TABLE Test_TimeString (Timeint int)
GO
INSERT INTO Test_TimeString VALUES(04242016)
GO
CREATE TABLE Test_Time (Timedate DATETIME)
GO
INSERT INTO Test_Time
SELECT FORMAT(Timeint,'##/##/####')
FROM Test_TimeString
SELECT * FROM Test_Time