0
votes

I have a character variable which contains dates like (string length is 30):

Mon Jul 24 02:48:17 -0700 2017
------------------------------ ruler
123456789012345678901234567890
         1         2         3

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.

1
Show your code with substr. Do you have to deal with this sort of input many multiple times ? If so, you could place the code for conversion to SAS date value in a macro. If the existing code works and the date string value construct is not variant maybe just accept it and move on. Just be sure to give the assigned variable a date format.Richard

1 Answers

0
votes

Do you need just the date? Or do you need to get the time part also? The SCAN() function can make this easier.

data test;
  str='Mon Jul 24 02:48:17 -0700 2017';
  date=input(cats(scan(str,3),scan(str,2),scan(str,-1)),date9.);
  format date date9.;
  datetime=input(cats(scan(str,3),scan(str,2),scan(str,-1),':',scan(str,4,' ')),datetime20.);
  format datetime datetime19.;
run;

Result:

str=Mon Jul 24 02:48:17 -0700 2017 
date=24JUL2017 
datetime=24JUL2017:02:48:17