Help using Base SAS procedures

Format date

Reply
Contributor
Posts: 22

Format date

Hi,

Could you please help how to format the date, for examples?

from Oct. 1st to 10/01/12

from Oct. 3rd to 10/03/12

from Oct. 22nd to 10/22/12

from Oct. 30th to 10/30/12

Thank you,

le

PROC Star
Posts: 7,468

Re: Format date

Here is one way:

data have;

  informat date $9.;

  input date &;

  cards;

Oct. 1st

Oct. 3rd

Oct. 22nd

Oct. 30th

;

data want;

  set have (rename=(date=char_date));

  format date date9.;

  date=input(

    catt(substrn(char_date, 6,anyalpha(char_date,6)-6),substrn(char_date, 1,3),'2012'),

    date9.);

run;

Respected Advisor
Posts: 4,920

Re: Format date

REGEX is your friend for this kind of operation :

data have;
      length date $20;
      input date &;
datalines;
Oct. 1st
Oct. 3rd
Oct. 22nd
Oct. 30th
;

data want(drop=mth);

retain mth;

set have;

/* Extract a word (the month name) and a number (the day of the month) possibly

followed with a suffix, keep the number followed by the month name. */

if _n_=1 then mth = prxparse("s/(\w{3,})\W+(\d+)(st|rd|nd|th)?/$2 $1/i");

/* Add the current year, convert to a SAS date */

date1 = input(catt(prxchange(mth, 1, date), put(year(today()), 5.)), date20.);

format date1 mmddyy8.;

run;


proc print; run;

PG

PG
Super User
Posts: 10,023

Re: Format date

data have;
  input date & $;
  cards;
Oct. 1st
Oct. 3rd
Oct. 22nd
Oct. 30th
;
run;
data have;
 set have;
 _date=input(cats(scan(date,1, ,'kd'),scan(date,1,' .'),'2012'),date9.);
 format _date date9. ;
run;

Ksharp

Contributor
Posts: 22

Re: Format date

Thank you all. Much appreciated for your help.

Ksharp: I have an error: ERROR 159-185: Null parameters for SCAN are invalid.

-le

PROC Star
Posts: 7,468

Re: Format date

You would have to post your log.  It sounds like there was something wrong with your code.  Ksharp's example worked on my system.

Super User
Posts: 10,023

Re: Format date

Are  you using SAS 9.1 ?  or try something like :

data have;
  input date & $20.;
  cards;
Oct. 1st
Oct. 3rd
Oct. 22nd
Oct. 30th
;
run;
data have;
 set have;
 _date=input(cats(scan(date,1,' ','kd'),scan(date,1,' ','ka'),'2012'),date9.);
 format _date date9. ;
run;

If it still can't work. I suggest you to use Art or PG's code.

Ksharp

Occasional Contributor
Posts: 17

Re: Format date

Hi,

i have tried this simple code to solve this.

data have;

     set have;

_date=input(cats(compress(date,'DK'), substr(date,1,3),'2012'), anydtdte21.);

format _date mmddyy8.;

run;

Rohit

Contributor
Posts: 21

Re: Format date

Posted in reply to rohit_prajapati

Hi,

This is working. The only difference from the code in first reply is in line 3, where you formatting date value to look the way you want.

data want;

  set have (rename=(date=char_date));

  format date mmddyy8.;

  date=input(

    catt(substrn(char_date, 6,anyalpha(char_date,6)-6),substrn(char_date, 1,3),'2012'),

    date9.);

run;

Ask a Question
Discussion stats
  • 8 replies
  • 465 views
  • 0 likes
  • 6 in conversation