Struggling with dates formats, want YYYY-MM-DD - date

As an absolute beginner to SAS I quickly ran into problems with date formatting.
I have a dataset containing transaction with three types of dates: BUSDATE, SPOTDATE, MATURITY. Each transaction is represented on two lines, and I want BUSDATE and SPOTDATE from line 1 but MATURITY from line 2.
In the original set, the dates are in YYYY-MM-DD format.
DATA masterdata;
SET sourcedata(rename(BUSDATE=BUSDATE2 SPOTDATE=SPOTDATE2 MATURITY=MATURITY2));
BUSDATE=BUSDATE2;
SPOTDATE=SPOTDATE2;
IF TRANS_TYPE='Swap' THEN;
MATURITY=SPOTDATE;
RUN;
Problem is, this returns something like 17169 (which I guess is the number of days from a certain date).
How can I make it output in YYYY-MM-DD format - or is this approach wrong; should I first convert the date variables to some SAS date format?

if you have valid SAS dates, just add a FORMAT statement to your DATA STEP.
Format busdate spotdate maturity yymmdd10. ;
SAS dates are numeric variables. They represent the number of days since 1/1/1960. You use a FORMAT to display dates.

Adding to CarolinaJay's answer, you normally want to keep them as numeric format, since you can do math (like "# of days since date X") with them. However, if for some reason you need a character variable, you can do this:
date_As_char=put(datevar,YYMMDD10.);
Incidentally, YYMMDD10 will actually give you YYYY-MM-DD, as you asked for; if you want a different separator, see http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000589916.htm (YYMMDDxw. format) - if you put a letter after the last D, for certain letters, you get a different separator. Like, YYMMDDn10. gives you no separator, or YYMMDDs10. gives you slashes. YYMMDDd10. gives you dashes, just like omitting the letter would. This concept also applies to MMDDYY formats, and I think a few others.

Related

What does T mean in "YYYY-mm-DDTHH:MM"?

I am trying to pull some data from Twitter, and the date format is "YYYY-mm-DDTHH:MM". What does T mean in "YYYY-mm-DDTHH:MM"?
The T isn't substituted for a value, it's a character used in the output to designate that the second part is a Time.
For example: 2021-04-20T13:03
The format is part of the ISO 8601 international standard.

Google Sheets - Concatenate NOW-1, slash, and NOW+120

In Google Sheets, I need to create a date in the following format:
2016-06-15T12:00-0800/2016-10-16T12:00-0800
(Yesterday's date / today's date + 120 days)
Using =NOW()-1, I get yesterday's date.
Using Format - Date - More - Year(1930)-Month(05)-Day(01)T:Hour(01):00-0800, I get the proper format for the 1st part of the date range (2016-06-15T12:00-0800).
Repeating the same process with =NOW()+120.
Got the 2nd part of the date range (2016-10-16T12:00-0800).
PROBLEM: Trying to =CONCATENATE(A2,"/",B2), results in this:
42901.6965777315/43022.6965777315
...and no matter what I do - change the format, try to use =CONCATENATE(=TEXT(A2),"/",=TEXT(B2)), or other tricks I know, I either get a blank cell, an error message, or an even worse mess.
All I want is to combine 2 date cells into 1, with a slash in between. How can this be accomplished?
Try join instead of concatenate:
=join("/",A1,B1)
Maybe:
=text(now()-1,"yyyy-mm-ddThh:mm")&"-0800/"&text(now()+120,"yyyy-mm-ddThh:mm")&"-0800"
TEXT with only a date as argument returns the serial number corresponding to that date,
First you should confer the result of you formulas that involves NOW to a formatted text by using TEXT with the second argument. Then you could concatenate the result of that.
The above be donde on a single formula but you maybe should start by doing each step on separate cells in order to make it easy to check the result of each part.

Convert text date format to number date format

I have stored some data entrys in a CSV file in following Format:
Thu Jul 28 08:42:33 GMT+01:00 2016
and need to convert it to just a time stamp (eg. h:m:s). How can I quickly and easily do this?
To convert a text date in a cell to a serial number, you use the DATEVALUE function. Then you copy the formula, select the cells that contain the text dates, and use Paste Special to apply a date format to them. Select a blank cell and verify that its number format is General.
If the size is fixed, then assuming the string is in A1:
=MID(A1,12,8)
The result can then be converted to an actual time value using TIMEVALUE.
Looks like a simple call to a single function would extract the substring you want, since it begins at a specific offset and only runs for eight characters (two hh plus colon plus two mm plus colon plus two ss).

