Converting non-standard date into SAS date in SAS - date

I have a date variable in the data. The value in the variable are not well formatted. I want to convert them into SAS date format like datetime9. .
date
Nov 2 2013 10:00AM
Oct 6 2012 11:00AM
Aug 26 2002 18:00PM
Mar 22 2012 07:00AM

There are a few functions to know about.
scan(str,n) -- this pulls the nth word from a string.
catt(str1,str2,str3) -- this concatenates strings.
input(str,informat) -- this converts a string to a number using the informat
dhms(d,h,m,s) -- Day Hour Minute Second. A date, plus time parts equals a datetime.
Also DDMONYYYY is informat = date9.
HH:MM <AM|PM> is informat = time.
So...
data test;
format date $20.;
date = "Nov 2 2013 10:00AM"; output;
date = "Oct 6 2012 11:00AM"; output;
date = "Aug 26 2002 18:00PM"; output;
date = "Mar 22 2012 07:00AM"; output;
run;
data test;
set test;
format day date9. time time. date2 datetime.;
day = input(catt(scan(date,2),scan(date,1),scan(date,3)),date9.);
time = input(scan(date,4),time.);
date2 = dhms(day,hour(time),minute(time),second(time));
run;

data any;
attrib datevar format=date9.;
infile cards;
input datevar ANYDTDTM20.;
datevar=datepart(datevar);
cards;
Nov 2 2013 10:00AM
Oct 6 2012 11:00AM
Aug 26 2002 18:00PM
Mar 22 2012 07:00AM
;run;

Related

Is there a way to make custom date formats in Julia?

I would like to be able to convert
"Tue Sep 01 00:00:26 +00:00 2020"
into a date type in Julia using the built in Date function.
I only need the year, month, and date.
This is tricky because you have a time zone here hence you need to use TimeZones.jl
using TimeZones, Dates
df = Dates.DateFormat("e u d H:M:S z y");
d = ZonedDateTime("Tue Sep 01 00:00:26 +00:00 2020", df)
Let se what we got:
julia> d = ZonedDateTime("Tue Sep 01 00:00:26 +00:00 2020", df)
2020-09-01T00:00:26+00:00
julia> Date(d)
2020-09-01
For more try typying ?DateFormat in the console - you will see the docs.
Code Matches Comment
–––––––– ––––––––– ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
y 1996, 96 Returns year of 1996, 0096
Y 1996, 96 Returns year of 1996, 0096. Equivalent to y
m 1, 01 Matches 1 or 2-digit months
u Jan Matches abbreviated months according to the locale keyword
U January Matches full month names according to the locale keyword
d 1, 01 Matches 1 or 2-digit days
H 00 Matches hours (24-hour clock)
I 00 For outputting hours with 12-hour clock
M 00 Matches minutes
S 00 Matches seconds
s .500 Matches milliseconds
e Mon, Tues Matches abbreviated days of the week
E Monday Matches full name days of the week
p AM Matches AM/PM (case-insensitive)
yyyymmdd 19960101 Matches fixed-width year, month, and day
To parse dates you need the Dates.jl standard library. To parse this particular format though, I think you additionally need the TimeZones.jl package:
using Dates
using TimeZones # gives the `z` for the format below
fmt = dateformat"e u d H:M:S z y" # the format of your string
d = Date("Tue Sep 01 00:00:26 +00:00 2020", fmt)
Then you can simply look at d's values for example with:
julia> d
2020-09-01
julia> year(d)
2020
julia> month(d)
9
julia> day(d)
1

Sas changing of date format

I have three columns with date formatted differently in SAS:
12 june 2017 00:15 - full date
2016 - only year
12 - only month
I Need to change the format of date and subtract after the dates to get results in the number of months.
for instance, "12 June 2017 00:15" - December 2016 = 7
how to do it?
As you have probably already found, there isn't a ready-made SAS date informat that will correctly handle your full date field, so you'll need to write a bit of custom logic to convert it before doing your calculation. date9. is the closest matching format I could find:
data example;
fulldate = '12 june 2017 00:15';
year = 2016;
month = 12;
/* Convert string to date9 format and input */
fulldate_num = input(
cats(
scan(fulldate,1),
substr(scan(fulldate,2,' '),1,3),
scan(fulldate,3)
), date9.
);
/* Calculate difference in months */
monthdiff = intck('month', mdy(month,1,year), fulldate_num);
run;
Convert the "full date" field to a SAS date value.
Convert the combo of year and month to a SAS date value, too.
Use the INTCK function to find the difference in months.
For example:
data dates ;
input dt $18. yy mm ;
mm_diff = intck ("mon", input (cats (yy, mm), yymmn6.), input (dt, anydtdte12.)) ;
put mm_diff= ;
cards ;
12 june 2017 00:15 2016 12
11 june 2018 00:15 2017 3
;
run ;
The log will print:
mm_diff=6
mm_diff=15
As a side note, the statement "there isn't a ready-made SAS date informat that will correctly handle your full date field" made elsewhere in this thread is incorrect. As the program snippet above shows, the ANYDTDTEw. informat handles it with aplomb. It's just incumbent upon the programmer to supply a sufficient informat width W. Above, it is selected as W=12. If you're reluctant to guess and/or count, just use ANYDTDTE32.
Regards,
Paul Dorfman
Assuming that you have three numeric variables and the first one contains valid SAS datetime values you should first convert both to valid SAS date values. You can then use the INTCK() function to count months.
nmonths = intck('month',datepart(VAR1),mdy(VAR3,1,VAR2));

