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;
Please, advise me how to do this with sample code.
Thanks
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.
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.
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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.