unexpected result converting date using datestr

Can anyone tell me why if I type in MATLAB
datestr('17-03-2016','dd-mmmm-yyyy')
I get
06-September-0022
From the datestr docs
DateString = datestr(___,formatOut) specifies the format of the output text using formatOut. You can use formatOut with any of the input arguments in the above syntaxes.
So in your example the 'dd-mmmm-yyyy' is specifying the output format, not the input format.
Also
DateString = datestr(DateStringIn) converts DateStringIn to text in the format, day-month-year hour:minute:second. All dates and times represented in DateStringIn must have the same format.
where
'dd-mm-yyyy' is not in the list of allowed DateStringIn formats AND the documentation explicitly recommends using datenum to ensure correct behaviour. (Note: I underlined the wrong must in the sentence, it's the second must I wanted to emphasise)
So Sandar_Usama's answer of
datestr(datenum('17-03-2016','dd-mmmm-yyyy'))
is the officially correct method straight out of the docs.
Bottom line, always read the documentation.
Use this instead: datestr(datenum('17-03-2016','dd-mmmm-yyyy'))
To address the last unanswered point in this question, why does datenum behave like this?
>> datestr(datenum('17-03-2016'))
ans =
06-Sep-0022
Without explicitly telling datestr and datenum how it should treat the input, it will try to match against the expected formats. Since none of the documented formats match (see #dan's answer), it fails.
Although what it does next is undocumented, at least up to whatever version of Matlab we are running, it falls into a "last resource" attempt to give you a date number.
Matlab will try to parse different month names from your input, remove non-numeric characters, and then timedate elements from the string. In your case, they are 17, 03, and 2016. The first is expected to be either month or year. Since there's no 17th month, it is treated as year. Then 03 is the month, and 2016 is the day.
Now, March 2016th, 17 is not a valid date, but Matlab will give it a slack and read as 1985 days past March 31st, 17. And that gives us September 6th, 22.
Because Matlab's timestamp is a floating number for the number of days since its epoch, you can trigger that answer, using valid dates, like so:
>> datestr(datenum('0017-03-31') + 1985)
ans =
06-Sep-0022

crystal reports - how to extract a date from string

Using Crystal Reports 2008, I need to extract a date from a text field. This date is usually in the format dd/mm/yy, but could also be entered as d/m/yy, dd/m/yyyy, etc.
This date could appear anywhere within the string.
At the moment I am relying on the fact that the date is placed at the end of the string, without a following fullstop, and using LEFT/RIGHT to extract each date part. These parts are then passed to another formula to create a full date:
Dim AllocationDate() as Date
If Not(IsNull({Command.Notes})) then
Formula = DateValue ((ToNumber ({#Year})), (ToNumber ({#Month})), (ToNumber ({#Day})))
However, if anyone uses a variation of format, adds a fullstop or more notes after the date the whole report keels over.
Is there any way I could extract this date by looking for a pattern? I'm guessing I could the use TRIM to get around the inconsistencies in format.
tyvmia
You may want to consider using a regular expression.
Crystal Reports doesn't have native support for regular expressions, so you'll need to add a UFL: crystal reports : is there a way to regex in crystal reports?
You should be able to adapt the pattern in this question for your needs: Javascript date regex DD/MM/YYYY
Finally, you can test the pattern on your text using regexpal.com.
** edit **
Create a SQL-expression field (Oracle 10 syntax) to extract date string and convert it to a date field:
// {%Allocation Date}
(
-- match date-like string, then convert to date type; is no dates are found, NULL is returned
TO_DATE( REGEXP_SUBSTR( TABLE.FIELD, '\d{1,2}\/\d{1,2}\/\d{2,4}',1 ,1), 'dd/mm/yyyy')
)
While you could
And, as a last resort, you can try converting the multi-line string into a long string by replacing the special characters that represent CR, LF, etc. Replacing them with spaces or another innocuous character, and then treat the resultant string as if it were just a regular string (with the date in the middle).
You would still have to make some assumptions to make this possible: ONE date per string, all special characters are known (or you have to test for all possible special characters), the date has SOME conformity to the format, etc.