how to convert a string date variable into datetime variable in sas - datetime

I have a string variable for datetime. Sometimes it is a whole number like 3040000 sometimes a decimal value like this 3130215.123.
I would like to convert this into a date time variable like mm-dd-yyyy.
Thanks in advance.
add: I think the value 3130215.123 refers to feb-15-2013 12:30:00.

I think these are
3YYMMDD.HHMM
3130215.123 -> 3|13|02|15|12|30
(the last 0 is assumed).
So, you need:
length year month day hour minute second $2;
year=substr(dtvar,2,2);
month=substr(dtvar,4,2);
day=substr(dtvar,6,2);
hour=translate(subpad(dtvar,9,2),'0',' ');
minute=translate(subpad(dtvar,11,2),'0',' ');
second=translate(subpad(dtvar,13,2),'0',' ');
then
new_dtvar=dhms(mdy(month,day,year),hour,minute,second);

Dates, Times, and Datetimes are stored as doubles. Only the format matters for display.
try
var1 = input(var,best.);
format var1 datetime19.;
in a data step to apply the format. Then look at the results.

Based on comments on the original question, the statements would be:
format var_date ddmmyyd10. var_time time5. var_datetime datetime19.;
var_date=input(put(int(17000000+(var)),10.),yymmdd10.);
var_time=input(put((var-int(var))*1e6,z6.),hhmmss6.);
var_datetime=dhms(var_date,hour(var_time),minute(var_time),0);
Haven't had a chance to test, so feel free to comment with any errors you get.

Related

Changing a string of factors to time

I am trying to take a column of my data that is in factor format and change it to time in the format
hours:minutes:seconds:milliseconds
I tried:
start.times <- as.POSIXct(as.character(start.times), format="%H:%M:%OS")
but it returned values with todays date and left out the milliseconds in them and that is not what I want.
I also tried downloading chron and running the code:
start.times <- times(start.times)
but this just returned NA's.....
Please help!
My data is all about start times and end times of dolphin vocalizations and I am trying to find the mean whistle duration and the inter whistle interval. Anyways, I don't really know how to get my data into the format I need it in. Thank you!
Assuming you have a factor that looks like:
start.time <- c("0:13:45.9", "3:09:44.9")
Then what you wrote should work if you change the last colon to a period
as.POSIXct(start.time, format ="%H:%M:%S.%OS")

SAS Datetime and IF statements

Hello I am trying to set a date 01/02/2015 to 01/09/2015. I am using a IF statement but I think since the column has as format of mmddyy10 and informat datetime20 its not working.
The column looks like this
01/09/2015
01/02/2015
05/23-2015
So I want it to look like this
01/09/2015
01/09/2015
05/23-2015
Is there a function in SAS that allows me to do this? Or will I have to convert the column to a Char and do my If statement that way.
As long as it's a date value - so the unformatted value is something around 20000 - you can use date constant.
data want;
set have;
if datevar = '02JAN2015'd then datevar='09JAN2015'd;
run;
This is irrespective of the format applied to the column. Format is not variable type; it's something like "When I look at this variable, please print it nicely using this format." The underlying value is the same for all dates: some number of days since 1/1/1960.
Date constants are always represented using DATE9. format (DDMONYYYY).
If it's actually a datetime, then the format would not be MMDDYY10. but something else. Then you use a datetime constant:
if datetimevar = '02JAN2015:00:00:00'dt then ...
or convert it to date using datepart first.
if datepart(datetimevar) = '02JAN2015'd then ...

convert string to time in r

I have an array of time strings, for example 115521.45 which corresponds to 11:55:21.45 in terms of an actual clock.
I have another array of time strings in the standard format (HH:MM:SS.0) and I need to compare the two.
I can't find any way to convert the original time format into something useable.
I've tried using strptime but all it does is add a date (the wrong date) and get rid of time decimal places. I don't care about the date and I need the decimal places:
for example
t <- strptime(105748.35, '%H%M%OS') = ... 10:57:48
using %OSn (n = 1,2 etc) gives NA.
Alternatively, is there a way to convert a time such as 10:57:48 to 105748?
Set the options to allow digits in seconds, and then add the date you wish before converting (so that the start date is meaningful).
options(digits.secs=3)
strptime(paste0('2013-01-01 ',105748.35), '%Y-%M-%d %H%M%OS')

Time series (xts) strptime; ONLY month and day

I've been trying to do a time series on my dataframe, and I need to strip times from my csv. This is what I've got:
campbell <-read.csv("campbell.csv")
campbell$date = strptime(campbell$date, "%m/%d")
campbell.ts <- xts(campbell[,-1],order.by=campbell[,1])
First, what I'm trying to do is just get xts to strip the dates as "xx/xx" meaning just the month and day. I have no year for my data. When I try that second line of code and call upon the date column, it converts it to "2013-xx-xx." These months and days have no year associated with them, and I can't figure out how to get rid of the 2013. (The csv file I'm calling on has the dates in the format "9/30,10/1...etc.)
Secondly, once I try and make a time series (the third line), I am unsure what the "order.by" command is calling on. What am I indexing?
Any help??
Thanks!
For strptime, you need to provide the full date, i.e. day, month and year. In case, any of these is not provided, current ones are assumed from the system's time and appended to the incomplete date. So, if you want to retain your date format as you have read it, first make a copy of that and store in a temporary variable and then use strptime over campbell$date to convert into R readable date format. Since, year is not a concern to you, you need not bother about it even though it is automatically appended by strptime.
campbell <-read.csv("campbell.csv")
date <- campbell$date
campbell$date <- strptime(campbell$date, "%m/%d")
Secondly, what you are doing by 'the third line' (xts(campbell[,-1],order.by=campbell[,1])) command is that, your are telling to order all the data of campbell except the first column (campbell[,-1]) according to the index provided by the time data in the first column of campbell (campbell[,1]). So, it would only work given the date is in the first column.
After ordering the data according to time-series, you can replace back the campbell$date column with date to get back the date format you wanted (although here, first you have to order date also like shown below)
date <- xts(date, order.by=campbell[,1]) # assuming campbell$date is campbell[,1]
campbell.ts <- xts(campbell[,-1], order.by=campbell[,1])
campbell.ts <- cbind(date, campbell.ts)
format(as.Date(campbell$dat, "%m/%d/%Y"), "%m/%d")

Subtract 1 year from datetime

I have seen a lot of info on how to subtract one datetime from the other and how to add years on to a datetime. but the following is giving me a headache....
When a user inserts a record a hidden field called subdate with a value of datetime.now is added to the db.
I then need to have an 'old records' page that lists all the entries that are over 1 year old and was hoping to use something (but using subtract method) similar to;
(DateTime.Now.AddYears(1))
but there is no SubtractYears available? Why?
Please can you let me know how I achieve the same result?
DateTime.Now.AddYears(-1)
From the documentation:
A number of years. The value parameter can be negative or positive.
now = datetime.now()
last_year = (now.year - 1)
datestr = (datetime.strptime(str(now.year - 1), "%Y")).strftime("%y")
print(f"Last Year: {datestr}")
The output will be:
Last Year: 20
If you prefer to have four digit year then change %y to %Y

Resources