Pyrite | Level 9

## Find the difference between two char type date variables

Hi All,

In a dataset, I have two columns st_dt and end_dt which are char type dates :

```data have;
input dt1 \$ 1-10 @12 dt2 \$12.;
format dt1 \$12. dt2 \$12.;
datalines;
2012-02-22 2012-06-22
2012-02
2013-05-24
2013 2014-01-01
2013-01-01
;
run;

```

I have to calculate something like this:

if  dt1 >= dt2 then var1 = (dt1-dt2) + 1;

else var1 = dt2 -dt1;

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Pyrite | Level 9

## Re: Find the difference between two char type date variables

Thank you for the reply. As I was trying it continuously, at last I got the solution to first convert the char type date variables to numeric using input for example:

dt1_num = input(dt1,yymmdd10.) and then using format dt1_num yymmdd10.;

Similarly, converted the other date column dt2 also to numeric. I am converting to numeric only if the column is not equal to ' ' and it's length is 10.

After this, I am able to calculate as per the formula I have mentioned in my previous post using these numeric columns.

2 REPLIES 2
PROC Star

## Re: Find the difference between two char type date variables

I guess you are using character variables because always know the year, but sometimes you don't know the day-of-month,  or the month and day-of-month.

But to do the task you want, you first need to establish rules of assigning exact dates, in the face of those missing components.  So what are those rules?

Once you've got those rules, then you can establish numeric date values from your character dates.  Then it's trivial to do comparisons, differences, progression and regression through time span.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Pyrite | Level 9

## Re: Find the difference between two char type date variables

Thank you for the reply. As I was trying it continuously, at last I got the solution to first convert the char type date variables to numeric using input for example:

dt1_num = input(dt1,yymmdd10.) and then using format dt1_num yymmdd10.;

Similarly, converted the other date column dt2 also to numeric. I am converting to numeric only if the column is not equal to ' ' and it's length is 10.

After this, I am able to calculate as per the formula I have mentioned in my previous post using these numeric columns.

Discussion stats
• 2 replies
• 392 views
• 0 likes
• 2 in conversation