In my code below I am unable to convert the date format in column 1 from dd/MM/yyyy to MM-dd-yyyy. How do I configure it correctly to stop seeing this error??
>> T = readtable("IGEv3.xlsx");
>> tday=T{2:end, 1};
>> tday=datestr(datenum(tday, 'dd-MM-yyyy'), 'MM-dd-yyyy')
Error using datetime/datenum
Too many input arguments.
That looks like your Date column contains datetime values instead of strings or datenum doubles. (Strings or cellstrs show up with quotes in table display output. And the error message "Error using datetime/datenum" means that you're calling datenum() on a datetime input.)
datetime values support datestr calls directly. You can probably just do this:
tday=datestr(T{2:end,1}, 'mm-dd-yyyy')
And personally, I find it more readable when you reference variables by name instead of index:
tday = datestr(T.Date(2:end), 'mm-dd-yyyy')
Related
I've looked for help on the internet for the following, but I could not find a satisfying answer: for an assignment, I need to plot the time series of a certain variable (the term spread in percentages), with years on the x-axis.
However, we use daily data. Does anybody know a convenient way in which this can be done? The 'date' variable that I've got is formulated in the following way: 20111017 represents the 17th of October 2011.
I tried to extract the first 4 numbers of the variable 'date', by using the substr(date, 1, 4) command, but the message 'type mismatch' popped up. Also, I'm not quite sure if it gives the right information if I only use the years to plot daily data (over the years). It now gives the following graph, which doesn't look that nice.
Answering the question in your title.
The date() function expects a string. If your variable with value 20111017 is in a numeric format you can convert it like this: tostring datenum , gen(datestr).
Then when using the date() function you must provide a mask that tells Stata what format the date string is in. Below is a reproducible example you can run to see how this works.
* Example generated by -dataex-. For more info, type help dataex
clear
input float datenum
20111016
end
* Convert numberic varaible to string
tostring datenum , gen(datestr)
* Convert string to date
gen date = date(datestr, "YMD")
* Display date as date
format date %td
If this does not help you, try to provide a reproducible example.
This adds some details to the helpful answer by #TheIceBear.
As he indicates, one way to get a Stata daily date from your run-together date variable is convert it to a string first. But tostring is just one way to do that and not essential. (I have nothing against tostring, as its original author, but it is better suited to other tasks.)
Here I use daily() not date(): the results are identical, but it's a good idea to use daily(): date() is all too often misunderstood as a generic date function, whereas all it does is produce daily dates (or missings).
To get a numeric year variable, just divide by 10000 and round down. You could convert to a string, extract the first 4 characters, and then convert to numeric, but that's more operations.
clear
set obs 1
gen long date = 20111017
format date %8.0f
gen ddate = daily(strofreal(date, "%8.0f"), "YMD")
format %td ddate
gen year = floor(date/10000)
list
+-----------------------------+
| date ddate year |
|-----------------------------|
1. | 20111017 17oct2011 2011 |
+-----------------------------+
I'm trying to get DAYID as string in format YYYYMMDD, however its not working correctly.
I have a timestamp field, I take first 10 characters and convert it into date (working correctly)
toDate(substring(EventTimestamp,1,10))
-- 2021-03-24
However when I try to convert to string using below expression, I;m getting wrong answer. I;m getting the Day as 83.
toString(toDate(substring(EventTimestamp,1,10)),'YYYYMMDD')
--20210383
Date format characters are case-sensitive as per this answer here, so use yyyyMMdd instead.
An example using formatDateTime:
#formatDateTime(utcnow(), 'yyyyMMdd')
I currently have a dataset with dates in the format "FY15 FEB". In attempting to format this variable for use with SAS's times and dates, I've done the following:
data temp;
set pre_temp;
yr = substr(fiscal,3,2);
month = substr(fiscal,6,length(fiscal));
mmmyy = month||yr;
input mmmyy MONYY5.;
datalines;
run;
So, I have the strings representing the year and corresponding month. However, running this code gives me the error "The informat $MONYY was not found or could not be loaded." Doing some background on this error tells me that it has something to do with passing the informat a value with the wrong type; what should I alter in order to get the correct output?
*Edit: I see on the SAS support page for formats that "MONYYw. expects a SAS date value as input;" given this, how do I go from strings to a different date format before this one?
When you see a $, it means character value. In this case, you're feeding SAS a character value and giving it a numeric format. SAS inserts the $ for you, but there is no such format in existence.
I'm going to ignore the datalines statement, because I'm not sure why it's there (though I do notice there is no set statement). You might have an easier time just changing your program to:
data temp;
yr = substr(fiscal,3,2);
month = substr(fiscal,6,length(fiscal));
pre_mmmyy = strip(month)||strip(yr);
mmmyy=input(pre_mmmyy,MONYY5.);
run;
you can also remove the "length(fiscal))" from the substring function. The 3rd argument to the substring function is optional, and will go to the end of the string by default.
I am trying to use datenum to get a datenumber for a set of dates from a cell. Automatic datenum works but it only gets times correctly using conversion, and not days.
The date format is like this: yyyy-mm-dd hh:mm:ss.000
And I have tried using
X_time(:,1) = datenum(Y_time, 'dd/mm/yyyy HH:MM:SS')
to no avail:
Error using datenum (line 178)
DATENUM failed.
Caused by:
Error using dtstr2dtnummx
Failed on converting date string to date number.
Your format string for datenum must exactly match the format of the string you're inputting, and no field can be specified more than once. When defining datenum format strings, it is therefore important to differentiate between MM (minutes) and mm (month).
So yyyy-mm-dd hh:mm:ss.000 becomes yyyy-mm-dd HH:MM:SS.FFF
While months and minutes are the only ones that have this overlap problem, it is standard to use lower case for dates and upper case for times in the format string.
At the moment I am using xlsread to open a set of data that I have in excel with given timestamps. But when these values are placed in matlab it changes the formatting of the timestamp.
In excel it is:
dd/mm/yyyy HH:MM
but when it puts it into matlab it changes it to
mm/dd/yyyy HH:MM
which ruins my other code. I have tried using formatIn and specifying it, but then it returns an error if no value for midnight is given.
Any help would be appreciated.
You can use datenum and datestr to convert the format to what you want. In the following example I'm assuming your timestamps are contained in a cell array of strings, but it also works if it's a char matrix:
>> timestamps = {'08/25/2014 13:14'; '08/26/2014 14:15'} %// mm/dd/yyyy HH:MM
>> result = datestr(datenum(timestamps, 'mm/dd/yyyy HH:MM'), 'dd/mm/yyyy HH:MM')
result =
25/08/2014 13:14
26/08/2014 14:15
What Luis recommended should help you to get any format that you like. However there is something important to realize here:
Excel does not 'have' the date in your format. It has the date stored as a number like 123546.123 and presents it to you in a certain way.
If you want to get the date in exactly the way that excel presents it, the trick is to avoid importing the relevant column as a date, but just import it as text instead.
How to do this depends on your import method, but it should not be very hard.