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.
I am a beginner in SAS and I have a data set of traffic incidents to analyse. I want to filter out the data by time of the day - all incidents before 18:00:00 . or incidents between 9:00:00 - 18:00:00
I have tried to find a suitable code, but have not had any success. Could anybody help out with this? Im using the standard SAS not enterprise guide.
Is it with a WHERE statement? if so, how do I input the time?
I assume from your description you have a data set with a time variable and want to subset it using a hard-coded time of day. For this, it's easiest to use a time literal with standard WHERE processing. A time literal is a time specified in quotes followed by the T character.
For example, you can create something similar to the following that will subset the times data set but only with observations where time is earlier than 18:00:
data times_before_6pm;
set times;
where time < '18:00't; /* restrict to times of day earlier than 6pm */
run;
This assumes your times are time values and not datetime values. If they are datetime values, you'll need to extract the time portion from it (using the TIMEPART() function, which you can do in the WHERE statement).
Hope this helps.
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.
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.');
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