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;

  

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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