BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Moksha
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Moksha
Quartz | Level 8

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.

 

View solution in original post

2 REPLIES 2
mkeintz
Jade | Level 19

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

--------------------------
Moksha
Quartz | Level 8

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.

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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