BookmarkSubscribeRSS Feed
VijayKumar
Calcite | Level 5

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....

3 REPLIES 3
Mit
Calcite | Level 5 Mit
Calcite | Level 5

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;

snoopy369
Barite | Level 11

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.

Jagadishkatam
Amethyst | Level 16

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

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!

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.

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
  • 3 replies
  • 478 views
  • 0 likes
  • 4 in conversation