BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lawilap
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ScottBass
Rhodochrosite | Level 12

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;

 


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

View solution in original post

2 REPLIES 2
ScottBass
Rhodochrosite | Level 12

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;

 


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
lawilap
Fluorite | Level 6

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;

  

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1905 views
  • 0 likes
  • 2 in conversation