I have a character variable which contains dates like: Mon Jul 24 02:48:17 -0700 2017
How can I extract the usable(date,month,year) date and save it as numeric date. I know how to do it by using SUBSTR function and then concatenating everything together. But I want to know a easier and quick way. I tried using ANYDTDTE informat to read it, but got blank values. Let me know if there is any simpler way to read this date.
I had a quick glance at the SAS informats:
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a001239776.htm
My quick glance didn't turn up a suitable informat. Perhaps I missed it.
This works with your given input. Not sure if it's any different than your approach?
data test;
cdate='Mon Jul 24 02:48:17 -0700 2017';
date=input(
cats(
scan(cdate,3,' '),
scan(cdate,2,' '),
scan(cdate,-1,' ')
),date9.);
format date date9.;
run;
I had a quick glance at the SAS informats:
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a001239776.htm
My quick glance didn't turn up a suitable informat. Perhaps I missed it.
This works with your given input. Not sure if it's any different than your approach?
data test;
cdate='Mon Jul 24 02:48:17 -0700 2017';
date=input(
cats(
scan(cdate,3,' '),
scan(cdate,2,' '),
scan(cdate,-1,' ')
),date9.);
format date date9.;
run;
This is awesome. Although I was looking for some informat which can read the string without scanning it. Additionally, I changed the code a bit to keep the same variable in dataset.
data test(drop=cdate rename=(date=cdate));
cdate='Mon Jul 24 02:48:17 -0700 2017';
date=input(
cats(
scan(cdate,3,' '),
scan(cdate,2,' '),
scan(cdate,-1,' ')
),date9.);
format date date9.;
run;
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.