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.
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 have a .csv or .txt file which contains a date/timestamp field.
It looks like 'DD/MM/YYYY HH:MM:SS' in the CSV file. When I load this file is from a txt instead of .csv I can see there is trailing fractional seconds that .csv file isn't displaying.
I want the field to be read in as a date/time field 'DD/MM/YYYY HH:MM:SS' (two spaces between date & time) but cant find the correct format to used.
I'm trying to format this field into a usable format and have tried multiple format types, such as DATETIME19., ANYDTM etc, and I have also tried the picture format < https://communities.sas.com/t5/SAS-Data-Management/Timestamp-format-of-YYYY-MM-DD-HH-MM-SS/m-p/314667#M8994>
I currently have the field read in as a character but cant slice the data. Data below is to shown as an example, I'm reading this is from a text file currently.
DATA = '2020-08-24 10:49:17.8653230'
DATA ENTITY;
INFILE ENT
DELIMITER='09'x
DSD
FIRSTOBS=2
TRUNCOVER;
FORMAT CreatedOn $19.;
INPUT CreatedOn $;
RUN;
OUTPUT = '2020-08-24 10:49:17'
I've looked through a lot of online resources and must be missing something as I cant make anything I've tried to work.
I want the fields to be recognized as a date&time field with the DD/MM/YYYY HH:MM:SS format instead of a character field.
I'm running base SAS 9.4.2
Thanks in advance!
Imports fine for me using anydtdtm.
data demo;
example_date = '2020-08-24 10:49:17.8653230';
want = input(example_date, anydtdtm.);
format want datetime22.;
run;
proc print;run;
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')
I got one problem with UNIX time to human readable time. In fact I was also thinking is it possible to conterte UNIX time to Comma-separated-values...so that I can make a plot based on this converted real time and the corresponding values.
But my UNIX is in int64 format, I tried using
unix_epoch = datenum(1970,1,1,0,0,0);
for i=1:1:size(data_mat)
matlab_time(i,1) = data_mat(i,1)./86400 + unix_epoch;
end
for example: 1352434077. So when I tried to converte it with this code: It gave 735087..not something like YYYY-MM-DD: hh:mm:ss ...can anyone gave me a hint? Thanks a lot!
You need to use datestr (the output of which is a string):
>> time_num = 735087
time_num = 735087
>> datestr(time_num)
ans = 06-Aug-2012
You can also specify a format for the output, such as 'dd-mmm-yyyy HH:MM:SS', check the doc.
Hi I have a date conversion problem in SAS,
I imported an excel file which has the following dates.,
2012-01-09
2011-01-31
2010-06-28
2005-06-10
2012-09-19
2012-09-19
2007-06-12
2012-09-20
2004-11-01
2007-03-27
2008-06-23
2006-04-20
2012-09-20
2010-07-14
after I imported the dates have changed like this
40917
40574
40357
38513
41171
41171
39245
41172
38292
39168
39622
38827
41172
40373
I have used the input function to convert the dates but it gives a strange result.,
the code I used.,
want_date=input(have_date, anydtdte12.);
informat want_date date9.; format have_date date9.;run;
I get very stange and out of the World dates., any idea how can I convert these?
You can encourage SAS to convert the data as date during the import, although this isn't necessarily a panacea.
proc import file=whatever out=whatever dbms=excel replace;
dbdsopts=(dbSasType=( datevar=date ) );
run;
where datevar is your date column name. This tells SAS to expect this to be a date and to try to convert it.
See So Your Data Are in Excel for more information, or the documentation.
From : http://www2.sas.com/proceedings/sugi29/068-29.pdf
Times are counted internally in SAS as seconds since midnight and
date/time combinations are calculated as the number of seconds since
midnight 1 January 1960.
Excel also uses simple numerical values for dates and times
internally. For the date values the difference with the SAS date is
only the anchor point. Excel uses 1 January 1900 as day one.
So add a constant.
EXAMPLES:
SAS_date = Excel_date - 21916;
SAS_time = Excel_time * 86400;
SAS_date_time = (Excel_date_time - 21916) * 86400;
As Justin wrote you need to correct for the different zero date (SAS vs. Excel).
Then you just need to apply a format (if you want to get a date variable to do calculations):
want_date = have_date-21916;
format want_date date9.;
Or convert it to a string:
want_date = put(have_date-21916, date9.);
In either case you can choose the date format you prefer.