BookmarkSubscribeRSS Feed
Lani
Calcite | Level 5

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

8 REPLIES 8
art297
Opal | Level 21

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;

PGStats
Opal | Level 21

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
Ksharp
Super User
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

Lani
Calcite | Level 5

Thank you all. Much appreciated for your help.

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

-le

art297
Opal | Level 21

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

Ksharp
Super User

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

rohit_prajapati
Calcite | Level 5

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

Marina
Calcite | Level 5

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 1426 views
  • 0 likes
  • 6 in conversation