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

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
Pyrite | Level 9

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
PROC Star

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
Pyrite | Level 9

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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 296 views
  • 0 likes
  • 2 in conversation