Help using Base SAS procedures

fix up mixed dates

Reply
Super Contributor
Posts: 399

fix up mixed dates

I'm not a big fan of SAS dates, so anyone know of a simple and quick way to fix up mixed dates in a dataset.

Have:

abc  Jan-01

abc  Feb-01

abc  01-Mar

abc  01-Apr

def  Jan-01

def  Feb-01

def  01-Mar

and so on......many many obs

Want:

abc  2001.01

abc  2001.02

abc  2001.03

abc  2001.04

def   2001.01

you get the idea...

Thanks

Respected Advisor
Posts: 3,780

Re: fix up mixed dates

The ANY informats don't seem to want to read this so perhaps a user written INFORMAT will work.

proc format;

   picture monyyd(default=6) other='%b-%0y'  (datatype=date);

   picture yymond(default=6) other='%0y-%b'  (datatype=date);

   run;

     

data cntl;

   retain fmtname 'XmonyyX' type 'I' hlo 'UJ ';

   do year=2000 to 2012; * adjust as necessary;

      do month=1 to 12;

         label = mdy(month,1,year);

         start = put(label,monyyd6.);

         output;

         start = put(label,yymond6.);

         output;

         end;

      end;

   format label yyqp.;

   run;

*proc print;

   run;

proc format cntlin=cntl;

   *select @X:;

   run;

data test;

   input id:$3. cdate:$6. +(-7) date:xmonyyx.;

   format date yymmp.;

   cards;

abc  JAN-01

abc  Feb-01

abc  01-Mar

abc  01-Apr

def  Jan-01

def  Feb-01

def  01-Mar

;;;;

   run;

proc print;

   run;

Obs    id     cdate      date

1     abc    JAN-01    2001.01

2     abc    Feb-01    2001.02

3     abc    01-Mar    2001.03

4     abc    01-Apr    2001.04

5     def    Jan-01    2001.01

6     def    Feb-01    2001.02

7     def    01-Mar    2001.03

PROC Star
Posts: 7,416

Re: fix up mixed dates

How about?:

data have (drop=dummy);

  options datestyle=myd;

  format date date9.;

  input (code dummy) ($) ;

  if anydigit(substr(dummy,1,1)) then

   dummy=catt(substr(dummy,4,3),'-',

    substr(dummy,1,2));

  date=input(dummy, anydtdte6.);

  cards;

abc  Jan-01

abc  Feb-01

abc  01-Mar

abc  01-Apr

def  Jan-01

def  Feb-01

def  01-Mar

;

Super Contributor
Posts: 399

fix up mixed dates

Love it how I get anwers and learn a whole lot here.  thanks

Ask a Question
Discussion stats
  • 3 replies
  • 192 views
  • 0 likes
  • 3 in conversation