Just wondering how I can alter the following query to show date in the format I want. I am using SAS to pull this data.
Existing Date format: 15MAR2011:09:05:16.000000
Format I want: 15MAR2011:09:05:16
Query I am using:
proc sql;
create table data.test as
select * from connection to odbc
(
select ID,
DATE AS CREATION_DATE,
from maintable
);
quit;
A format affects how SAS displays a variable value. It does not affect the actual value itself.
So, assuming the variable CREATION_DATE is a datetime value, just assign it a format of DATETIME20. to display is as you want:
proc sql;
create table data.test as
select ID, CREATION_DATE format=datetime20.
from connection to odbc
( select ID, DATE AS CREATION_DATE
from maintable );
quit;
However, some ODBC interfaces will return your date column as a character string, so you need to be sure it is showing up on the SAS side as a proper datetime value.
Related
After I run this SAS SQL process. The date variable "EXVISDAT" showed in the results in a numerical way. How can I change the code to let the date variable "EXVISDAT" show in format of "date9."?
proc sql;
create table test2 as
select VISIT, Targetdays, Targetdays + '01JUL2019'd - 1 as EXVISDAT
from data_in.chrono_visits_lts14424
order by Targetdays
;
quit;
You can read about it in documentation.
In yours case, query will look like:
proc sql;
create table test2 as
select VISIT, Targetdays, Targetdays + '01JUL2019'd - 1 as EXVISDAT format=date9.
from data_in.chrono_visits_lts14424
order by Targetdays
;
quit;
I'm having problems to DATEs in SAS Enterprise Guide 7.1 M4.
it's very very simple in SQL Server or VBA but in SAS is driving me crazy.
Problem:
For some strange reason I'm unable to make a simple select. I tried many different forms of formating and convertions but any seems to work
My Simple select returns no observations.
Description of T1.DT_DATE in proc contents
Type: Num
Len: 8
Format: DDMMYY10.
Informat: DATETIME20.
%let DATE_EXAMPLE='01JAN2019'd;
data _null_;
call symput ('CONVERTED_DATE',put(&DATE_EXAMPLE, ddmmyy10.));
run;
%put &CONVERTED_DATE;
PROC SQL;
CREATE TABLE TEST_SELECT AS
SELECT *
FROM MY_SAMPLE_DATA as T1
WHERE T1.DT_DATE = &CONVERTED_DATE
;QUIT;
Intially you are setting up the date properly but you are changing it to a different value that is not understood in where clause. See the resolutions of macrovariable for both macrovariables you have created
%put value of my earlier date value is &DATE_EXAMPLE;
value of my earlier date value is '01JAN2019'd
%put value of my current date value is &CONVERTED_DATE;
value of my current date value is 01/01/2019
change your code to use date literal that is '01JAN2019'd then your code will work. 01/01/2019 value will not make sense in where clause.
PROC SQL;
CREATE TABLE TEST_SELECT AS
SELECT *
FROM MY_SAMPLE_DATA as T1
WHERE T1.DT_DATE = &CONVERTED_DATE
;QUIT;
I created below SAS code to pull the data for particular a date.
%let date =2016-12-31;
proc sql;
connect to teradata as tera ( user=testuser password=testpass );
create table new as select * from connection tera (select acct,org
from dw.act
where date= &date.);
disconnect from tera;
quit;
There are situation where that particular date may be missing in the dataset due to holiday.
I thinking how to query the previous date(non-holiday) if the mention date in the %let statement is holiday
Before running your query you have to do a lookup or data check on the date you are using. You have two options:
Use a Date Dimension table in order identify/lookup holidays.
Count how many records you have for that date, if you get 0 obs for this date, use date+1 in your query.
I recommend using the date dimension table option.
Teradata has Sys_Calendar.Calendar view. You can use that in query, it has all the information regarding weekdays and others.
if you want to SAS way use weekday function and use call symput as shown below. Teradata needs single quote around the date, so it is better to have single quotes around when creating macro variable
data _null_;
/* this is for intial date*/
date_int = input('2016-12-31', yymmdd10.);
/* create a new date variable depending on weekday*/
if weekday(date_int) = 7 then date =date_int-2; /*sunday -2 days to get
friday*/
else if weekday(date_int) = 6 then date =date_int-1;/*saturday -1 day to get
friday*/
else date =date_int;
format date date_int yymmdd10.;
call symputx('date', ''''||put(date,yymmdd10.)||'''');
run;
%put modfied date is &date;
modified date is '2016-12-29'
Now you can use this macro variable in your pass through.
I am using proc sql on a data set and only want to include values where the date is before 2003. The dates in the dataset are in the format of DDMMMYYYY - for ex. 01JAN2004.
I'm trying to use:
proc sql;
select Name, Department, Join_Date format = WORDDATE.w.
from xxxxxxxxxxxxxxxx
where Join_Date < 01JAN2003;
quit;
But this does not work
Your date value isn't being recognised as such. Try:
proc sql;
select Name, Department, Join_Date format = WORDDATE.w.
from xxxxxxxxxxxxxxxx
where Join_Date < '01JAN2003'd;
quit;
I'm trying to make my SAS Teradata query a little more efficient.
I can get the where timestamp filter in the outer nest to work but it doesnt work when i try to place it in the inner nest. I know i'm overlooking something really simple. Thanks for the help!
SELECT *
FROM CONNECTION TO ODBC
(
SELECT name, ID, timestamp
FROM TD.table
WHERE
timestamp > 1764460800
)
/* where timestamp > 1764460800 */
/*outside nest*/
;
quit;
1764460800 = 11/30/2015
When using pass through, you need to 'pass through' valid syntax for the underlying database. In this case you are looking for:
proc sql;
SELECT *
FROM CONNECTION TO ODBC
(
SELECT name, ID, timestamp
FROM TD.table
WHERE
timestamp > date '2015-11-30' /* Teradata format */
)
where timestamp > 1764460800 /* SAS format */
;
quit;