char dates

Reply
Contributor
Posts: 30

char dates

Hi,

I have a column in my table

         Alphabetic List of Variables and Attributes 

#    Variable               Type     Len    Format       Informat

1    MNDATE             Char      26      $26.           $26.

Now when I try to do,     

  select  (tab1.mndate - tab2.mndate )  as duration   ......  ........  ...;

it says , cant subtract char...

What should I do to substract the dates.... ?  (  I cannot change my column type...)     Experts please help....

Frequent Contributor
Frequent Contributor
Posts: 83

Re: char dates

Create a   new variable of calendar date from the char date. Then do the calculations and then drop the calendar date.

data tab1;

set tab1;

day=substr(MINDATE,1,2);

month=substr(MINDATE,4,2);

year=substr(MINDATE,7,4);

run;

data tab1;

set tab1;

day1=day*1;

month1=month*1;

year1=year*1;

drop day year month;

run;

data want;

set tab1;

Date1=input(catx('/',day1,month1,year1),ddmmyy10.);

format Date1 ddmmyy10.;

drop date;

run;

Regular Contributor
Posts: 244

Re: char dates

select(input(tab1.mndate,MMDDYY10.) - input(tab2.mndate,MMDDYY10.)) ...

This assumes that is the correct informat; you may need a different one, or even a datetime, in which case you would need datepart(input... ) - datepart(input...) if you want days not seconds.

Trusted Advisor
Posts: 1,131

Re: char dates

Hi Vijay,

As per the proc content screenshot, we believe the date is in character format. In order to perform the calculation on the date, we need to convert the date to numeric, so we need to read the date in numeric format, you can use the informat anydtdte. to read the date converting it to sas date value on which you can perform the calculation.

The interesting thing about this informat anydtdte. is that it can read any type of date whether mmddyy or ddmmyy or any other.

you can use it in the following way similar to snoopy369, just change the format

select(input(tab1.mndate,anydtdte.) - input(tab2.mndate,anydtdte.)) ...

Hope this works.

Thanks,

Jagadish

Thanks,
Jag
Ask a Question
Discussion stats
  • 3 replies
  • 228 views
  • 0 likes
  • 4 in conversation