I am trying to pull in a date time variable that is formatted as a char 55. I would like to be able to do it within my initial pull so I can limit on the date.
Here is the format for field I am pulling in.
09/28/2017 00:00:00
TYPE: Char
Len: 55
Format: $55
Informat: $55
I have tried so many things to convert it but with no luck. My variable always comes out blank.
Convert character datetime to numeric using the input function then use the datepart function to retrieve only the date part and apply proper formatting after.
proc sql;
create table want as
select datepart(input(date, anydtdtm.)) as date format=MMDDYY10.
from have;
quit;
proc sql;
connect to hadoop(schema=pharm PROPERTIES='tez.queue.name=user_queue');
create table daily as
select datepart(input(date, anydtdtm.)) as date format=MMDDYY10.
from connection to hadoop
(select date from table limit 100)
;
quit;
Hi I'm working in SAS platform and I've a data_set with more then 30 columns. there are two date columns in that data-set. dates in that data set are in format as 1.33E12
This is the little part of my table
I want to create a new data-set with few columns and then I'm exporting it to excel file.
my code is
dataset
othercolumns | date1 | date2
- 1.33E12 2.53E14
proc sql noprint;
create table my_data_set as
select ID, col_1, col_2, date1, date2
from data_set;
quit;
I want my date values in date1 and date2 column in a readable format like 10feb2017 as date9. SAS date format so they can be exported to my excel file. right now with E power dates I'm getting ####### as date1 and date2 columns in excel
I've tried
select ID, col_1, col_2, datepaart(date1), datepart(date2)
Warning: Invalid Argument, getting '.' values in date column
select ID, col_1, col_2, date1 date9., date2 date9.
select ID, col_1, col_2, date1 DATEw., date2 DATEw.
Error: Syntax error
select ID, col_1, col_2, date1 format=DATE9., date2 format=DATE9.
Getting the same E date values in my table
select ID, col_1, col_2, put(date1 , date9. ), put(date2 , date9.)
Error: Date value out of range
How to convert the E date into a readable format into my table so i can export it to excel?
this is my export code
ods excel file ="C:\data.xlsx";
ods excel close;
proc export
data = work.my_data_set
dbms = xlsx
outfile = "C:\data.xlsx"
replace;
quit;
data have;
unix_ts = 253402300799;
put unix_ts= datetime21.;
sas_dt = unix_ts + '01JAN1970:0:0'DT ;
put sas_dt= datetime21.;
run;
proc sql;
create table want as
select
(
case
when unix_ts + '01JAN1970:0:0'DT > '27FEB8000:0:0'DT then unix_ts + '01JAN1970:0:0'DT - 2 * 86400
when unix_ts + '01JAN1970:0:0'DT > '28FEB4000:0:0'DT then unix_ts + '01JAN1970:0:0'DT - 1 * 86400
else unix_ts + '01JAN1970:0:0'DT
end
) as sas_date format=datetime21.
from have;
quit;
Rather than cutting and pasting you should understand what is going on with the case statement and the 01-JAN-1970
253,402,300,799
Unix timestamp, seconds from 01-JAN-1970, representing 31-DEC-9999:23:59:59
Likely sentinel value contained in valid_to that OP imprecisely shows as
2.534E14
Date columns presumed to be Unix time stamps.
253,717,747,199
SAS datetime value '31-DEC-9999:23:59:59'DT is seconds from 01-JAN-1960
Timestamp conversion
Unix timestamp values are epoch 01-JAN-1970:0:0:0
SAS datetime values are epoch 01-JAN-1960:0:0:0
So one would presume a SAS values are 10 years (in seconds) greater than Unix value.
The simple approach is to add the epoch base differential to the Unix timestamp to achieve the SAS datetime
SAS_DT = UNIX_TS + '01JAN1970'DT; *Naive conversion;
However, this is incorrect because Unix and SAS calendaring disagree on some leap years!
253,402,300,799 is 31-DEC-9999:23:59:59 per https://www.epochconverter.com/
253,717,747,199 is '31-DEC-9999:23:59:59'DT
difference, 315,446,400 should be SAS '01-JAN-1970:0:0'DT. But the difference is actually '30DEC1969:00:00'DT.
So, adding the epoch baseline differential to a far off Unix timestamp will result in a SAS datetime that does not represent the same calendar point as in Unix.
In other words 253,402,300,799 + '01-JAN-1970:0:0'DT is '02-JAN-10000:0:0'DT -- two days beyond the expected Unix sentinel
Or, after about 8,000 years, the calendar accounting systems in Unix and SAS will deviate by 2 days.
Calendaring deviation
Unix calendaring considers year 4000 to be a leap year, 29-FEB-4000 is valid.
SAS calendaring incorrectly considers 4000 to be a non-leap year, '29-FEB-4000'DT is invalid.
ly4000 = '29-FEB-4000:0:0'DT;
-------------------
77
ERROR: Invalid date/time/datetime constant '29-FEB-4000:0:0'DT.
ERROR 77-185: Invalid number conversion on '29-FEB-4000:0:0'DT.
The same deviation happens again in year 8,000.
The least damaged conversion of Unix timestamp to SAS datetime takes the naïve conversion and subtracts one day for each misaligned leap-day determination in the time frame.
It looks as though you have two different types of UNIX timestamps, which count the number of milliseconds or microseconds from a particular date and time - usually 1st January 1970 00:00:00.000000. Without knowing exactly what sort of timestamps they are, I can only make an educated guess as to how to convert them to human-readable dates. Here are a few possible interpretations:
data example;
date1=2.53e14;
date2=1.33e12;
run;
proc sql;
create table want as
select
intnx('year',datepart(date/1e3),10,'s') format = yymmdd10. as date1a,
intnx('year',datepart(date/1e6),10,'s') format = yymmdd10. as date1b,
intnx('year',datepart(date2/1e3),10,'s') format = yymmdd10. as date2a,
intnx('year',datepart(date2/1e6),10,'s') format = yymmdd10. as date2b
from example;
quit;
The logic here is:
Divide by 1000 or 1000000 to convert to seconds
Interpret the result as a SAS datetime value counting the number of seconds from 1st January 1960 00:00:00
Extract the date component from the datetime
Add 10 years to convert to the UNIX epoch
Hopefully one of these looks right to you.
I've a table named cust_field_value_l in sas server under Sasoprsk library having a column name value_dt who store date data.
When I get data from this table it comes in binary data as 1.827E9 or 1.826E9 and gives ***** when i print it in my webout file.
proc sql noprint;
create table date(date datetime);
quit;
proc sql noprint;
insert into date(date)
select value_dt
from Sasoprsk.cust_field_value_l;
quit;
Now my date table has data as 1.826E9 etc.
data _null_;
put "<html><body>";
run;
data _null_;
put numberOfObservations=;
set work.date nobs=numberOfObservations;
file _webout;
put date;
run;
data _null_;
put "</body></html>";
run;
I'm receiving ***** in my webout file
I've tried this too but it only giving me wrong date
proc sql noprint;
insert into date(date)
select (today()-datepart((value_dt / 1000.0)+315601200))
from Sasoprsk.cust_field_value_l;
quit;
this code giving me only 03NOV07 for all the date values.
I don't know how can i get correct date value from db and show them in webout file;
value_dt has all the values in 23NOV2017:00:00:00 formate.
value_dt
23NOV2017:00:00:00
15NOV2017:00:00:00
20NOV2017:00:00:00
without comparing with today()
date
1.827E9
1.826E9
1.827E9
and its giving me ***** for every row
with comparing with today()
date
03NOV07
03NOV07
03NOV07
and printing 03NOV07 for every date data
SAS only has two data types. Fixed length character strings and floating point numbers. DATE values are stored as the number of days since '01JAN1960'd and DATETIME values are stored as the number of seconds since '01JAN1960:00:00'dt. The numbers you are displaying are consistent with a datetime value. The TODAY() function (also know as the DATE() function) will return a DATE value. If you want a DATETIME value use the DATETIME() function instead.
If you want users to supply date values (like '03NOV2007'd) to filter your datetime variable then you will need to convert one or the other before comparing them. Say you have a datetime variable name TIMESTAMP and you want to find events that occured today you could use something like
where today() = datepart(timestamp)
If you are creating a new data set by reading values from an existing dataset then just use a DATA step. No need to pre-creating the dataset and then "inserting" the observations into it. Or to bother with using PROC SQL. Just create the dataset you want.
data date ;
set Sasoprsk.cust_field_value_l ;
keep value_dt ;
rename value_dt=date ;
format value_dt datetime20. ;
run;
But if the whole purpose is just to print the values to the _WEBOUT fileref then there is no need to make a copy of the data first. Just print directly from the source data.
data _null_;
put numberOfObservations=;
set Sasoprsk.cust_field_value_l nobs=numberOfObservations;
file _webout;
put value_dt datetime20.;
run;
If you only want to print the date part of the datetime value then you can use the DTDATE9. format instead.
Having some issues deleting rows from a data set. They need to be deleted by a date criteria, but the variable is in e8601dt. format. One thing I noticed about the variable is that its a number type variable, but left aligned (not sure if that has relevance or not), so I attempted to substring, and some additional attempts (below)...no success -
PROC SQL;
DELETE *
FROM DATASETS.BATCH_REPORT
WHERE datepart(BATCH_DATE) > '2015-10-01'
;
QUIT;
PROC SQL;
DELETE *
FROM DATASETS.BATCH_REPORT
WHERE BATCH_DATE > '11oct2015'd
;
QUIT;
Assuming there has to be an easy way to call out a value in this format...or will I need to convert this variable to a more compliable format, then do my processing?
OK...did some research. Apparently (and some one please correct me if I am wrong)...to use the e8601dt. format, a date value needs to be multiplied by 86400, then you can apply the format. So.....dividing by 86400 brought me back to the SAS data as an integer. This did the trick :
PROC SQL;
DELETE *
FROM SETS
WHERE ID >= 20372
;
QUIT;
You're close! Date conversions are a pain between systems. The representation of the values depends on the environment configuration.
Within proc SQL, I think you have to specify oracle functions (not the SAS datepart) Looks like you've figured out that Oracle's 'DATE' datatype stores both date&time within the same value. The DATE datatype stores the year (including the century), the month, the day, the hours, the minutes, and the seconds (after midnight). SAS has 2 different date types: date and datetime.
I'd suggest using the oracle to_date() function to compare against a character date, i.e.
WHERE BATCH_DATE > to_date('2015-10-01','yyyy-mm-dd')
If desired, you could use the oracle to_char(BATCH_DATE,'mm-dd-yyyy') to cast the date variable to a text value and then compare on the text value. But you loose some of the comparison power.
....edited due to new info about ...ew... db2 ..... :-)
I'm way NOT a DB2 guy, but maybe something like this?
First, set the date as in: (the date passed to DB2 needs the double quotes):
CALL SYMPUT('INT_DATE',"'"||PUT(sas_date,YYMMDDD10.)||"'");
Then use in the SQL as in:
PROC SQL ;
WHERE BATCH_DATE >= &INT_DATE
https://communities.sas.com/t5/SAS-Procedures/DB2-Date9-format-To-SAS-Serial-Date/td-p/32436
I have a table A, which has a column fromdate.
select to_date(fromdate,'DD-Mon-YYYY') from A
returns value as 31-AUG-99
But I need to know whether the date is 31-AUG-1999 or 31-AUG-2099
Is there any one to help?
Use to_char function to get the date in character format.
Try this:
select to_char(fromdate,'DD-Mon-YYYY') from A;
Or if you want to want it in date then you have to change nls date settings.
alter session set nls_date_format = 'DD-Mon-YYYY'
before executing your original posted query.