SAS Date Format conversion - sas

I've recently started using SAS Enterprise guide and would like to convert the date "01MAR2014" to "MAR 2014". I've been googling but unfortunately no luck.
I've tried the following however it just returns an
SELECT input(put(StartDate,EURDFMYw.),9.) AS Order_DDD_YY
Does anyone know how to convert this?

If your date is a SAS date, i.e. a numeric with date format applied then you can change the display by using the monyy5. format instead. Note that this will create a character variable that will no longer sort properly in reports/datasets, it will sort alphabetically instead of a date order.
put(startDate, monyy7.) as Order_DDD_YY
Otherwise you can apply the format instead of converting it.

Related

Amazon Athena - Convert String to Date

I would like to convert some dates stored as a string to a date. The data looks as follows: 26APR2017:06:01:44. I would prefer it to be in any regular date format.
Can someone please help me to convert this to a date format? I'm sure the dateparse function is useful here and have used it before but am unsure how to adjust it for this particular data.
You can use date_parse() to parse and date() to cast timestamp retunred from parse function to date.
select date_parse('26APR2017:06:01:44','%d%b%Y:%H:%i:%s') will give you
2017-04-26 06:01:44.000
select date(date_parse('26APR2017:06:01:44','%d%b%Y:%H:%i:%s')) will give you
2017-04-26

Finding dates between two dates not working

I have a dataset where date_occur is in MMDDYY10 format (looks like 10/23/2014). I want all dates in FY17 which is 10/01/2016-09/30/2017. The following code is not working for some reason. Why!!
I'm not getting any error or warning messages I'm just getting an empty table.
Thanks in advance
data fy17;
set y16_17;
where date_occur between 10/01/2016 and 09/30/2017;
run;
The Log says:
NOTE: There were 0 observations read from the data set WORK.Y16_17.
WHERE (date_occur>=0.0001487357 and date_occur<=0.0049603175);
I'm thinking SAS is not understanding that date_occur is in mmddyy10 format. The internet has code like
where date_occur between '10/01/2016'd and '09/30/2017'd;
I tried this and it did not work either.
Your 'dates' are being interpreted as numeric expressions, and none of the date values in your table lie in the resulting range. Use date literals instead:
data fy17;
set y16_17;
where date_occur between '01oct2016'd and '30sep2017'd;
run;
You can only use date. format (ddmmmyyyy / ddmmmyy) when specifying date literals.

how to convert char date to sas date , tried many ways.

I have a data which is in one column which has a mix of all data types, formats.
text, numeric and also date.
I have to transpose the coloumn to rows.
When I import the data in to sas 9.3 the dates are turning into a number (char format) and after transpose when I try to convert using the input function it is not giving the correct date.
The observation I am getting is like 41172 in the date column.
How do I convert it?
Think you're confusing data types and formats. The two types are character and numeric.
Dates are all numeric, and are stored as a SASDATE which is the offset from 01 JAN 1960, with each day representing 1.
To have each SASDate be displayed, you must format that variable as a date (eg. date7, ddmmyy etc). That means you first need to use the input function to go from text to numeric (SASDate) then the put function to format it as you want it.
If you're using the input function, you just need to add one of those formats and a put function:
newvar=put(input(var,8.),date7.);
Or you could leave it as just the input() and use this:
format var date7.;
In your data step.

converting format (from date to numeric) using SAS

I am working with a dataset in SAS, containing many variables.
One of these variables is a DATE variable and it has a date/time format. It looks like this:
12FEB97:00:00:00
27MAR97:00:00:00
14APR97:00:00:00
Now the thing is that I would like to convert this variable into a NUMERIC format. And I would like to get the following result (based on the previously shown 3 examples):
199702
199703
199704
Do you have any ideas how to do it? I already read through many docs, pdfs etc. but still could not find a proper solution yet.
Thank you very much!
First, you need to extract the date from your datetime variable. You can use the datepart function:
date = datepart(var);
Then, you want to put this variable (which will still be coded as a date number) into a number that you can read the year and month. Do this with putn:
date_as_num = putn(date,'yymmn6.');

Converting a string to a date in DB2

I am working with a DB2 database for the first time.
I am trying to work with DB2 dates, but the data is stored as a string in the DB2 database.
I want to convert this date-string into an actual date, preferably dropping off time because I want all transactions between 1 Jan 2011 and 26 Jan 2011.
So essentially, I want this MS SQL statement in DB2 magic...
CONVERT(datetime,SETTLEMENTDATE.VALUE,103)
For background, I have got as far as
CAST(SETTLEMENTDATE.VALUE, DATE)
AND
DATE(SETTLEMENTDATE.VALUE)
But I need the expert knowledge of a DB2 whizzkid!
Thanks
Based on your own answer, I'm guessing that your column has data formatted like this:
'DD/MM/YYYY HH:MI:SS'
The actual separators between Day/Month/Year don't matter, nor does anything that comes after the year.
You don't say what version of DB2 you are using or what platform it's running on, so I'm going to assume that it's on Linux, UNIX or Windows.
Almost any recent version of DB2 for Linux/UNIX/Windows (8.2 or later, possibly even older versions), you can do this using the TRANSLATE function:
select
date(translate('GHIJ-DE-AB',column_with_date,'ABCDEFGHIJ'))
from
yourtable
With this solution it doesn't matter what comes after the date in your column.
In DB2 9.7, you can also use the TO_DATE function (similar to Oracle's TO_DATE):
date(to_date(column_with_date,'DD-MM-YYYY HH:MI:SS'))
This requires your data match the formatting string; it's easier to understand when looking at it, but not as flexible as the TRANSLATE option.
I know its old post but still I want to contribute
Above will not work if you have data format like this 'YYYMMDD'
For example:
Dt
20151104
So I tried following in order to get the desired result.
select cast(Left('20151104', 4)||'-'||substring('20151104',5,2)||'-'||substring('20151104', 7,2) as date) from SYSIBM.SYSDUMMY1;
Additionally,
If you want to run the query from MS SQL linked server to DB2(To display only 100 rows).
SELECT top 100 * from OPENQUERY([Linked_Server_Name],
'select cast(Left(''20151104'', 4)||''-''||substring(''20151104'',5,2)||''-''||substring(''20151104'', 7,2) as date) AS Dt
FROM SYSIBM.SYSDUMMY1')
Result after above query:
Dt
2015-11-04
Hope this helps for others.
In format function your can use timestamp_format function.
Example, if the format is YYYYMMDD you can do it :
select TIMESTAMP_FORMAT(yourcolumnchar, 'YYYYMMDD') as YouTimeStamp
from yourtable
you can then adapt then format with elements format foundable here
Okay, seems like a bit of a hack. I have got it to work using a substring, so that only the part of the string with the date (not the time) gets passed into the DATE function...
DATE(substr(SETTLEMENTDATE.VALUE,7,4)||'-'|| substr(SETTLEMENTDATE.VALUE,4,2)||'-'|| substr(SETTLEMENTDATE.VALUE,1,2))
I will still accept any answers that are better than this one!
You can use:
select VARCHAR_FORMAT(creationdate, 'MM/DD/YYYY') from table name