Date minus date is not giving desired answer - IONIC 2

When I try to subtract
Wed Dec 06 2017 15:58:59 GMT+0530 (India Standard Time) minus Tue Nov 28 2017 00:00:00 GMT+0530 (India Standard Time) , the answer which is coming is -22
But the answer should be 6
What is going wrong and where, below is my page.ts code:
this.tt = new Date();
this.tt1 = this.datePipe.transform(this.tt,'dd/mm/yyyy');
console.log(this.ent[0],"server DATE");
// in console we see this - 28-NOV-17 server DATE
var firstDate= new Date(this.ent[0]); //Jan 01 2017 00:00:00
var secondDate = new Date();//Jan 04 2017 00:00:00
console.log(firstDate);
// answer in console - Tue Nov 28 2017 00:00:00 GMT+0530 (India Standard Time)
console.log(secondDate);
//answer in console - Wed Dec 06 2017 15:58:59 GMT+0530 (India Standard Time)
console.log(secondDate.getDate() - firstDate.getDate() );
//answer in console - -22
Date.getDate() gives the "dd" of the date (in your case 6 and 28 which explains the result being -22).
I'm a bit confused with the expected result being 6. So maybe my answer won't fit you. What I would do however is convert the date in time, do the substraction and convert it back to number of days.
So
Math.Floor((secondDate.getTime() - firstDate.getTime()) / 86400000);
(86400000 being 1000 (milliseconds) * 3600 (seconds in an hour) * 24 (number of hours in a day)
you can convert both dates to timestamp and subtract the timestamp you will get the result days in millis now convert it to days
getTimestamp(dateParam:string):string{
var date = new Date(dateParam); // some mock date
var milliseconds = date.getTime();
return milliseconds.toString();
}
var one_day=1000*60*60*24;
console.log(Math.ceil(getTimestamp(secondDate) - getTimestamp(firstDate))/(one_day) );

Issue saving a string as ISO date format

I'm trying to save a date to MongoDB from FullCalendar in my Grails application.
I'm trying to parse the string 2015-12-27T00:00:00.000Z into the below format:
def startDate = new Date().parse("YYYY-MM-dd'T'HH:mm:ss.SSSXXX",it.start)
def endDate = new Date().parse("YYYY-MM-dd'T'HH:mm:ss.SSSXXX",it.end)
But, weirdly when I print the formatted date, I get Sun Dec 28 05:30:00 IST 2014. I don't know what or how that particular date is picked.
You should use lowercase y for year. Uppercase Y is for "Week year".
new Date().parse("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", "2015-12-27T00:00:00.000Z")
===> Sat Dec 26 19:00:00 EST 2015
import java.text.SimpleDateFormat;
println new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX").parse("2018-07-30 09:57:15 +0800")

How to format a date in Ext JS 3?

I have a date value like this ;
Date {Fri Feb 13 2015 02:00:00 GMT+0200 (GTB Standart Saati)}
I get it from a grid column with ;
selectionModel.getSelected().data['date'];
And column model date format is 'd/m/Y'. It shows in grid well (d/m/Y).But when i get selected value it returns a date format doesn't like 'd/m/Y'. How can i format this date to set to a textfield ?
According to ExtJs Docs
var d = new Date(1993, 6, 28, 14, 39, 7);
println(d.toString()); // prints Wed Jul 28 1993 14:39:07 GMT-0600 (PDT)
println(d.toDateString()); // prints Wed Jul 28 1993
Edit:
Please use Ext.Date.format to format the date:
Ext.Date.format(d,'d/m/Y');
According to ExtJS 3.4 Docs Date object is extended with .format() method.
So you should be able to just do
var d = new Date();
d.format("d/m/Y");